mirror of
https://github.com/aykhans/slash-e.git
synced 2025-07-07 13:42:34 +00:00
chore: init web project
This commit is contained in:
6
web/src/services/README.md
Normal file
6
web/src/services/README.md
Normal file
@ -0,0 +1,6 @@
|
||||
# Services
|
||||
|
||||
What should service do?
|
||||
|
||||
- request data api and throw error;
|
||||
- dispatch state actions;
|
23
web/src/services/globalService.ts
Normal file
23
web/src/services/globalService.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import store from "../store";
|
||||
import { setGlobalState } from "../store/modules/global";
|
||||
import userService from "./userService";
|
||||
|
||||
const globalService = {
|
||||
getState: () => {
|
||||
return store.getState().global;
|
||||
},
|
||||
|
||||
initialState: async () => {
|
||||
const defaultGlobalState = {};
|
||||
|
||||
try {
|
||||
await userService.initialState();
|
||||
} catch (error) {
|
||||
// do nth
|
||||
}
|
||||
|
||||
store.dispatch(setGlobalState(defaultGlobalState));
|
||||
},
|
||||
};
|
||||
|
||||
export default globalService;
|
6
web/src/services/index.ts
Normal file
6
web/src/services/index.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import globalService from "./globalService";
|
||||
import shortcutService from "./shortcutService";
|
||||
import userService from "./userService";
|
||||
import workspaceService from "./workspaceService";
|
||||
|
||||
export { globalService, shortcutService, userService, workspaceService };
|
63
web/src/services/shortcutService.ts
Normal file
63
web/src/services/shortcutService.ts
Normal file
@ -0,0 +1,63 @@
|
||||
import * as api from "../helpers/api";
|
||||
import store from "../store/";
|
||||
import { createShortcut, deleteShortcut, patchShortcut, setShortcuts } from "../store/modules/shortcut";
|
||||
|
||||
const convertResponseModelShortcut = (shortcut: Shortcut): Shortcut => {
|
||||
return {
|
||||
...shortcut,
|
||||
createdTs: shortcut.createdTs * 1000,
|
||||
updatedTs: shortcut.updatedTs * 1000,
|
||||
};
|
||||
};
|
||||
|
||||
const shortcutService = {
|
||||
getState: () => {
|
||||
return store.getState().shortcut;
|
||||
},
|
||||
|
||||
fetchWorkspaceShortcuts: async (workspaceId: WorkspaceId) => {
|
||||
const { data } = (
|
||||
await api.getShortcutList({
|
||||
workspaceId,
|
||||
})
|
||||
).data;
|
||||
const shortcuts = data.map((s) => convertResponseModelShortcut(s));
|
||||
store.dispatch(setShortcuts(shortcuts));
|
||||
return shortcuts;
|
||||
},
|
||||
|
||||
getMyAllShortcuts: async () => {
|
||||
const { data } = (await api.getShortcutList()).data;
|
||||
const shortcuts = data.map((s) => convertResponseModelShortcut(s));
|
||||
store.dispatch(setShortcuts(shortcuts));
|
||||
},
|
||||
|
||||
getShortcutById: (id: ShortcutId) => {
|
||||
for (const s of shortcutService.getState().shortcutList) {
|
||||
if (s.id === id) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
|
||||
createShortcut: async (shortcutCreate: ShortcutCreate) => {
|
||||
const { data } = (await api.createShortcut(shortcutCreate)).data;
|
||||
const shortcut = convertResponseModelShortcut(data);
|
||||
store.dispatch(createShortcut(shortcut));
|
||||
},
|
||||
|
||||
patchShortcut: async (shortcutPatch: ShortcutPatch) => {
|
||||
const { data } = (await api.patchShortcut(shortcutPatch)).data;
|
||||
const shortcut = convertResponseModelShortcut(data);
|
||||
store.dispatch(patchShortcut(shortcut));
|
||||
},
|
||||
|
||||
deleteShortcutById: async (shortcutId: ShortcutId) => {
|
||||
await api.deleteShortcutById(shortcutId);
|
||||
store.dispatch(deleteShortcut(shortcutId));
|
||||
},
|
||||
};
|
||||
|
||||
export default shortcutService;
|
66
web/src/services/userService.ts
Normal file
66
web/src/services/userService.ts
Normal file
@ -0,0 +1,66 @@
|
||||
import * as api from "../helpers/api";
|
||||
import store from "../store";
|
||||
import { setUser, patchUser } from "../store/modules/user";
|
||||
|
||||
export const convertResponseModelUser = (user: User): User => {
|
||||
return {
|
||||
...user,
|
||||
createdTs: user.createdTs * 1000,
|
||||
updatedTs: user.updatedTs * 1000,
|
||||
};
|
||||
};
|
||||
|
||||
const userService = {
|
||||
getState: () => {
|
||||
return store.getState().user;
|
||||
},
|
||||
|
||||
initialState: async () => {
|
||||
try {
|
||||
const { data: user } = (await api.getMyselfUser()).data;
|
||||
if (user) {
|
||||
store.dispatch(setUser(convertResponseModelUser(user)));
|
||||
}
|
||||
} catch (error) {
|
||||
// do nth
|
||||
}
|
||||
},
|
||||
|
||||
doSignIn: async () => {
|
||||
const { data: user } = (await api.getMyselfUser()).data;
|
||||
if (user) {
|
||||
store.dispatch(setUser(convertResponseModelUser(user)));
|
||||
} else {
|
||||
userService.doSignOut();
|
||||
}
|
||||
return user;
|
||||
},
|
||||
|
||||
doSignOut: async () => {
|
||||
store.dispatch(setUser());
|
||||
await api.signout();
|
||||
},
|
||||
|
||||
getUserById: async (userId: UserId) => {
|
||||
const { data: user } = (await api.getUserById(userId)).data;
|
||||
if (user) {
|
||||
return convertResponseModelUser(user);
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
},
|
||||
|
||||
patchUser: async (userPatch: UserPatch): Promise<void> => {
|
||||
const { data } = (await api.patchUser(userPatch)).data;
|
||||
if (userPatch.id === store.getState().user.user?.id) {
|
||||
const user = convertResponseModelUser(data);
|
||||
store.dispatch(patchUser(user));
|
||||
}
|
||||
},
|
||||
|
||||
deleteUser: async (userDelete: UserDelete) => {
|
||||
await api.deleteUser(userDelete);
|
||||
},
|
||||
};
|
||||
|
||||
export default userService;
|
55
web/src/services/workspaceService.ts
Normal file
55
web/src/services/workspaceService.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import * as api from "../helpers/api";
|
||||
import store from "../store";
|
||||
import { createWorkspace, deleteWorkspace, patchWorkspace, 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;
|
||||
},
|
||||
|
||||
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));
|
||||
},
|
||||
};
|
||||
|
||||
export default workspaceService;
|
Reference in New Issue
Block a user