erp-platform/ui/src/store/abpConfig.model.ts
Sedat ÖZTÜRK e1a9562b22 init project
2025-05-06 09:45:49 +03:00

154 lines
4.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NavigationTree } from '@/@types/navigation'
import appConfig from '@/configs/app.config'
import {
ApplicationConfigurationDto,
ApplicationLocalizationRequestDto,
} from '@/proxy/config/models'
import { MenuDto } from '@/proxy/menus'
import getChildren from '@/utils/navigation'
import type { Action, Thunk } from 'easy-peasy'
import { action, thunk } from 'easy-peasy'
import { Injections, StoreModel } from '.'
import { getLocalization, setLocalization } from '../services/localization.service'
export type Texts = Record<string, Record<string, string>>
export interface AbpConfigModel {
config?: ApplicationConfigurationDto
setConfig: Action<AbpConfigModel, ApplicationConfigurationDto | undefined>
getConfig: Thunk<AbpConfigModel, boolean, Injections>
texts?: Texts
setTexts: Action<AbpConfigModel, Texts | undefined>
getTexts: Thunk<AbpConfigModel, ApplicationLocalizationRequestDto, Injections>
menu: {
mainMenu: NavigationTree[]
}
setMenu: Action<AbpConfigModel, NavigationTree[]>
getMenu: Thunk<AbpConfigModel, never, Injections, StoreModel>
}
export const abpConfigModel: AbpConfigModel = {
config: undefined,
setConfig: action((state, payload) => {
state.config = payload
}),
getConfig: thunk(async (actions, payload, helpers) => {
const service = helpers.injections.abpConfigService
const result = await service.getAppConfig(payload)
// AppConfig'den gelen culture mevcuttan farklı ise localizationları da al
const newCulture = result.data?.localization.currentCulture.cultureName
const currentCulture = helpers.getState().config?.localization.currentCulture.cultureName
const isCultureDifferent = newCulture && currentCulture !== newCulture
const isTextsEmpty = !helpers.getState().texts
if (isCultureDifferent || isTextsEmpty) {
await actions.getTexts({
cultureName: newCulture ?? currentCulture ?? appConfig.locale,
onlyDynamics: false,
})
}
// Set HTML Lang
document.documentElement.setAttribute('lang', newCulture ?? currentCulture ?? appConfig.locale)
actions.setConfig(result.data)
await actions.getMenu()
}),
texts: undefined,
setTexts: action((state, payload) => {
state.texts = payload
}),
getTexts: thunk(async (actions, payload, helpers) => {
const service = helpers.injections.abpConfigService
const result = await service.getLocalizations(payload)
actions.setTexts(setLocalization(result.data))
}),
menu: {
mainMenu: [],
},
setMenu: action((state, payload) => {
// const formMenu: NavigationTree = {
// key: 'form',
// path: 'form/Form-0001',
// title: 'Form',
// type: 'item',
// translateKey: 'form',
// icon: 'form',
// subMenu: [],
// authority: [],
// }
// state.menu.mainMenu = [...payload, formMenu]
state.menu.mainMenu = [...payload]
}),
getMenu: thunk(async (actions, _, { injections, getState, getStoreState }) => {
const { signedIn } = getStoreState().auth.session
if (!signedIn) {
return
}
const result = await injections.menuService.getList({
sorting: 'order',
skipCount: 0,
maxResultCount: 1000,
})
const texts = getState().texts
const defaultResourceName = getState().config?.localization.defaultResourceName
result.data.items?.forEach((menu: MenuDto) => {
const displayName = '::' + menu.displayName
menu.displayName = getLocalization(texts, defaultResourceName, displayName)
})
const menu = getChildren(result.data.items ?? [], null)
actions.setMenu(menu)
/*
const appsMenuConfig: NavigationTree[] = [
{
key: 'apps',
path: '',
title: 'APPS',
translateKey: 'nav.apps',
icon: 'apps',
type: NAV_ITEM_TYPE_TITLE,
authority: [ADMIN, USER],
subMenu: [
{
key: 'apps.project',
path: '',
title: 'Project',
translateKey: 'nav.appsProject.project',
icon: 'project',
type: NAV_ITEM_TYPE_COLLAPSE,
authority: [ADMIN, USER],
subMenu: [
{
key: 'appsProject.dashboard',
path: `${APP_PREFIX_PATH}/project/dashboard`,
title: 'Dashboard',
translateKey: 'nav.appsProject.dashboard',
icon: '',
type: NAV_ITEM_TYPE_ITEM,
authority: [ADMIN, USER],
subMenu: [],
},
{
key: 'appsProject.projectList',
path: `${APP_PREFIX_PATH}/project/project-list`,
title: 'Project List',
translateKey: 'nav.appsProject.projectList',
icon: '',
type: NAV_ITEM_TYPE_ITEM,
authority: [ADMIN, USER],
subMenu: [],
},
],
},
],
},
]
*/
}),
}