import {getDefaultPagination} from '@/utils/pagination'; import {useService} from '@/services'; import {plainClone} from '@/utils/object'; import {useRouter} from 'vue-router'; import {emptyObjectFunc} from '@/utils/func'; import {translate} from '@/utils/i18n'; // i18n const t = translate; export const getDefaultStoreState = (ns: StoreNamespace): BaseStoreState => { return { ns, dialogVisible: { createEdit: true, }, activeDialogKey: undefined, createEditDialogTabName: 'single', form: {} as T, isSelectiveForm: false, selectedFormFields: [], readonlyFormFields: [], formList: [], newFormFn: emptyObjectFunc, confirmLoading: false, tableData: [], tableTotal: 0, tablePagination: getDefaultPagination(), tableListFilter: [], tableListSort: [], allList: [], sidebarCollapsed: false, actionsCollapsed: false, tabs: [{id: 'overview', title: t('common.tabs.overview')}], afterSave: [], }; }; export const getDefaultStoreGetters = (opts?: GetDefaultStoreGettersOptions): BaseStoreGetters> => { if (!opts) opts = {}; if (!opts.selectOptionValueKey) opts.selectOptionValueKey = '_id'; if (!opts.selectOptionLabelKey) opts.selectOptionLabelKey = 'name'; return { dialogVisible: (state: BaseStoreState) => state.activeDialogKey !== undefined, // isBatchForm: (state: BaseStoreState) => state.isSelectiveForm || state.createEditDialogTabName === 'batch', isBatchForm: (state: BaseStoreState) => { return state.isSelectiveForm || state.createEditDialogTabName === 'batch'; }, formListIds: (state: BaseStoreState) => state.formList.map(d => (d as BaseModel)._id as string), allListSelectOptions: (state: BaseStoreState) => state.allList.map(d => { const _d = d as BaseModel; return { value: _d[opts?.selectOptionValueKey as string], label: _d[opts?.selectOptionLabelKey as string], }; }), allDict: (state: BaseStoreState) => { const dict = new Map(); state.allList.forEach(d => dict.set((d as BaseModel)._id as string, d as any)); return dict; }, /** * @deprecated */ tabName: () => { const router = useRouter(); if (!router) return ''; const arr = router.currentRoute.value.path.split('/'); if (arr.length < 3) return ''; return arr[3]; }, allTags: (state: BaseStoreState, getters, rootState) => { return rootState.tag.allList.filter(d => d.col === `${state.ns}s`); }, tabs: (state: BaseStoreState, getters, rootState) => { return state.tabs.filter(t => rootState.layout.detailTabVisibleFn(state.ns, t)); }, }; }; export const getDefaultStoreMutations = (): BaseStoreMutations => { return { showDialog: (state: BaseStoreState, key: DialogKey) => { state.activeDialogKey = key; }, hideDialog: (state: BaseStoreState) => { // reset all other state variables state.createEditDialogTabName = 'single'; state.isSelectiveForm = false; state.selectedFormFields = []; state.formList = []; state.confirmLoading = false; // set active dialog key as undefined state.activeDialogKey = undefined; }, setCreateEditDialogTabName: (state: BaseStoreState, tabName: CreateEditTabName) => { state.createEditDialogTabName = tabName; }, resetCreateEditDialogTabName: (state: BaseStoreState) => { state.createEditDialogTabName = 'single'; }, setForm: (state: BaseStoreState, value: T) => { state.form = value; }, resetForm: (state: BaseStoreState) => { state.form = state.newFormFn() as T; }, setIsSelectiveForm: (state: BaseStoreState, value: boolean) => { state.isSelectiveForm = value; }, setSelectedFormFields: (state: BaseStoreState, value: string[]) => { state.selectedFormFields = value; }, resetSelectedFormFields: (state: BaseStoreState) => { state.selectedFormFields = []; }, setReadonlyFormFields: (state: BaseStoreState, value: string[]) => { state.readonlyFormFields = value; }, resetReadonlyFormFields: (state: BaseStoreState) => { state.readonlyFormFields = []; }, setFormList: (state: BaseStoreState, value: T[]) => { state.formList = value; }, resetFormList: (state: BaseStoreState) => { state.formList = []; }, setConfirmLoading: (state: BaseStoreState, value: boolean) => { state.confirmLoading = value; }, resetTableData: (state: BaseStoreState) => { state.tableData = []; }, setTableData: (state: BaseStoreState, payload: TableDataWithTotal) => { const {data, total} = payload; state.tableData = data; state.tableTotal = total; }, setTablePagination: (state: BaseStoreState, pagination: TablePagination) => { state.tablePagination = pagination; }, resetTablePagination: (state: BaseStoreState) => { state.tablePagination = getDefaultPagination(); }, setTableListFilter: (state: BaseStoreState, filter: FilterConditionData[]) => { state.tableListFilter = filter; }, resetTableListFilter: (state: BaseStoreState) => { state.tableListFilter = []; }, setTableListFilterByKey: (state: BaseStoreState, {key, conditions}) => { const filter = state.tableListFilter.filter(d => d.key !== key); conditions.forEach(d => { d.key = key; filter.push(d); }); state.tableListFilter = filter; }, resetTableListFilterByKey: (state: BaseStoreState, key) => { state.tableListFilter = state.tableListFilter.filter(d => d.key !== key); }, setTableListSort: (state: BaseStoreState, sort: SortData[]) => { state.tableListSort = sort; }, resetTableListSort: (state: BaseStoreState) => { state.tableListSort = []; }, setTableListSortByKey: (state: BaseStoreState, {key, sort}) => { const idx = state.tableListSort.findIndex(d => d.key === key); if (idx === -1) { if (sort) { state.tableListSort.push(sort); } } else { if (sort) { state.tableListSort[idx] = plainClone(sort); } else { state.tableListSort.splice(idx, 1); } } }, resetTableListSortByKey: (state: BaseStoreState, key) => { state.tableListSort = state.tableListSort.filter(d => d.key !== key); }, setAllList: (state: BaseStoreState, value: T[]) => { state.allList = value; }, resetAllList: (state: BaseStoreState) => { state.allList = []; }, expandSidebar: (state: BaseStoreState) => { state.sidebarCollapsed = false; }, collapseSidebar: (state: BaseStoreState) => { state.sidebarCollapsed = true; }, expandActions: (state: BaseStoreState) => { state.actionsCollapsed = false; }, collapseActions: (state: BaseStoreState) => { state.actionsCollapsed = true; }, setTabs: (state: BaseStoreState, tabs) => { state.tabs = tabs; }, setAfterSave: (state: BaseStoreState, fnList) => { state.afterSave = fnList; }, }; }; export const getDefaultStoreActions = (endpoint: string): BaseStoreActions => { const { getById, create, updateById, deleteById, getList, getAll, createList, updateList, deleteList, } = useService(endpoint); return { getById: async ({commit}: StoreActionContext>, id: string) => { const res = await getById(id); commit('setForm', res.data); return res; }, create: async ({commit}: StoreActionContext>, form: T) => { const res = await create(form); return res; }, updateById: async ({commit}: StoreActionContext>, {id, form}: { id: string; form: T }) => { const res = await updateById(id, form); return res; }, deleteById: async ({commit}: StoreActionContext>, id: string) => { const res = await deleteById(id); return res; }, getList: async ({state, commit}: StoreActionContext>) => { const {page, size} = state.tablePagination; const res = await getList({ page, size, conditions: JSON.stringify(state.tableListFilter), sort: JSON.stringify(state.tableListSort), } as ListRequestParams); commit('setTableData', {data: res.data || [], total: res.total}); return res; }, getListWithParams: async (_: StoreActionContext>, params?: ListRequestParams) => { return await getList(params); }, getAllList: async ({commit}: StoreActionContext>) => { const res = await getAll(); commit('setAllList', res.data || []); return res; }, createList: async ({state, commit}: StoreActionContext>, data: T[]) => { const res = await createList(data); return res; }, updateList: async ({state, commit}: StoreActionContext>, { ids, data, fields, }: BatchRequestPayloadWithData) => { const res = await updateList(ids, data, fields); return res; }, deleteList: async ({commit}: StoreActionContext>, ids: string[]) => { return await deleteList(ids); }, }; };