2019-08-04 21:58:31 +00:00
|
|
|
import loadDirectory, { FileObject } from '@/api/server/files/loadDirectory';
|
|
|
|
import { action, Action, thunk, Thunk } from 'easy-peasy';
|
|
|
|
import { ServerStore } from '@/state/server/index';
|
2020-04-10 20:57:24 +00:00
|
|
|
import { cleanDirectoryPath } from '@/helpers';
|
2019-08-04 21:58:31 +00:00
|
|
|
|
|
|
|
export interface ServerFileStore {
|
|
|
|
directory: string;
|
|
|
|
contents: FileObject[];
|
|
|
|
getDirectoryContents: Thunk<ServerFileStore, string, {}, ServerStore, Promise<void>>;
|
|
|
|
setContents: Action<ServerFileStore, FileObject[]>;
|
|
|
|
pushFile: Action<ServerFileStore, FileObject>;
|
|
|
|
removeFile: Action<ServerFileStore, string>;
|
|
|
|
setDirectory: Action<ServerFileStore, string>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const files: ServerFileStore = {
|
2019-08-06 04:52:48 +00:00
|
|
|
directory: '/',
|
2019-08-04 21:58:31 +00:00
|
|
|
contents: [],
|
|
|
|
|
|
|
|
getDirectoryContents: thunk(async (actions, payload, { getStoreState }) => {
|
|
|
|
const server = getStoreState().server.data;
|
|
|
|
if (!server) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-10 20:57:24 +00:00
|
|
|
const contents = await loadDirectory(server.uuid, cleanDirectoryPath(payload));
|
2019-08-04 21:58:31 +00:00
|
|
|
|
2019-11-03 18:46:43 +00:00
|
|
|
actions.setDirectory(payload.length === 0 ? '/' : payload);
|
2019-08-04 21:58:31 +00:00
|
|
|
actions.setContents(contents);
|
|
|
|
}),
|
|
|
|
|
|
|
|
setContents: action((state, payload) => {
|
|
|
|
state.contents = payload;
|
|
|
|
}),
|
|
|
|
|
|
|
|
pushFile: action((state, payload) => {
|
|
|
|
const matchIndex = state.contents.findIndex(file => file.uuid === payload.uuid);
|
|
|
|
if (matchIndex < 0) {
|
|
|
|
state.contents = state.contents.concat(payload);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
state.contents[matchIndex] = payload;
|
|
|
|
}),
|
|
|
|
|
|
|
|
removeFile: action((state, payload) => {
|
|
|
|
state.contents = state.contents.filter(file => file.uuid !== payload);
|
|
|
|
}),
|
|
|
|
|
|
|
|
setDirectory: action((state, payload) => {
|
2020-04-10 20:57:24 +00:00
|
|
|
state.directory = cleanDirectoryPath(payload)
|
2019-08-04 21:58:31 +00:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
export default files;
|