mirror of
https://github.com/aykhans/slash-e.git
synced 2025-04-20 22:07:15 +00:00
92 lines
2.6 KiB
TypeScript
92 lines
2.6 KiB
TypeScript
import * as api from "../helpers/api";
|
|
import store from "../store";
|
|
import { createWorkspace, deleteWorkspace, patchWorkspace, setWorkspaceById, setWorkspaceList } from "../store/modules/workspace";
|
|
|
|
const convertResponseModelWorkspace = (workspace: Workspace): Workspace => {
|
|
return {
|
|
...workspace,
|
|
createdTs: workspace.createdTs * 1000,
|
|
updatedTs: workspace.updatedTs * 1000,
|
|
};
|
|
};
|
|
|
|
const workspaceService = {
|
|
getState: () => {
|
|
return store.getState().workspace;
|
|
},
|
|
|
|
fetchWorkspaceList: async () => {
|
|
const { data } = (await api.getWorkspaceList()).data;
|
|
const workspaces = data.map((w) => convertResponseModelWorkspace(w));
|
|
store.dispatch(setWorkspaceList(workspaces));
|
|
return workspaces;
|
|
},
|
|
|
|
fetchWorkspaceById: async (workspaceId: WorkspaceId) => {
|
|
const { data } = (await api.getWorkspaceById(workspaceId)).data;
|
|
const workspace = convertResponseModelWorkspace(data);
|
|
store.dispatch(setWorkspaceById(workspace));
|
|
return workspace;
|
|
},
|
|
|
|
getWorkspaceByName: (workspaceName: string) => {
|
|
const workspaceList = workspaceService.getState().workspaceList;
|
|
for (const workspace of workspaceList) {
|
|
if (workspace.name === workspaceName) {
|
|
return workspace;
|
|
}
|
|
}
|
|
return undefined;
|
|
},
|
|
|
|
getWorkspaceById: (id: WorkspaceId) => {
|
|
const workspaceList = workspaceService.getState().workspaceList;
|
|
for (const workspace of workspaceList) {
|
|
if (workspace.id === id) {
|
|
return workspace;
|
|
}
|
|
}
|
|
return undefined;
|
|
},
|
|
|
|
createWorkspace: async (create: WorkspaceCreate) => {
|
|
const { data } = (await api.createWorkspace(create)).data;
|
|
const workspace = convertResponseModelWorkspace(data);
|
|
store.dispatch(createWorkspace(workspace));
|
|
return workspace;
|
|
},
|
|
|
|
patchWorkspace: async (patch: WorkspacePatch) => {
|
|
const { data } = (await api.patchWorkspace(patch)).data;
|
|
const workspace = convertResponseModelWorkspace(data);
|
|
store.dispatch(patchWorkspace(workspace));
|
|
return workspace;
|
|
},
|
|
|
|
deleteWorkspaceById: async (id: WorkspaceId) => {
|
|
await api.deleteWorkspaceById(id);
|
|
store.dispatch(deleteWorkspace(id));
|
|
},
|
|
|
|
getWorkspaceUserList: async (id: WorkspaceId) => {
|
|
const { data } = (
|
|
await api.getWorkspaceUserList({
|
|
workspaceId: id,
|
|
})
|
|
).data;
|
|
return data;
|
|
},
|
|
|
|
getWorkspaceUser: async (workspaceId: WorkspaceId, userId: UserId) => {
|
|
const { data } = (
|
|
await api.getWorkspaceUser({
|
|
workspaceId: workspaceId,
|
|
userId: userId,
|
|
})
|
|
).data;
|
|
return data;
|
|
},
|
|
};
|
|
|
|
export default workspaceService;
|