mirror of https://github.com/usememos/memos
refactor: sync frontend
parent
4535e0ce6d
commit
3fa918169e
@ -1,8 +1,8 @@
|
|||||||
import globalStateService from "./globalStateService";
|
import globalStateService from "./globalStateService";
|
||||||
import locationService from "./locationService";
|
import locationService from "./locationService";
|
||||||
import memoService from "./memoService";
|
import memoService from "./memoService";
|
||||||
import queryService from "./queryService";
|
import shortcutService from "./shortcutService";
|
||||||
import userService from "./userService";
|
import userService from "./userService";
|
||||||
import resourceService from "./resourceService";
|
import resourceService from "./resourceService";
|
||||||
|
|
||||||
export { globalStateService, locationService, memoService, queryService, userService, resourceService };
|
export { globalStateService, locationService, memoService, shortcutService, userService, resourceService };
|
||||||
|
@ -1,82 +0,0 @@
|
|||||||
import userService from "./userService";
|
|
||||||
import api from "../helpers/api";
|
|
||||||
import appStore from "../stores/appStore";
|
|
||||||
|
|
||||||
class QueryService {
|
|
||||||
public getState() {
|
|
||||||
return appStore.getState().queryState;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async getMyAllQueries() {
|
|
||||||
if (!userService.getState().user) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { data } = await api.getMyQueries();
|
|
||||||
appStore.dispatch({
|
|
||||||
type: "SET_QUERIES",
|
|
||||||
payload: {
|
|
||||||
queries: data,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
public getQueryById(id: string) {
|
|
||||||
for (const q of this.getState().queries) {
|
|
||||||
if (q.id === id) {
|
|
||||||
return q;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public pushQuery(query: Model.Query) {
|
|
||||||
appStore.dispatch({
|
|
||||||
type: "INSERT_QUERY",
|
|
||||||
payload: {
|
|
||||||
query: {
|
|
||||||
...query,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public editQuery(query: Model.Query) {
|
|
||||||
appStore.dispatch({
|
|
||||||
type: "UPDATE_QUERY",
|
|
||||||
payload: query,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public async deleteQuery(queryId: string) {
|
|
||||||
await api.deleteQueryById(queryId);
|
|
||||||
appStore.dispatch({
|
|
||||||
type: "DELETE_QUERY_BY_ID",
|
|
||||||
payload: {
|
|
||||||
id: queryId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public async createQuery(title: string, querystring: string) {
|
|
||||||
const { data } = await api.createQuery(title, querystring);
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async updateQuery(queryId: string, title: string, querystring: string) {
|
|
||||||
const { data } = await api.updateQuery(queryId, title, querystring);
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async pinQuery(queryId: string) {
|
|
||||||
await api.pinQuery(queryId);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async unpinQuery(queryId: string) {
|
|
||||||
await api.unpinQuery(queryId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const queryService = new QueryService();
|
|
||||||
|
|
||||||
export default queryService;
|
|
@ -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;
|
@ -1,92 +0,0 @@
|
|||||||
import utils from "../helpers/utils";
|
|
||||||
|
|
||||||
export interface State {
|
|
||||||
queries: Model.Query[];
|
|
||||||
}
|
|
||||||
|
|
||||||
interface SetQueries {
|
|
||||||
type: "SET_QUERIES";
|
|
||||||
payload: {
|
|
||||||
queries: Model.Query[];
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
interface InsertQueryAction {
|
|
||||||
type: "INSERT_QUERY";
|
|
||||||
payload: {
|
|
||||||
query: Model.Query;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
interface DeleteQueryByIdAction {
|
|
||||||
type: "DELETE_QUERY_BY_ID";
|
|
||||||
payload: {
|
|
||||||
id: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
interface UpdateQueryAction {
|
|
||||||
type: "UPDATE_QUERY";
|
|
||||||
payload: Model.Query;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Actions = SetQueries | InsertQueryAction | DeleteQueryByIdAction | UpdateQueryAction;
|
|
||||||
|
|
||||||
export function reducer(state: State, action: Actions): State {
|
|
||||||
switch (action.type) {
|
|
||||||
case "SET_QUERIES": {
|
|
||||||
const queries = utils.dedupeObjectWithId(
|
|
||||||
action.payload.queries
|
|
||||||
.sort((a, b) => utils.getTimeStampByDate(b.createdAt) - utils.getTimeStampByDate(a.createdAt))
|
|
||||||
.sort((a, b) => utils.getTimeStampByDate(b.pinnedAt ?? 0) - utils.getTimeStampByDate(a.pinnedAt ?? 0))
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...state,
|
|
||||||
queries,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
case "INSERT_QUERY": {
|
|
||||||
const queries = utils.dedupeObjectWithId(
|
|
||||||
[action.payload.query, ...state.queries].sort(
|
|
||||||
(a, b) => utils.getTimeStampByDate(b.createdAt) - utils.getTimeStampByDate(a.createdAt)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...state,
|
|
||||||
queries,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
case "DELETE_QUERY_BY_ID": {
|
|
||||||
return {
|
|
||||||
...state,
|
|
||||||
queries: [...state.queries].filter((query) => query.id !== action.payload.id),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
case "UPDATE_QUERY": {
|
|
||||||
const queries = state.queries.map((m) => {
|
|
||||||
if (m.id === action.payload.id) {
|
|
||||||
return {
|
|
||||||
...m,
|
|
||||||
...action.payload,
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
...state,
|
|
||||||
queries,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
default: {
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const defaultState: State = {
|
|
||||||
queries: [],
|
|
||||||
};
|
|
@ -0,0 +1,92 @@
|
|||||||
|
import utils from "../helpers/utils";
|
||||||
|
|
||||||
|
export interface State {
|
||||||
|
shortcuts: Model.Shortcut[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SetShortcutsAction {
|
||||||
|
type: "SET_SHORTCUTS";
|
||||||
|
payload: {
|
||||||
|
shortcuts: Model.Shortcut[];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
interface InsertShortcutAction {
|
||||||
|
type: "INSERT_SHORTCUT";
|
||||||
|
payload: {
|
||||||
|
shortcut: Model.Shortcut;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DeleteShortcutByIdAction {
|
||||||
|
type: "DELETE_SHORTCUT_BY_ID";
|
||||||
|
payload: {
|
||||||
|
id: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
interface UpdateShortcutAction {
|
||||||
|
type: "UPDATE_SHORTCUT";
|
||||||
|
payload: Model.Shortcut;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Actions = SetShortcutsAction | InsertShortcutAction | DeleteShortcutByIdAction | UpdateShortcutAction;
|
||||||
|
|
||||||
|
export function reducer(state: State, action: Actions): State {
|
||||||
|
switch (action.type) {
|
||||||
|
case "SET_SHORTCUTS": {
|
||||||
|
const shortcuts = utils.dedupeObjectWithId(
|
||||||
|
action.payload.shortcuts
|
||||||
|
.sort((a, b) => utils.getTimeStampByDate(b.createdAt) - utils.getTimeStampByDate(a.createdAt))
|
||||||
|
.sort((a, b) => utils.getTimeStampByDate(b.updatedAt) - utils.getTimeStampByDate(a.updatedAt))
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
shortcuts,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
case "INSERT_SHORTCUT": {
|
||||||
|
const shortcuts = utils.dedupeObjectWithId(
|
||||||
|
[action.payload.shortcut, ...state.shortcuts].sort(
|
||||||
|
(a, b) => utils.getTimeStampByDate(b.createdAt) - utils.getTimeStampByDate(a.createdAt)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
shortcuts,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
case "DELETE_SHORTCUT_BY_ID": {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
shortcuts: [...state.shortcuts].filter((shortcut) => shortcut.id !== action.payload.id),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
case "UPDATE_SHORTCUT": {
|
||||||
|
const shortcuts = state.shortcuts.map((m) => {
|
||||||
|
if (m.id === action.payload.id) {
|
||||||
|
return {
|
||||||
|
...m,
|
||||||
|
...action.payload,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
shortcuts,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const defaultState: State = {
|
||||||
|
shortcuts: [],
|
||||||
|
};
|
@ -1,6 +1 @@
|
|||||||
declare namespace Api {
|
declare namespace Api {}
|
||||||
interface MemosStat {
|
|
||||||
timestamp: string;
|
|
||||||
amount: number;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue