101 lines
3.4 KiB
TypeScript
101 lines
3.4 KiB
TypeScript
import type { Action, Thunk } from 'easy-peasy'
|
||
import { action, thunk } from 'easy-peasy'
|
||
import { Injections, StoreModel } from '.'
|
||
import { getLocalization, setLocalization } from '../services/localization.service'
|
||
import appConfig from '../proxy/configs/app.config'
|
||
import { NavigationTree } from '../proxy/menus/navigation'
|
||
import { MenuDto } from '../proxy/menus/models'
|
||
import getChildren from '../utils/navigation'
|
||
import {
|
||
ApplicationConfigurationDto,
|
||
ApplicationLocalizationRequestDto,
|
||
} from '../proxy/config/models'
|
||
|
||
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)
|
||
|
||
//Eğer login değilse
|
||
if (result.data.currentUser.isAuthenticated) {
|
||
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) => {
|
||
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)
|
||
}),
|
||
}
|