-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: change to use store instead of passing props
Signed-off-by: NaYeong,Kim <[email protected]>
- Loading branch information
Showing
5 changed files
with
124 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
apps/web/src/services/cost-explorer/stores/data-sources-page-store.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { computed, reactive } from 'vue'; | ||
|
||
import dayjs from 'dayjs'; | ||
import { defineStore } from 'pinia'; | ||
|
||
import { SpaceConnector } from '@cloudforet/core-lib/space-connector'; | ||
|
||
import type { ListResponse } from '@/schema/_common/api-verbs/list'; | ||
import type { CostDataSourceListParameters } from '@/schema/cost-analysis/data-source/api-verbs/list'; | ||
import type { DataSourceModel } from '@/schema/monitoring/data-source/model'; | ||
import { store } from '@/store'; | ||
|
||
import { useAllReferenceStore } from '@/store/reference/all-reference-store'; | ||
|
||
import { assetUrlConverter } from '@/lib/helper/asset-helper'; | ||
|
||
import ErrorHandler from '@/common/composables/error/errorHandler'; | ||
|
||
import type { DataSourceItem } from '@/services/cost-explorer/types/data-sources-type'; | ||
|
||
export const useDataSourcesPageStore = defineStore('page-data-sources', () => { | ||
const allReferenceStore = useAllReferenceStore(); | ||
const allReferenceGetters = allReferenceStore.getters; | ||
|
||
const state = reactive({ | ||
dataSourceList: [] as DataSourceModel[], | ||
totalCount: 0, | ||
selectedIndices: [] as number[], | ||
}); | ||
|
||
const _getters = reactive({ | ||
provider: computed(() => allReferenceGetters.provider), | ||
timezone: computed(() => store.state.user.timezone), | ||
}); | ||
|
||
const getters = reactive({ | ||
dataSourceList: computed<DataSourceItem[]>(() => state.dataSourceList.map((i) => { | ||
const icon = _getters.provider[i.provider].icon; | ||
return { | ||
...i, | ||
icon: assetUrlConverter(icon), | ||
created_at: dayjs(i.created_at).tz(_getters.timezone).format('YYYY-MM-DD HH:mm:ss'), | ||
}; | ||
})), | ||
selectedItem: computed<DataSourceItem>(() => getters.dataSourceList[state.selectedIndices[0]]), | ||
}); | ||
|
||
const mutation = { | ||
setSelectedIndices: (indices: number[]) => { | ||
state.selectedIndices = indices; | ||
}, | ||
}; | ||
|
||
const actions = { | ||
fetchDataSourceList: async () => { | ||
try { | ||
const { results, total_count } = await SpaceConnector.clientV2.costAnalysis.dataSource.list<CostDataSourceListParameters, ListResponse<DataSourceModel>>(); | ||
state.dataSourceList = results || []; | ||
state.totalCount = total_count || 0; | ||
} catch (e) { | ||
ErrorHandler.handleError(e); | ||
state.dataSourceList = []; | ||
state.totalCount = 0; | ||
} | ||
}, | ||
}; | ||
|
||
return { | ||
state, | ||
getters, | ||
...mutation, | ||
...actions, | ||
}; | ||
}); |