chore: update redux store and service

This commit is contained in:
boojack
2022-05-22 11:01:20 +08:00
parent c3c2882dc5
commit a580df5c9f
27 changed files with 199 additions and 307 deletions

View File

@ -1,8 +1,6 @@
import userService from "./userService";
import api from "../helpers/api";
import { UNKNOWN_ID } from "../helpers/consts";
import store from "../store/";
import { deleteShortcut, patchShortcut, setShortcuts } from "../store/modules/shortcut";
import { createShortcut, deleteShortcut, patchShortcut, setShortcuts } from "../store/modules/shortcut";
const convertResponseModelShortcut = (shortcut: Shortcut): Shortcut => {
return {
@ -18,21 +16,12 @@ const shortcutService = {
},
getMyAllShortcuts: async () => {
if (!userService.getState().user) {
return false;
}
const data = await api.getMyShortcuts();
const shortcuts = data.map((s) => convertResponseModelShortcut(s));
const rawData = await api.getMyShortcuts();
const shortcuts = rawData.map((s) => convertResponseModelShortcut(s));
store.dispatch(setShortcuts(shortcuts));
return shortcuts;
},
getShortcutById: (id: ShortcutId) => {
if (id === UNKNOWN_ID) {
return null;
}
for (const s of shortcutService.getState().shortcuts) {
if (s.id === id) {
return s;
@ -42,11 +31,15 @@ const shortcutService = {
return null;
},
pushShortcut: (shortcut: Shortcut) => {
store.dispatch(setShortcuts(shortcutService.getState().shortcuts.concat(shortcut)));
createShortcut: async (shortcutCreate: ShortcutCreate) => {
const data = await api.createShortcut(shortcutCreate);
const shortcut = convertResponseModelShortcut(data);
store.dispatch(createShortcut(shortcut));
},
editShortcut: (shortcut: Shortcut) => {
patchShortcut: async (shortcutPatch: ShortcutPatch) => {
const data = await api.patchShortcut(shortcutPatch);
const shortcut = convertResponseModelShortcut(data);
store.dispatch(patchShortcut(shortcut));
},
@ -54,24 +47,6 @@ const shortcutService = {
await api.deleteShortcutById(shortcutId);
store.dispatch(deleteShortcut(shortcutId));
},
createShortcut: async (title: string, payload: string) => {
const data = await api.createShortcut(title, payload);
shortcutService.pushShortcut(convertResponseModelShortcut(data));
},
updateShortcut: async (shortcutId: ShortcutId, title: string, payload: string) => {
const data = await api.updateShortcut(shortcutId, title, payload);
store.dispatch(patchShortcut(convertResponseModelShortcut(data)));
},
pinShortcut: async (shortcutId: ShortcutId) => {
await api.pinShortcut(shortcutId);
},
unpinShortcut: async (shortcutId: ShortcutId) => {
await api.unpinShortcut(shortcutId);
},
};
export default shortcutService;