Co-authored-by: Germey <cqc@cuiqingcai.com>
This commit is contained in:
HCodeandGermey authored and GitHub committed 2024-08-16 17:49:20 +00:00
1 parent 2e0665f09c
commit 6be9299532
42 files changed
+2342 -5

No files matched your search

+148
View File
@@ -0,0 +1,148 @@
import { applicationOperator, lumaOperator, serviceOperator } from '@/operators';
import { ILumaState } from './models';
import { ActionContext } from 'vuex';
import { IRootState } from '../common/models';
import { IApplication, ICredential, ILumaConfig, ILumaTask, IService, IApplicationType } from '@/models';
import { Status } from '@/models/common';
import { LUMA_SERVICE_ID } from '@/constants';
export const resetAll = ({ commit }: ActionContext<ILumaState, IRootState>): void => {
commit('resetAll');
};
export const setCredential = async ({ commit }: any, payload: ICredential): Promise<void> => {
console.debug('set credential', payload);
commit('setCredential', payload);
};
export const setConfig = ({ commit }: any, payload: ILumaConfig) => {
commit('setConfig', payload);
};
export const setService = async ({ commit }: any, payload: IService): Promise<void> => {
console.debug('set service', payload);
commit('setService', payload);
};
export const setApplication = ({ commit }: any, payload: IApplication[]) => {
commit('setApplication', payload);
};
export const getApplications = async ({
commit,
state,
rootState
}: ActionContext<ILumaState, IRootState>): Promise<IApplication[]> => {
console.debug('start to get applications for qrart');
return new Promise((resolve, reject) => {
state.status.getApplications = Status.Request;
applicationOperator
.getAll({
user_id: rootState?.user?.id,
service_id: LUMA_SERVICE_ID
})
.then((response) => {
console.debug('get applications success', response?.data);
state.status.getApplications = Status.Success;
commit('setApplications', response.data.items);
// check if there is any application with 'Period' type
const application = response.data.items?.find((application) => application?.type === IApplicationType.PERIOD);
const application2 = response.data.items?.find((application) => application?.type === IApplicationType.USAGE);
if (application && application?.remaining_amount) {
console.debug('set application with Period', application);
commit('setApplication', application);
} else if (application2) {
console.debug('set application with Usage', application2);
commit('setApplication', application2);
}
resolve(response.data.items);
console.debug('save applications success', response.data.items);
})
.catch((error) => {
console.error('get applications error', error);
state.status.getApplications = Status.Error;
reject(error);
});
});
};
export const setTasks = ({ commit }: any, payload: any) => {
commit('setTasks', payload);
};
export const setTasksItems = ({ commit }: any, payload: ILumaTask[]) => {
commit('setTasksItems', payload);
};
export const setTasksTotal = ({ commit }: any, payload: number) => {
commit('setTasksTotal', payload);
};
export const setTasksActive = ({ commit }: any, payload: ILumaTask) => {
commit('setTasksActive', payload);
};
export const getService = async ({ commit, state }: ActionContext<ILumaState, IRootState>): Promise<IService> => {
return new Promise((resolve, reject) => {
console.debug('start to get service for luma');
state.status.getService = Status.Request;
serviceOperator
.get(LUMA_SERVICE_ID)
.then((response) => {
state.status.getService = Status.Success;
commit('setService', response.data);
resolve(response.data);
})
.catch((error) => {
state.status.getService = Status.Error;
reject(error);
});
});
};
export const getTasks = async (
{ commit, state }: ActionContext<ILumaState, IRootState>,
{ offset, limit }: { offset?: number; limit?: number }
): Promise<ILumaTask[]> => {
return new Promise((resolve, reject) => {
console.debug('start to get tasks', offset, limit);
const credential = state.credential;
const token = credential?.token;
if (!token) {
return reject('no token');
}
lumaOperator
.tasks(
{
applicationId: state.application?.id
},
{
token
}
)
.then((response) => {
console.debug('get imagine tasks success', response.data.items);
commit('setTasksItems', response.data.items);
commit('setTasksTotal', response.data.count);
resolve(response.data.items);
})
.catch((error) => {
return reject(error);
});
});
};
export default {
setService,
getService,
resetAll,
setCredential,
setConfig,
setApplication,
getApplications,
setTasks,
setTasksItems,
setTasksTotal,
setTasksActive,
getTasks
};
+14
View File
@@ -0,0 +1,14 @@
import { Module } from 'vuex';
import { ILumaState } from './models';
import actions from './actions';
import mutations from './mutations';
import state from './state';
export const luma: Module<ILumaState, any> = {
namespaced: true,
state,
mutations,
actions
};
export default luma;
+21
View File
@@ -0,0 +1,21 @@
import { IApplication, ICredential, IService, Status } from '@/models';
import { ILumaConfig, ILumaTask } from '@/models';
export interface ILumaState {
application: IApplication | undefined;
service: IService | undefined;
credential: ICredential | undefined;
config: ILumaConfig | undefined;
tasks:
| {
items: ILumaTask[] | undefined;
total: number | undefined;
active: ILumaTask | undefined;
}
| undefined;
status: {
getService: Status;
getApplications: Status;
getTasks: Status;
};
}
+66
View File
@@ -0,0 +1,66 @@
import { IApplication, ICredential, ILumaConfig, ILumaTask, IService } from '@/models';
import { ILumaState } from './models';
export const resetAll = (state: ILumaState): void => {
state.service = undefined;
state.application = undefined;
state.config = undefined;
state.credential = undefined;
state.tasks = undefined;
};
export const setService = (state: ILumaState, payload: IService): void => {
state.service = payload;
};
export const setCredential = (state: ILumaState, payload: ICredential): void => {
state.credential = payload;
};
export const setApplication = (state: ILumaState, payload: IApplication): void => {
state.application = payload;
};
export const setConfig = (state: ILumaState, payload: ILumaConfig): void => {
state.config = payload;
};
export const setTasksItems = (state: ILumaState, payload: ILumaTask[]): void => {
const newPayload = {
...state.tasks,
items: payload
} as typeof state.tasks;
state.tasks = newPayload;
};
export const setTasksTotal = (state: ILumaState, payload: number): void => {
const newPayload = {
...state.tasks,
total: payload
} as typeof state.tasks;
state.tasks = newPayload;
};
export const setTasksActive = (state: ILumaState, payload: ILumaTask): void => {
const newPayload = {
...state.tasks,
active: payload
} as typeof state.tasks;
state.tasks = newPayload;
};
export const setTasks = (state: ILumaState, payload: any): void => {
state.tasks = payload;
};
export default {
setTasks,
setApplication,
setConfig,
setCredential,
setService,
setTasksActive,
setTasksItems,
setTasksTotal,
resetAll
};
+1
View File
@@ -0,0 +1 @@
export default ['luma.config', 'luma.credential', 'luma.application', 'luma.tasks'];
+17
View File
@@ -0,0 +1,17 @@
import { ILumaState } from './models';
import { Status } from '@/models';
export default (): ILumaState => {
return {
service: undefined,
application: undefined,
tasks: undefined,
credential: undefined,
config: undefined,
status: {
getService: Status.None,
getApplications: Status.None,
getTasks: Status.None
}
};
};