Skip to content

Commit ff8b907

Browse files
authored
Merge pull request #60 from spendingjp/feature/50_call_budget_api
Feature/50 call budget api
2 parents c5aa6b4 + 4f9e4d8 commit ff8b907

18 files changed

+313
-49
lines changed

components/molecules/HouseTypeSelection.vue

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,20 @@ export default Vue.extend({
4141
selected: 'SINGLE',
4242
}
4343
},
44-
mounted() {
45-
this.selectHouseType(this.$props.selectedType)
44+
computed: {
45+
parsedCofogData() {
46+
return this.$accessor.regionCofogData.parsedCofogData
47+
},
48+
},
49+
watch: {
50+
parsedCofogData(val, old) {
51+
if (!('amount' in old) && 'amount' in val) {
52+
// @ts-ignore
53+
this.selectHouseType(this.$props.selectedType)
54+
}
55+
},
4656
},
57+
mounted() {},
4758
methods: {
4859
/**
4960
* 世帯種別を選択する
@@ -55,6 +66,7 @@ export default Vue.extend({
5566
}
5667
this.$emit('selectd-value', type)
5768
this.$accessor.dailyBreadData.setHouseType(type)
69+
// @ts-ignore
5870
this.selected = type
5971
},
6072
},

components/molecules/IncomeSelector.vue

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ export default Vue.extend({
133133
yearlyTax() {
134134
return this.$accessor.dailyBreadData.yearlyTax
135135
},
136+
parsedCofogData() {
137+
return this.$accessor.regionCofogData.parsedCofogData
138+
},
136139
},
137140
watch: {
138141
sliderValue(newVal) {
@@ -142,10 +145,13 @@ export default Vue.extend({
142145
this.$accessor.dailyBreadData.setIncome(newVal)
143146
}, 100)
144147
},
148+
parsedCofogData(val, old) {
149+
if (!('amount' in old) && 'amount' in val) {
150+
this.$accessor.dailyBreadData.setIncome(this.value)
151+
}
152+
},
145153
},
146-
mounted() {
147-
this.$accessor.dailyBreadData.setIncome(this.value)
148-
},
154+
mounted() {},
149155
} as ThisTypedComponentOptionsWithRecordProps<Vue, DataType, MethodType, ComputedType, PropType>)
150156
</script>
151157

pages/bubbletree.vue

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,35 @@ export default Vue.extend({
288288
title: '使途別予算額',
289289
}
290290
},
291-
created() {
292-
const cofogData: CofogData = this.$repositories('cofogData').Get()
291+
async beforeMount() {
292+
// TODO: move to middleware
293+
const hostname = window.location.hostname
294+
try {
295+
this.$accessor.regionCofogData.setHostname(hostname)
296+
} catch (err) {
297+
if (err instanceof SyntaxError) {
298+
console.error('Error on beforeMount: ', err)
299+
this.$router.push({
300+
path: '404.html',
301+
})
302+
return
303+
}
304+
}
305+
306+
try {
307+
await this.$accessor.regionCofogData.fetchBudgetListAndWdmmgData()
308+
} catch (err) {
309+
if (err instanceof ReferenceError) {
310+
console.error('Error on beforeMount: ', err)
311+
this.$router.push({
312+
path: '404.html',
313+
})
314+
}
315+
}
316+
const cofogData: CofogData = this.$accessor.regionCofogData.parsedCofogData
317+
if (!('amount' in cofogData)) {
318+
this.$router.push({ path: '/' })
319+
}
293320
this.circlePackData.childrenData = cofogData.taxList.map((item) => ({
294321
name: item.cofog.Name,
295322
value: item.amount.value,
@@ -302,8 +329,7 @@ export default Vue.extend({
302329
})),
303330
})),
304331
}))
305-
},
306-
mounted() {
332+
307333
const svg = d3.select('#graph')
308334
309335
const viewBox = svg.attr('viewBox')
@@ -331,6 +357,7 @@ export default Vue.extend({
331357
})
332358
this.setListText(this.circlePackData, '')
333359
},
360+
mounted() {},
334361
methods: {
335362
/**
336363
* ズーム

pages/index.vue

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,32 @@ export default Vue.extend({
1717
title: '使途一日あたり',
1818
}
1919
},
20+
async beforeMount() {
21+
// execute only on client side.
22+
// TODO: move to middleware
23+
const hostname = window.location.hostname
24+
try {
25+
this.$accessor.regionCofogData.setHostname(hostname)
26+
} catch (err) {
27+
if (err instanceof SyntaxError) {
28+
console.error('Error on beforeMount: ', err)
29+
this.$router.push({
30+
path: '404.html',
31+
})
32+
return
33+
}
34+
}
35+
36+
try {
37+
await this.$accessor.regionCofogData.fetchBudgetListAndWdmmgData()
38+
} catch (err) {
39+
if (err instanceof ReferenceError) {
40+
console.error('Error on beforeMount: ', err)
41+
this.$router.push({
42+
path: '404.html',
43+
})
44+
}
45+
}
46+
},
2047
})
2148
</script>

plugins/Axios.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ let $axios: NuxtAxiosInstance
55
export function initializeAxios(axiosInstance: NuxtAxiosInstance) {
66
$axios = axiosInstance
77

8+
$axios.defaults.xsrfCookieName = 'csrftoken'
9+
$axios.defaults.xsrfHeaderName = 'X-CSRFTOKEN'
10+
$axios.defaults.headers = {
11+
'Content-Type': 'application/json',
12+
'X-Requested-With': 'XMLHttpRequest',
13+
}
14+
815
$axios.onRequest((config) => {
916
console.log('request to ' + config.baseURL)
1017
return config

plugins/api/BudgetList.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export type BudgetResponse = {
2+
id: string
3+
name: string
4+
slug: string
5+
year: string
6+
subtitle: string
7+
classificationSystem: string
8+
government: string
9+
sourceBudget?: string
10+
createdAt: string
11+
updatedAt: string
12+
}
13+
14+
export type Budget = BudgetResponse
15+
16+
export type BudgetListResponse = {
17+
"budgets": BudgetResponse[]
18+
"defaultBudgets": BudgetResponse
19+
}
20+
21+
export type BudgetList = BudgetListResponse
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { NuxtAppOptions } from '@nuxt/types/app'
2+
import { BudgetList, BudgetListResponse } from '../api/BudgetList'
3+
4+
5+
export class BudgetListApiService {
6+
private app: NuxtAppOptions
7+
8+
constructor(app: NuxtAppOptions) {
9+
this.app = app
10+
}
11+
12+
/**
13+
* APIからBudgetListデータを取得する
14+
* @returns BudgetListデータ
15+
*/
16+
public async GetData(): Promise<BudgetList> {
17+
try {
18+
const repo = this.app.$repositories('budget')
19+
const budgetListResponse: BudgetListResponse = await repo.Get()
20+
21+
return budgetListResponse
22+
} catch (e) {
23+
console.log(e)
24+
throw new Error('APIレスポンスをオブジェクトに変換失敗')
25+
}
26+
}
27+
}

plugins/applicationServices/COFOGAPIService.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@ export class COFOGAPIService {
2424
* APIからCOFOGデータを取得する
2525
* @returns COFOGデータ
2626
*/
27-
public async GetData(): Promise<CofogData> {
27+
public async GetData(budgetSlug: string): Promise<CofogData> {
2828
try {
2929
const apiResponse: COFOGAPIResponse = await this.app
3030
.$repositories('cofog')
31-
.Get()
31+
.Get(budgetSlug)
3232

3333
const taxList: TaxItemLevel1[] = apiResponse.budgets.map((item) => ({
3434
...this.ConvertBudget2TaxItem(item),
3535
children:
3636
item.children !== null
3737
? item.children.map((level2item) => ({
38-
...this.ConvertBudget2TaxItem(level2item),
39-
children:
40-
level2item.children !== null
41-
? level2item.children.map((level3item) =>
42-
this.ConvertBudget2TaxItem(level3item)
43-
)
44-
: [],
45-
}))
38+
...this.ConvertBudget2TaxItem(level2item),
39+
children:
40+
level2item.children !== null
41+
? level2item.children.map((level3item) =>
42+
this.ConvertBudget2TaxItem(level3item)
43+
)
44+
: [],
45+
}))
4646
: [],
4747
}))
4848

@@ -51,7 +51,7 @@ export class COFOGAPIService {
5151
taxList,
5252
year: apiResponse.year,
5353
governmentName: apiResponse.government.name,
54-
budgetName: apiResponse.sourceBudget.name,
54+
budgetName: apiResponse.name,
5555
}
5656
} catch (e) {
5757
throw new Error('APIレスポンスをオブジェクトに変換失敗')

plugins/applicationServices/TaxApplicationService.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ export class TaxApplicationService {
3131
*/
3232
public GetDailyBreadData(person: Person): DailyBread {
3333
// COFOGデータを取得
34-
const cofogData: CofogData = this.app.$repositories('cofogData').Get()
35-
if (cofogData === undefined) {
34+
// const cofogData: CofogData = this.app.$repositories('cofogData').Get()
35+
const cofogData: CofogData = this.app.$accessor.regionCofogData.parsedCofogData
36+
if (!("amount" in cofogData)) {
3637
throw new Error('COFOGデータなし')
3738
}
3839

@@ -76,8 +77,8 @@ export class TaxApplicationService {
7677
children:
7778
'children' in item
7879
? item.children.map((child: TaxItemLevel2 | TaxItemLevel3) =>
79-
this.ConvertTax2BreadItem(child, service, person, government)
80-
)
80+
this.ConvertTax2BreadItem(child, service, person, government)
81+
)
8182
: null,
8283
}
8384
}

plugins/factories/ApiRepositoryFactory.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@ import { CofogDataRepository } from '../repositories/CofogDataRepository'
33
import {
44
CofogRepository
55
} from '~/plugins/repositories/CofogRepository'
6+
import { BudgetRepository } from '../repositories/BudgetRepository'
67

78
export interface Repositories {
8-
cofog: typeof CofogRepository,
9+
cofog: typeof CofogRepository
910
cofogData: typeof CofogDataRepository
11+
budget: typeof BudgetRepository
1012
// sample: typeof SampleRepository
1113
}
1214

1315
export const repositories: Repositories = {
1416
cofog: CofogRepository,
15-
cofogData: CofogDataRepository
17+
cofogData: CofogDataRepository,
18+
budget: BudgetRepository,
1619
// sample: SampleRepository
1720
}
1821

0 commit comments

Comments
 (0)