From d28a812bc2b62fc583469a6574ee92e8a2c07e65 Mon Sep 17 00:00:00 2001 From: Germey Date: Sat, 30 Dec 2023 13:34:45 +0800 Subject: [PATCH] refactor vuex --- src/store/chat.ts | 26 ---------- src/store/chat/actions.ts | 9 ++++ src/store/chat/index.ts | 14 ++++++ src/store/chat/models.ts | 7 +++ src/store/chat/mutations.ts | 16 +++++++ src/store/chat/state.ts | 8 ++++ src/store/common.ts | 79 ------------------------------- src/store/common/actions.ts | 26 ++++++++++ src/store/common/getters.ts | 29 ++++++++++++ src/store/common/index.ts | 11 +++++ src/store/common/models.ts | 31 ++++++++++++ src/store/common/mutations.ts | 39 +++++++++++++++ src/store/common/state.ts | 17 +++++++ src/store/index.ts | 8 ++-- src/store/midjourney.ts | 29 ------------ src/store/midjourney/actions.ts | 14 ++++++ src/store/midjourney/index.ts | 18 +++++++ src/store/midjourney/models.ts | 6 +++ src/store/midjourney/mutations.ts | 15 ++++++ src/store/models.ts | 40 ---------------- 20 files changed, 264 insertions(+), 178 deletions(-) delete mode 100644 src/store/chat.ts create mode 100644 src/store/chat/actions.ts create mode 100644 src/store/chat/index.ts create mode 100644 src/store/chat/models.ts create mode 100644 src/store/chat/mutations.ts create mode 100644 src/store/chat/state.ts delete mode 100644 src/store/common.ts create mode 100644 src/store/common/actions.ts create mode 100644 src/store/common/getters.ts create mode 100644 src/store/common/index.ts create mode 100644 src/store/common/models.ts create mode 100644 src/store/common/mutations.ts create mode 100644 src/store/common/state.ts delete mode 100644 src/store/midjourney.ts create mode 100644 src/store/midjourney/actions.ts create mode 100644 src/store/midjourney/index.ts create mode 100644 src/store/midjourney/models.ts create mode 100644 src/store/midjourney/mutations.ts delete mode 100644 src/store/models.ts diff --git a/src/store/chat.ts b/src/store/chat.ts deleted file mode 100644 index bab414e..0000000 --- a/src/store/chat.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { IApplication } from '@/operators'; -import { Module } from 'vuex'; -import { IChatState, Status } from './models'; - -export const chat: Module = { - namespaced: true, - state(): IChatState { - return { - applications: undefined, - applicationsStatus: undefined - }; - }, - mutations: { - setApplications(state: IChatState, payload: IApplication[]): void { - state.applications = payload; - }, - setApplicationsStatus(state: IChatState, payload: Status): void { - state.applicationsStatus = payload; - } - }, - actions: { - async setApplications({ commit }: any, payload: IApplication[]) { - commit('setApplications', payload); - } - } -}; diff --git a/src/store/chat/actions.ts b/src/store/chat/actions.ts new file mode 100644 index 0000000..51c58f3 --- /dev/null +++ b/src/store/chat/actions.ts @@ -0,0 +1,9 @@ +import { IApplication } from '@/operators'; + +export const setApplications = async ({ commit }: any, payload: IApplication[]) => { + commit('setApplications', payload); +}; + +export default { + setApplications +}; diff --git a/src/store/chat/index.ts b/src/store/chat/index.ts new file mode 100644 index 0000000..d0f59a1 --- /dev/null +++ b/src/store/chat/index.ts @@ -0,0 +1,14 @@ +import { Module } from 'vuex'; +import { IChatState } from './models'; +import state from './state'; +import actions from './actions'; +import mutations from './mutations'; + +export const chat: Module = { + namespaced: true, + state, + mutations, + actions +}; + +export default chat; diff --git a/src/store/chat/models.ts b/src/store/chat/models.ts new file mode 100644 index 0000000..86c553d --- /dev/null +++ b/src/store/chat/models.ts @@ -0,0 +1,7 @@ +import { IApplication } from '@/operators'; +import { Status } from '../common/models'; + +export interface IChatState { + applications: IApplication[] | undefined; + applicationsStatus: Status | undefined; +} diff --git a/src/store/chat/mutations.ts b/src/store/chat/mutations.ts new file mode 100644 index 0000000..0757fc9 --- /dev/null +++ b/src/store/chat/mutations.ts @@ -0,0 +1,16 @@ +import { IApplication } from '@/operators'; +import { IChatState } from './models'; +import { Status } from '../common/models'; + +export const setApplications = (state: IChatState, payload: IApplication[]): void => { + state.applications = payload; +}; + +export const setApplicationsStatus = (state: IChatState, payload: Status): void => { + state.applicationsStatus = payload; +}; + +export default { + setApplications, + setApplicationsStatus +}; diff --git a/src/store/chat/state.ts b/src/store/chat/state.ts new file mode 100644 index 0000000..0f986ff --- /dev/null +++ b/src/store/chat/state.ts @@ -0,0 +1,8 @@ +import { IChatState } from './models'; + +export default (): IChatState => { + return { + applications: undefined, + applicationsStatus: undefined + }; +}; diff --git a/src/store/common.ts b/src/store/common.ts deleted file mode 100644 index da941a8..0000000 --- a/src/store/common.ts +++ /dev/null @@ -1,79 +0,0 @@ -import { ENDPOINT } from '@/constants'; -import { IApplication, IUser } from '@/operators'; -import { ActionContext, Module } from 'vuex'; -import { ICommonState, IRootState, ISetting, IToken } from './models'; - -export const common: Module = { - namespaced: true, - state(): ICommonState { - return { - user: {}, - token: { - access: undefined, - refresh: undefined - }, - setting: { - endpoint: ENDPOINT, - stream: false - }, - applications: undefined - }; - }, - mutations: { - setUser(state: ICommonState, payload: IUser): void { - state.user = { - ...state.user, - ...payload - }; - }, - setToken(state: ICommonState, payload: IToken): void { - state.token = { - ...state.token, - ...payload - }; - }, - resetToken(state: ICommonState): void { - state.token = {}; - }, - setSetting(state: ICommonState, payload: ISetting): void { - state.setting = { - ...state.setting, - ...payload - }; - }, - setApplications(state: ICommonState, payload: IApplication[]): void { - state.applications = payload; - } - }, - actions: { - resetToken({ commit }: ActionContext) { - commit('resetToken'); - }, - setToken({ commit }: ActionContext, payload: IToken) { - commit('setToken', payload); - }, - setUser({ commit }: ActionContext, payload: IUser) { - commit('setUser', payload); - }, - setApplications({ commit }: ActionContext, payload: IApplication[]) { - commit('setApplications', payload); - } - }, - getters: { - authenticated(state: ICommonState): boolean { - return !!state.token.access; - }, - user(state: ICommonState): IUser { - return state.user; - }, - token(state: ICommonState): IToken { - return state.token; - }, - setting(state: ICommonState): ISetting { - return state.setting; - }, - applications(state: ICommonState): IApplication[] | undefined { - return state.applications; - } - } -}; diff --git a/src/store/common/actions.ts b/src/store/common/actions.ts new file mode 100644 index 0000000..c1f86e7 --- /dev/null +++ b/src/store/common/actions.ts @@ -0,0 +1,26 @@ +import { ActionContext } from 'vuex'; +import { IRootState, IToken } from '../common/models'; +import { IApplication, IUser } from '@/operators'; + +export const resetToken = ({ commit }: ActionContext) => { + commit('resetToken'); +}; + +export const setToken = ({ commit }: ActionContext, payload: IToken) => { + commit('setToken', payload); +}; + +export const setUser = ({ commit }: ActionContext, payload: IUser) => { + commit('setUser', payload); +}; + +export const setApplications = ({ commit }: ActionContext, payload: IApplication[]) => { + commit('setApplications', payload); +}; + +export default { + resetToken, + setToken, + setUser, + setApplications +}; diff --git a/src/store/common/getters.ts b/src/store/common/getters.ts new file mode 100644 index 0000000..ab8ccde --- /dev/null +++ b/src/store/common/getters.ts @@ -0,0 +1,29 @@ +import { IRootState } from './models'; + +export const authenticated = (state: IRootState): boolean => { + return !!state.token.access; +}; + +export const user = (state: IRootState): any => { + return state.user; +}; + +export const token = (state: IRootState): any => { + return state.token; +}; + +export const setting = (state: IRootState): any => { + return state.setting; +}; + +export const applications = (state: IRootState): any => { + return state.applications; +}; + +export default { + authenticated, + user, + token, + setting, + applications +}; diff --git a/src/store/common/index.ts b/src/store/common/index.ts new file mode 100644 index 0000000..6a2f446 --- /dev/null +++ b/src/store/common/index.ts @@ -0,0 +1,11 @@ +import state from './state'; +import mutations from './mutations'; +import actions from './actions'; +import getters from './getters'; + +export default { + state, + mutations, + actions, + getters +}; diff --git a/src/store/common/models.ts b/src/store/common/models.ts new file mode 100644 index 0000000..aab78e2 --- /dev/null +++ b/src/store/common/models.ts @@ -0,0 +1,31 @@ +import { IApplication, IUser } from '@/operators'; +import { IMidjourneyState } from '../midjourney/models'; +import { IChatState } from '../chat/models'; + +export enum Status { + Request = 'Request', + Success = 'Success', + Error = 'Error' +} + +export interface IToken { + access?: string; + refresh?: string; +} + +export interface ISetting { + stream?: boolean; + endpoint?: string; +} + +export interface ICommonState { + token: IToken; + user: IUser; + setting: ISetting; + applications: IApplication[] | undefined; +} + +export interface IRootState extends ICommonState { + midjourney?: IMidjourneyState; + chat?: IChatState; +} diff --git a/src/store/common/mutations.ts b/src/store/common/mutations.ts new file mode 100644 index 0000000..ec1d0a2 --- /dev/null +++ b/src/store/common/mutations.ts @@ -0,0 +1,39 @@ +import { IUser } from '@/operators'; +import { IRootState } from './models'; + +export const setUser = (state: IRootState, payload: IUser): void => { + state.user = { + ...state.user, + ...payload + }; +}; + +export const setToken = (state: IRootState, payload: any): void => { + state.token = { + ...state.token, + ...payload + }; +}; + +export const resetToken = (state: IRootState): void => { + state.token = {}; +}; + +export const setSetting = (state: IRootState, payload: any): void => { + state.setting = { + ...state.setting, + ...payload + }; +}; + +export const setApplications = (state: IRootState, payload: any): void => { + state.applications = payload; +}; + +export default { + setUser, + setToken, + resetToken, + setSetting, + setApplications +}; diff --git a/src/store/common/state.ts b/src/store/common/state.ts new file mode 100644 index 0000000..efc9f16 --- /dev/null +++ b/src/store/common/state.ts @@ -0,0 +1,17 @@ +import { ENDPOINT } from '@/constants'; +import { IRootState } from './models'; + +export default (): IRootState => { + return { + user: {}, + token: { + access: undefined, + refresh: undefined + }, + setting: { + endpoint: ENDPOINT, + stream: false + }, + applications: undefined + }; +}; diff --git a/src/store/index.ts b/src/store/index.ts index 0357f3e..4809475 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,12 +1,12 @@ import { createStore, ActionContext } from 'vuex'; import createPersistedState from 'vuex-persistedstate'; -import { common } from './common'; -import { midjourney } from './midjourney'; -import { chat } from './chat'; +import midjourney from './midjourney'; +import chat from './chat'; +import root from './common'; const store = createStore({ + ...root, modules: { - common, midjourney, chat }, diff --git a/src/store/midjourney.ts b/src/store/midjourney.ts deleted file mode 100644 index 18c142d..0000000 --- a/src/store/midjourney.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { IApplication, IMidjourneyPreset } from '@/operators'; -import { Module } from 'vuex'; -import { IMidjourneyState } from './models'; - -export const midjourney: Module = { - namespaced: true, - state(): IMidjourneyState { - return { - applications: undefined, - preset: {} - }; - }, - mutations: { - setApplications(state: IMidjourneyState, payload: IApplication[]): void { - state.applications = payload; - }, - setPreset(state: IMidjourneyState, payload: IMidjourneyPreset): void { - state.preset = payload; - } - }, - actions: { - setPreset({ commit }: any, payload: IMidjourneyPreset) { - commit('setPreset', payload); - }, - setApplications({ commit }: any, payload: IApplication[]) { - commit('setApplications', payload); - } - } -}; diff --git a/src/store/midjourney/actions.ts b/src/store/midjourney/actions.ts new file mode 100644 index 0000000..4cf1e70 --- /dev/null +++ b/src/store/midjourney/actions.ts @@ -0,0 +1,14 @@ +import { IApplication, IMidjourneyPreset } from '@/operators'; + +export const setPreset = ({ commit }: any, payload: IMidjourneyPreset) => { + commit('setPreset', payload); +}; + +export const setApplications = ({ commit }: any, payload: IApplication[]) => { + commit('setApplications', payload); +}; + +export default { + setPreset, + setApplications +}; diff --git a/src/store/midjourney/index.ts b/src/store/midjourney/index.ts new file mode 100644 index 0000000..b4988df --- /dev/null +++ b/src/store/midjourney/index.ts @@ -0,0 +1,18 @@ +import { Module } from 'vuex'; +import { IMidjourneyState } from './models'; +import actions from './actions'; +import mutations from './mutations'; + +export const midjourney: Module = { + namespaced: true, + state(): IMidjourneyState { + return { + applications: undefined, + preset: {} + }; + }, + mutations, + actions +}; + +export default midjourney; diff --git a/src/store/midjourney/models.ts b/src/store/midjourney/models.ts new file mode 100644 index 0000000..bb6fcc5 --- /dev/null +++ b/src/store/midjourney/models.ts @@ -0,0 +1,6 @@ +import { IApplication, IMidjourneyPreset } from '@/operators'; + +export interface IMidjourneyState { + applications: IApplication[] | undefined; + preset: IMidjourneyPreset; +} diff --git a/src/store/midjourney/mutations.ts b/src/store/midjourney/mutations.ts new file mode 100644 index 0000000..8bd645d --- /dev/null +++ b/src/store/midjourney/mutations.ts @@ -0,0 +1,15 @@ +import { IApplication } from '@/operators'; +import { IMidjourneyState } from './models'; + +export const setApplications = (state: IMidjourneyState, payload: IApplication[]): void => { + state.applications = payload; +}; + +export const setPreset = (state: IMidjourneyState, payload: any): void => { + state.preset = payload; +}; + +export default { + setApplications, + setPreset +}; diff --git a/src/store/models.ts b/src/store/models.ts deleted file mode 100644 index 0d97047..0000000 --- a/src/store/models.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { IApplication, IMidjourneyPreset, IUser } from '@/operators'; - -export enum Status { - Request = 'Request', - Success = 'Success', - Error = 'Error' -} - -export interface IToken { - access?: string; - refresh?: string; -} - -export interface ISetting { - stream?: boolean; - endpoint?: string; -} - -export interface ICommonState { - token: IToken; - user: IUser; - setting: ISetting; - applications: IApplication[] | undefined; -} - -export interface IMidjourneyState { - applications: IApplication[] | undefined; - preset: IMidjourneyPreset; -} - -export interface IChatState { - applications: IApplication[] | undefined; - applicationsStatus: Status | undefined; -} - -export interface IRootState { - common: ICommonState; - midjourney: IMidjourneyState; - chat: IChatState; -}