refactor vuex

This commit is contained in:
AceDataCloud committed 2023-12-30 13:34:45 +08:00
1 parent 9c69e96416
commit 565b367c59
20 files changed
+264 -178

No files matched your search

-26
View File
@@ -1,26 +0,0 @@
import { IApplication } from '@/operators';
import { Module } from 'vuex';
import { IChatState, Status } from './models';
export const chat: Module<IChatState, any> = {
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);
}
}
};
+9
View File
@@ -0,0 +1,9 @@
import { IApplication } from '@/operators';
export const setApplications = async ({ commit }: any, payload: IApplication[]) => {
commit('setApplications', payload);
};
export default {
setApplications
};
+14
View File
@@ -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<IChatState, any> = {
namespaced: true,
state,
mutations,
actions
};
export default chat;
+7
View File
@@ -0,0 +1,7 @@
import { IApplication } from '@/operators';
import { Status } from '../common/models';
export interface IChatState {
applications: IApplication[] | undefined;
applicationsStatus: Status | undefined;
}
+16
View File
@@ -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
};
+8
View File
@@ -0,0 +1,8 @@
import { IChatState } from './models';
export default (): IChatState => {
return {
applications: undefined,
applicationsStatus: undefined
};
};
-79
View File
@@ -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<ICommonState, IRootState> = {
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<ICommonState, IRootState>) {
commit('resetToken');
},
setToken({ commit }: ActionContext<ICommonState, IRootState>, payload: IToken) {
commit('setToken', payload);
},
setUser({ commit }: ActionContext<ICommonState, IRootState>, payload: IUser) {
commit('setUser', payload);
},
setApplications({ commit }: ActionContext<ICommonState, IRootState>, 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;
}
}
};
+26
View File
@@ -0,0 +1,26 @@
import { ActionContext } from 'vuex';
import { IRootState, IToken } from '../common/models';
import { IApplication, IUser } from '@/operators';
export const resetToken = ({ commit }: ActionContext<IRootState, IRootState>) => {
commit('resetToken');
};
export const setToken = ({ commit }: ActionContext<IRootState, IRootState>, payload: IToken) => {
commit('setToken', payload);
};
export const setUser = ({ commit }: ActionContext<IRootState, IRootState>, payload: IUser) => {
commit('setUser', payload);
};
export const setApplications = ({ commit }: ActionContext<IRootState, IRootState>, payload: IApplication[]) => {
commit('setApplications', payload);
};
export default {
resetToken,
setToken,
setUser,
setApplications
};
+29
View File
@@ -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
};
+11
View File
@@ -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
};
+31
View File
@@ -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;
}
+39
View File
@@ -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
};
+17
View File
@@ -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
};
};
+4 -4
View File
@@ -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
},
-29
View File
@@ -1,29 +0,0 @@
import { IApplication, IMidjourneyPreset } from '@/operators';
import { Module } from 'vuex';
import { IMidjourneyState } from './models';
export const midjourney: Module<IMidjourneyState, any> = {
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);
}
}
};
+14
View File
@@ -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
};
+18
View File
@@ -0,0 +1,18 @@
import { Module } from 'vuex';
import { IMidjourneyState } from './models';
import actions from './actions';
import mutations from './mutations';
export const midjourney: Module<IMidjourneyState, any> = {
namespaced: true,
state(): IMidjourneyState {
return {
applications: undefined,
preset: {}
};
},
mutations,
actions
};
export default midjourney;
+6
View File
@@ -0,0 +1,6 @@
import { IApplication, IMidjourneyPreset } from '@/operators';
export interface IMidjourneyState {
applications: IApplication[] | undefined;
preset: IMidjourneyPreset;
}
+15
View File
@@ -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
};
-40
View File
@@ -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;
}