Skip to content

Commit

Permalink
refactor vuex
Browse files Browse the repository at this point in the history
  • Loading branch information
Germey committed Dec 30, 2023
1 parent 16dae46 commit d28a812
Show file tree
Hide file tree
Showing 20 changed files with 264 additions and 178 deletions.
26 changes: 0 additions & 26 deletions src/store/chat.ts

This file was deleted.

9 changes: 9 additions & 0 deletions src/store/chat/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IApplication } from '@/operators';

export const setApplications = async ({ commit }: any, payload: IApplication[]) => {
commit('setApplications', payload);
};

export default {
setApplications
};
14 changes: 14 additions & 0 deletions src/store/chat/index.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 0 deletions src/store/chat/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IApplication } from '@/operators';
import { Status } from '../common/models';

export interface IChatState {
applications: IApplication[] | undefined;
applicationsStatus: Status | undefined;
}
16 changes: 16 additions & 0 deletions src/store/chat/mutations.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions src/store/chat/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IChatState } from './models';

export default (): IChatState => {
return {
applications: undefined,
applicationsStatus: undefined
};
};
79 changes: 0 additions & 79 deletions src/store/common.ts

This file was deleted.

26 changes: 26 additions & 0 deletions src/store/common/actions.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 29 additions & 0 deletions src/store/common/getters.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions src/store/common/index.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 31 additions & 0 deletions src/store/common/models.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 39 additions & 0 deletions src/store/common/mutations.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions src/store/common/state.ts
Original file line number Diff line number Diff line change
@@ -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
};
};
8 changes: 4 additions & 4 deletions src/store/index.ts
Original file line number Diff line number Diff line change
@@ -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
},
Expand Down
29 changes: 0 additions & 29 deletions src/store/midjourney.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/store/midjourney/actions.ts
Original file line number Diff line number Diff line change
@@ -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
};
Loading

0 comments on commit d28a812

Please sign in to comment.