finish refactor of mj
This commit is contained in:
1 parent
6ab318e3f0
commit
a75a000974
14 files changed
+232
-196
No files matched your search
@@ -1,4 +1,18 @@
|
||||
import { IApplication, IMidjourneyPreset } from '@/operators';
|
||||
import {
|
||||
IApplication,
|
||||
IMidjourneyImagineTask,
|
||||
IMidjourneyPreset,
|
||||
MIDJOURNEY_CHANNEL_FAST,
|
||||
MIDJOURNEY_CHANNEL_RELAX,
|
||||
MIDJOURNEY_CHANNEL_TURBO,
|
||||
apiUsageOperator,
|
||||
applicationOperator,
|
||||
midjourneyOperator
|
||||
} from '@/operators';
|
||||
import { IMidjourneyState } from './models';
|
||||
import { ActionContext } from 'vuex';
|
||||
import { log } from '@/utils/log';
|
||||
import { IRootState, Status } from '../common/models';
|
||||
|
||||
export const setPreset = ({ commit }: any, payload: IMidjourneyPreset) => {
|
||||
commit('setPreset', payload);
|
||||
@@ -8,7 +22,61 @@ export const setApplications = ({ commit }: any, payload: IApplication[]) => {
|
||||
commit('setApplications', payload);
|
||||
};
|
||||
|
||||
export const getApplications = async ({
|
||||
commit,
|
||||
rootState
|
||||
}: ActionContext<IMidjourneyState, IRootState>): Promise<IApplication[]> => {
|
||||
log(getApplications, 'start to get application for midjourney');
|
||||
commit('setGetApplicationsStatus', Status.Request);
|
||||
const { data: applications } = await applicationOperator.getAll({
|
||||
user_id: rootState.user.id,
|
||||
api_id: [MIDJOURNEY_CHANNEL_FAST.apiId, MIDJOURNEY_CHANNEL_RELAX.apiId, MIDJOURNEY_CHANNEL_TURBO.apiId]
|
||||
});
|
||||
log(getApplications, 'get application for midjourney success', applications);
|
||||
commit('setGetApplicationsStatus', Status.Success);
|
||||
commit('setApplications', applications?.items);
|
||||
return applications.items;
|
||||
};
|
||||
|
||||
export const setImagineTasks = ({ commit }: any, payload: IMidjourneyImagineTask[]) => {
|
||||
commit('setImagineTasks', payload);
|
||||
};
|
||||
|
||||
export const getImagineTasks = async (
|
||||
{ commit, state, rootState }: ActionContext<IMidjourneyState, IRootState>,
|
||||
{ offset, limit }: { offset?: number; limit?: number }
|
||||
): Promise<IMidjourneyImagineTask[]> => {
|
||||
log(getImagineTasks, 'start to get imagine tasks', offset, limit);
|
||||
commit('setGetImagineTasksStatus', Status.Request);
|
||||
const {
|
||||
data: { items: apiUsages, count }
|
||||
} = await apiUsageOperator.getAll({
|
||||
user_id: rootState.user.id,
|
||||
// @ts-ignore
|
||||
application_id: state.applications?.map((application: IApplication) => application.id),
|
||||
offset,
|
||||
limit,
|
||||
ordering: '-created_at'
|
||||
});
|
||||
log(getImagineTasks, 'get imagine api usage success', apiUsages);
|
||||
let tasks = (await midjourneyOperator.tasks(apiUsages.map((apiUsage) => apiUsage.metadata?.task_id as string))).data;
|
||||
tasks = tasks.map((task: IMidjourneyImagineTask) => {
|
||||
const apiUsage = apiUsages.filter((apiUsage) => apiUsage.metadata?.task_id === task?.id)[0];
|
||||
return {
|
||||
...task,
|
||||
created_at: apiUsage.created_at
|
||||
};
|
||||
});
|
||||
commit('setImagineTasksTotal', count);
|
||||
commit('setGetImagineTasksStatus', Status.Success);
|
||||
commit('setImagineTasks', tasks);
|
||||
return tasks;
|
||||
};
|
||||
|
||||
export default {
|
||||
setPreset,
|
||||
setApplications
|
||||
setApplications,
|
||||
getApplications,
|
||||
setImagineTasks,
|
||||
getImagineTasks
|
||||
};
|
||||
@@ -2,15 +2,11 @@ import { Module } from 'vuex';
|
||||
import { IMidjourneyState } from './models';
|
||||
import actions from './actions';
|
||||
import mutations from './mutations';
|
||||
import state from './state';
|
||||
|
||||
export const midjourney: Module<IMidjourneyState, any> = {
|
||||
namespaced: true,
|
||||
state(): IMidjourneyState {
|
||||
return {
|
||||
applications: undefined,
|
||||
preset: {}
|
||||
};
|
||||
},
|
||||
state,
|
||||
mutations,
|
||||
actions
|
||||
};
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import { IApplication, IMidjourneyPreset } from '@/operators';
|
||||
import { IApplication, IMidjourneyImagineTask, IMidjourneyPreset } from '@/operators';
|
||||
import { Status } from '../common/models';
|
||||
|
||||
export interface IMidjourneyState {
|
||||
applications: IApplication[] | undefined;
|
||||
getApplicationsStatus: Status | undefined;
|
||||
imagineTasks: IMidjourneyImagineTask[] | undefined;
|
||||
getImagineTasksStatus: Status | undefined;
|
||||
imagineTasksTotal: number | undefined;
|
||||
preset: IMidjourneyPreset;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { IApplication } from '@/operators';
|
||||
import { IMidjourneyState } from './models';
|
||||
import { Status } from '../common/models';
|
||||
|
||||
export const setApplications = (state: IMidjourneyState, payload: IApplication[]): void => {
|
||||
state.applications = payload;
|
||||
@@ -9,7 +10,27 @@ export const setPreset = (state: IMidjourneyState, payload: any): void => {
|
||||
state.preset = payload;
|
||||
};
|
||||
|
||||
export const setGetApplicationsStatus = (state: IMidjourneyState, payload: Status): void => {
|
||||
state.getApplicationsStatus = payload;
|
||||
};
|
||||
|
||||
export const setImagineTasks = (state: IMidjourneyState, payload: any): void => {
|
||||
state.imagineTasks = payload;
|
||||
};
|
||||
|
||||
export const setGetImagineTasksStatus = (state: IMidjourneyState, payload: Status): void => {
|
||||
state.getImagineTasksStatus = payload;
|
||||
};
|
||||
|
||||
export const setImagineTasksTotal = (state: IMidjourneyState, payload: number): void => {
|
||||
state.imagineTasksTotal = payload;
|
||||
};
|
||||
|
||||
export default {
|
||||
setApplications,
|
||||
setPreset
|
||||
setPreset,
|
||||
setGetApplicationsStatus,
|
||||
setImagineTasks,
|
||||
setGetImagineTasksStatus,
|
||||
setImagineTasksTotal
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
import { IMidjourneyState } from './models';
|
||||
|
||||
export default (): IMidjourneyState => {
|
||||
return {
|
||||
applications: undefined,
|
||||
getApplicationsStatus: undefined,
|
||||
imagineTasks: undefined,
|
||||
getImagineTasksStatus: undefined,
|
||||
imagineTasksTotal: undefined,
|
||||
preset: {}
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user