Add pika (#167)
This commit is contained in:
1 parent
ff0f3002d3
commit
5ebea7ef0c
160 files changed
+10841
-3375
No files matched your search
@@ -0,0 +1,160 @@
|
||||
import { applicationOperator, pikaOperator, serviceOperator } from '@/operators';
|
||||
import { IPikaState } from './models';
|
||||
import { ActionContext } from 'vuex';
|
||||
import { IRootState } from '../common/models';
|
||||
import { IApplication, ICredential, IPikaConfig, IPikaTask, IService, IApplicationType } from '@/models';
|
||||
import { Status } from '@/models/common';
|
||||
import { PIKA_SERVICE_ID } from '@/constants';
|
||||
|
||||
export const resetAll = ({ commit }: ActionContext<IPikaState, 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: IPikaConfig) => {
|
||||
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<IPikaState, IRootState>): Promise<IApplication[]> => {
|
||||
console.debug('start to get applications for pika');
|
||||
return new Promise((resolve, reject) => {
|
||||
state.status.getApplications = Status.Request;
|
||||
applicationOperator
|
||||
.getAll({
|
||||
user_id: rootState?.user?.id,
|
||||
service_id: PIKA_SERVICE_ID
|
||||
})
|
||||
.then((response) => {
|
||||
console.debug('get applications success', response?.data);
|
||||
state.status.getApplications = Status.Success;
|
||||
// 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);
|
||||
const credential = application?.credentials?.find(
|
||||
(credential) => credential?.host === window.location.origin
|
||||
);
|
||||
console.debug('set credential with Period', application);
|
||||
commit('setCredential', credential);
|
||||
} else if (application2) {
|
||||
console.debug('set application with Usage', application2);
|
||||
commit('setApplication', application2);
|
||||
const credential = application2?.credentials?.find(
|
||||
(credential) => credential?.host === window.location.origin
|
||||
);
|
||||
console.debug('set credential with Usage', application);
|
||||
commit('setCredential', credential);
|
||||
} else {
|
||||
console.debug('set application with null', response.data.items?.[0]);
|
||||
commit('setApplication', response.data.items?.[0]);
|
||||
}
|
||||
resolve(response.data.items);
|
||||
console.debug('save application success', response.data.items[0]);
|
||||
})
|
||||
.catch((error) => {
|
||||
state.status.getApplications = Status.Error;
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const setTasks = ({ commit }: any, payload: any) => {
|
||||
commit('setTasks', payload);
|
||||
};
|
||||
|
||||
export const setTasksItems = ({ commit }: any, payload: IPikaTask[]) => {
|
||||
commit('setTasksItems', payload);
|
||||
};
|
||||
|
||||
export const setTasksTotal = ({ commit }: any, payload: number) => {
|
||||
commit('setTasksTotal', payload);
|
||||
};
|
||||
|
||||
export const setTasksActive = ({ commit }: any, payload: IPikaTask) => {
|
||||
commit('setTasksActive', payload);
|
||||
};
|
||||
|
||||
export const getService = async ({ commit, state }: ActionContext<IPikaState, IRootState>): Promise<IService> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.debug('start to get service for pika');
|
||||
state.status.getService = Status.Request;
|
||||
serviceOperator
|
||||
.get(PIKA_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, rootState }: ActionContext<IPikaState, IRootState>,
|
||||
{ offset, limit }: { offset?: number; limit?: number }
|
||||
): Promise<IPikaTask[]> => {
|
||||
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');
|
||||
}
|
||||
pikaOperator
|
||||
.tasks(
|
||||
{
|
||||
userId: rootState?.user?.id,
|
||||
type: 'videos'
|
||||
},
|
||||
{
|
||||
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
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Module } from 'vuex';
|
||||
import { IPikaState } from './models';
|
||||
import actions from './actions';
|
||||
import mutations from './mutations';
|
||||
import state from './state';
|
||||
|
||||
export const pika: Module<IPikaState, any> = {
|
||||
namespaced: true,
|
||||
state,
|
||||
mutations,
|
||||
actions
|
||||
};
|
||||
|
||||
export default pika;
|
||||
@@ -0,0 +1,22 @@
|
||||
import { IApplication, ICredential, IService, Status } from '@/models';
|
||||
import { IPikaConfig, IPikaTask } from '@/models';
|
||||
|
||||
export interface IPikaState {
|
||||
application: IApplication | undefined;
|
||||
applications: IApplication[] | undefined;
|
||||
service: IService | undefined;
|
||||
credential: ICredential | undefined;
|
||||
config: IPikaConfig | undefined;
|
||||
tasks:
|
||||
| {
|
||||
items: IPikaTask[] | undefined;
|
||||
total: number | undefined;
|
||||
active: IPikaTask | undefined;
|
||||
}
|
||||
| undefined;
|
||||
status: {
|
||||
getService: Status;
|
||||
getApplications: Status;
|
||||
getTasks: Status;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import { IApplication, ICredential, IPikaConfig, IPikaTask, IService } from '@/models';
|
||||
import { IPikaState } from './models';
|
||||
|
||||
export const resetAll = (state: IPikaState): void => {
|
||||
state.service = undefined;
|
||||
state.application = undefined;
|
||||
state.config = undefined;
|
||||
state.credential = undefined;
|
||||
state.tasks = undefined;
|
||||
};
|
||||
|
||||
export const setService = (state: IPikaState, payload: IService): void => {
|
||||
state.service = payload;
|
||||
};
|
||||
|
||||
export const setCredential = (state: IPikaState, payload: ICredential): void => {
|
||||
state.credential = payload;
|
||||
};
|
||||
|
||||
export const setApplication = (state: IPikaState, payload: IApplication): void => {
|
||||
state.application = payload;
|
||||
};
|
||||
|
||||
export const setApplications = (state: IPikaState, payload: IApplication[]): void => {
|
||||
state.applications = payload;
|
||||
};
|
||||
|
||||
export const setConfig = (state: IPikaState, payload: IPikaConfig): void => {
|
||||
state.config = payload;
|
||||
};
|
||||
|
||||
export const setTasksItems = (state: IPikaState, payload: IPikaTask[]): void => {
|
||||
const newPayload = {
|
||||
...state.tasks,
|
||||
items: payload
|
||||
} as typeof state.tasks;
|
||||
state.tasks = newPayload;
|
||||
};
|
||||
|
||||
export const setTasksTotal = (state: IPikaState, payload: number): void => {
|
||||
const newPayload = {
|
||||
...state.tasks,
|
||||
total: payload
|
||||
} as typeof state.tasks;
|
||||
state.tasks = newPayload;
|
||||
};
|
||||
|
||||
export const setTasksActive = (state: IPikaState, payload: IPikaTask): void => {
|
||||
const newPayload = {
|
||||
...state.tasks,
|
||||
active: payload
|
||||
} as typeof state.tasks;
|
||||
state.tasks = newPayload;
|
||||
};
|
||||
|
||||
export const setTasks = (state: IPikaState, payload: any): void => {
|
||||
state.tasks = payload;
|
||||
};
|
||||
|
||||
export default {
|
||||
setTasks,
|
||||
setApplication,
|
||||
setApplications,
|
||||
setConfig,
|
||||
setCredential,
|
||||
setService,
|
||||
setTasksActive,
|
||||
setTasksItems,
|
||||
setTasksTotal,
|
||||
resetAll
|
||||
};
|
||||
@@ -0,0 +1 @@
|
||||
export default ['pika.credential', 'pika.application', 'pika.tasks'];
|
||||
@@ -0,0 +1,18 @@
|
||||
import { IPikaState } from './models';
|
||||
import { Status } from '@/models';
|
||||
|
||||
export default (): IPikaState => {
|
||||
return {
|
||||
service: undefined,
|
||||
application: undefined,
|
||||
applications: undefined,
|
||||
tasks: undefined,
|
||||
credential: undefined,
|
||||
config: undefined,
|
||||
status: {
|
||||
getService: Status.None,
|
||||
getApplications: Status.None,
|
||||
getTasks: Status.None
|
||||
}
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user