mirror of https://github.com/usememos/memos
feat: update storage setting section (#1140)
parent
6d2d322140
commit
84fb8b2288
@ -1,31 +0,0 @@
|
||||
import store, { useAppSelector } from "..";
|
||||
import * as api from "../../helpers/api";
|
||||
import { setStorages, createStorage, patchStorage, deleteStorage } from "../reducer/storage";
|
||||
|
||||
export const useStorageStore = () => {
|
||||
const state = useAppSelector((state) => state.storage);
|
||||
return {
|
||||
state,
|
||||
getState: () => {
|
||||
return store.getState().storage;
|
||||
},
|
||||
fetchStorages: async () => {
|
||||
const { data } = (await api.getStorageList()).data;
|
||||
store.dispatch(setStorages(data));
|
||||
},
|
||||
createStorage: async (storageCreate: StorageCreate) => {
|
||||
const { data: storage } = (await api.createStorage(storageCreate)).data;
|
||||
store.dispatch(createStorage(storage));
|
||||
return storage;
|
||||
},
|
||||
patchStorage: async (storagePatch: StoragePatch) => {
|
||||
const { data: storage } = (await api.patchStorage(storagePatch)).data;
|
||||
store.dispatch(patchStorage(storage));
|
||||
return storage;
|
||||
},
|
||||
deleteStorageById: async (storageId: StorageId) => {
|
||||
await api.deleteStorage(storageId);
|
||||
store.dispatch(deleteStorage(storageId));
|
||||
},
|
||||
};
|
||||
};
|
@ -1,53 +0,0 @@
|
||||
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
||||
|
||||
interface State {
|
||||
storages: Storage[];
|
||||
}
|
||||
|
||||
const storageSlice = createSlice({
|
||||
name: "storage",
|
||||
initialState: {
|
||||
storages: [],
|
||||
} as State,
|
||||
reducers: {
|
||||
setStorages: (state, action: PayloadAction<Storage[]>) => {
|
||||
return {
|
||||
...state,
|
||||
storages: action.payload,
|
||||
};
|
||||
},
|
||||
createStorage: (state, action: PayloadAction<Storage>) => {
|
||||
return {
|
||||
...state,
|
||||
storages: [action.payload].concat(state.storages),
|
||||
};
|
||||
},
|
||||
patchStorage: (state, action: PayloadAction<Partial<Storage>>) => {
|
||||
return {
|
||||
...state,
|
||||
storages: state.storages.map((storage) => {
|
||||
if (storage.id === action.payload.id) {
|
||||
return {
|
||||
...storage,
|
||||
...action.payload,
|
||||
};
|
||||
} else {
|
||||
return storage;
|
||||
}
|
||||
}),
|
||||
};
|
||||
},
|
||||
deleteStorage: (state, action: PayloadAction<StorageId>) => {
|
||||
return {
|
||||
...state,
|
||||
storages: state.storages.filter((storage) => {
|
||||
return storage.id !== action.payload;
|
||||
}),
|
||||
};
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const { setStorages, createStorage, patchStorage, deleteStorage } = storageSlice.actions;
|
||||
|
||||
export default storageSlice.reducer;
|
Loading…
Reference in New Issue