sozsoft-platform/ui/src/store/base.model.ts

79 lines
1.9 KiB
TypeScript
Raw Normal View History

2026-02-24 20:44:16 +00:00
import type { Action } from 'easy-peasy'
import { action } from 'easy-peasy'
export interface StoreError {
id: string
title: string
message: string
cid: string
statusCode?: string
}
export interface BaseStoreModel {
common: {
currentRouteKey: string
tabHasFocus: boolean
2026-04-23 10:36:51 +00:00
setupMode: boolean /** Veritabanı mevcut değilse true — setup sayfasına yönlendirme için */
2026-02-24 20:44:16 +00:00
}
messages: {
errors: StoreError[]
warning: string[]
}
}
export interface BaseStoreActions {
common: {
setCurrentRouteKey: Action<BaseStoreModel['common'], string>
setTabHasFocus: Action<BaseStoreModel['common'], boolean>
2026-04-23 10:36:51 +00:00
setSetupMode: Action<BaseStoreModel['common'], boolean>
2026-02-24 20:44:16 +00:00
}
messages: {
addError: Action<BaseStoreModel['messages'], StoreError>
removeError: Action<BaseStoreModel['messages'], string>
// setSuccess: Action<BaseStoreModel, string>
setWarning: Action<BaseStoreModel['messages'], string>
}
}
export type BaseModel = BaseStoreModel & BaseStoreActions
const initialState: BaseStoreModel = {
2026-04-23 10:36:51 +00:00
common: { currentRouteKey: '', tabHasFocus: false, setupMode: false },
2026-02-24 20:44:16 +00:00
messages: {
errors: [],
// success: [],
warning: [],
2026-04-23 10:36:51 +00:00
}
2026-02-24 20:44:16 +00:00
}
export const baseModel: BaseModel = {
common: {
...initialState.common,
setCurrentRouteKey: action((state, payload) => {
state.currentRouteKey = payload
}),
setTabHasFocus: action((state, payload) => {
state.tabHasFocus = payload
}),
2026-04-23 10:36:51 +00:00
setSetupMode: action((state, payload) => {
state.setupMode = payload
}),
2026-02-24 20:44:16 +00:00
},
messages: {
...initialState.messages,
addError: action((state, payload) => {
state.errors = [...state.errors, payload]
}),
removeError: action((state, payload) => {
state.errors = [...state.errors.filter((a) => a.id != payload)]
}),
setWarning: action((state, payload) => {
if (payload) {
state.warning = [payload]
} else {
state.warning = []
}
}),
},
}