refactor: sync frontend

This commit is contained in:
email
2022-02-04 19:17:11 +08:00
parent 4535e0ce6d
commit 3fa918169e
34 changed files with 447 additions and 403 deletions

View File

@ -0,0 +1,93 @@
import userService from "./userService";
import api from "../helpers/api";
import appStore from "../stores/appStore";
import utils from "../helpers/utils";
class ShortcutService {
public getState() {
return appStore.getState().shortcutState;
}
public async getMyAllShortcuts() {
if (!userService.getState().user) {
return false;
}
const data = await api.getMyShortcuts();
appStore.dispatch({
type: "SET_SHORTCUTS",
payload: {
shortcuts: data.map((s) => this.convertResponseModelShortcut(s)),
},
});
return data;
}
public getShortcutById(id: string) {
for (const q of this.getState().shortcuts) {
if (q.id === id) {
return q;
}
}
return null;
}
public pushShortcut(shortcut: Model.Shortcut) {
appStore.dispatch({
type: "INSERT_SHORTCUT",
payload: {
shortcut: {
...shortcut,
},
},
});
}
public editShortcut(shortcut: Model.Shortcut) {
appStore.dispatch({
type: "UPDATE_SHORTCUT",
payload: shortcut,
});
}
public async deleteShortcut(shortcutId: string) {
await api.deleteShortcutById(shortcutId);
appStore.dispatch({
type: "DELETE_SHORTCUT_BY_ID",
payload: {
id: shortcutId,
},
});
}
public async createShortcut(title: string, shortcutstring: string) {
const data = await api.createShortcut(title, shortcutstring);
return data;
}
public async updateShortcut(shortcutId: string, title: string, shortcutstring: string) {
const data = await api.updateShortcut(shortcutId, title, shortcutstring);
return data;
}
public async pinShortcut(shortcutId: string) {
await api.pinShortcut(shortcutId);
}
public async unpinShortcut(shortcutId: string) {
await api.unpinShortcut(shortcutId);
}
public convertResponseModelShortcut(shortcut: Model.Shortcut): Model.Shortcut {
return {
...shortcut,
createdAt: utils.getDataStringWithTs(shortcut.createdTs),
updatedAt: utils.getDataStringWithTs(shortcut.updatedTs),
};
}
}
const shortcutService = new ShortcutService();
export default shortcutService;