2020-07-11 06:09:37 +00:00
|
|
|
import { action, Action } from 'easy-peasy';
|
2020-04-10 20:57:24 +00:00
|
|
|
import { cleanDirectoryPath } from '@/helpers';
|
2019-08-04 21:58:31 +00:00
|
|
|
|
2022-11-21 20:58:55 +00:00
|
|
|
export interface FileUploadData {
|
2022-07-24 21:18:32 +00:00
|
|
|
loaded: number;
|
2022-11-21 20:58:55 +00:00
|
|
|
readonly abort: AbortController;
|
2022-07-24 21:18:32 +00:00
|
|
|
readonly total: number;
|
|
|
|
}
|
|
|
|
|
2019-08-04 21:58:31 +00:00
|
|
|
export interface ServerFileStore {
|
|
|
|
directory: string;
|
2020-07-11 23:47:13 +00:00
|
|
|
selectedFiles: string[];
|
2022-11-21 20:58:55 +00:00
|
|
|
uploads: Record<string, FileUploadData>;
|
2020-07-11 23:47:13 +00:00
|
|
|
|
2019-08-04 21:58:31 +00:00
|
|
|
setDirectory: Action<ServerFileStore, string>;
|
2020-07-11 23:47:13 +00:00
|
|
|
setSelectedFiles: Action<ServerFileStore, string[]>;
|
2020-07-11 23:57:30 +00:00
|
|
|
appendSelectedFile: Action<ServerFileStore, string>;
|
|
|
|
removeSelectedFile: Action<ServerFileStore, string>;
|
2022-07-24 21:18:32 +00:00
|
|
|
|
2022-11-21 20:58:55 +00:00
|
|
|
pushFileUpload: Action<ServerFileStore, { name: string; data: FileUploadData }>;
|
|
|
|
setUploadProgress: Action<ServerFileStore, { name: string; loaded: number }>;
|
2022-07-24 22:50:47 +00:00
|
|
|
clearFileUploads: Action<ServerFileStore>;
|
2022-07-24 21:18:32 +00:00
|
|
|
removeFileUpload: Action<ServerFileStore, string>;
|
2022-12-04 23:36:53 +00:00
|
|
|
cancelFileUpload: Action<ServerFileStore, string>;
|
2019-08-04 21:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const files: ServerFileStore = {
|
2019-08-06 04:52:48 +00:00
|
|
|
directory: '/',
|
2020-07-11 23:47:13 +00:00
|
|
|
selectedFiles: [],
|
2022-11-21 20:58:55 +00:00
|
|
|
uploads: {},
|
2019-08-04 21:58:31 +00:00
|
|
|
|
|
|
|
setDirectory: action((state, payload) => {
|
2020-06-13 16:49:32 +00:00
|
|
|
state.directory = cleanDirectoryPath(payload);
|
2019-08-04 21:58:31 +00:00
|
|
|
}),
|
2020-07-11 23:47:13 +00:00
|
|
|
|
|
|
|
setSelectedFiles: action((state, payload) => {
|
|
|
|
state.selectedFiles = payload;
|
|
|
|
}),
|
2020-07-11 23:57:30 +00:00
|
|
|
|
|
|
|
appendSelectedFile: action((state, payload) => {
|
2022-06-26 19:13:52 +00:00
|
|
|
state.selectedFiles = state.selectedFiles.filter((f) => f !== payload).concat(payload);
|
2020-07-11 23:57:30 +00:00
|
|
|
}),
|
|
|
|
|
|
|
|
removeSelectedFile: action((state, payload) => {
|
2022-06-26 19:13:52 +00:00
|
|
|
state.selectedFiles = state.selectedFiles.filter((f) => f !== payload);
|
2020-07-11 23:57:30 +00:00
|
|
|
}),
|
2022-07-24 21:18:32 +00:00
|
|
|
|
2022-07-24 22:50:47 +00:00
|
|
|
clearFileUploads: action((state) => {
|
2022-11-21 20:58:55 +00:00
|
|
|
Object.values(state.uploads).forEach((upload) => upload.abort.abort());
|
|
|
|
|
|
|
|
state.uploads = {};
|
|
|
|
}),
|
|
|
|
|
|
|
|
pushFileUpload: action((state, payload) => {
|
|
|
|
state.uploads[payload.name] = payload.data;
|
2022-07-24 22:50:47 +00:00
|
|
|
}),
|
|
|
|
|
2022-11-21 20:58:55 +00:00
|
|
|
setUploadProgress: action((state, { name, loaded }) => {
|
|
|
|
if (state.uploads[name]) {
|
|
|
|
state.uploads[name].loaded = loaded;
|
2022-07-24 22:50:47 +00:00
|
|
|
}
|
2022-07-24 21:18:32 +00:00
|
|
|
}),
|
|
|
|
|
|
|
|
removeFileUpload: action((state, payload) => {
|
2022-12-04 23:36:53 +00:00
|
|
|
if (state.uploads[payload]) {
|
|
|
|
delete state.uploads[payload];
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
cancelFileUpload: action((state, payload) => {
|
2022-11-21 20:58:55 +00:00
|
|
|
if (state.uploads[payload]) {
|
|
|
|
// Abort the request if it is still in flight. If it already completed this is
|
|
|
|
// a no-op.
|
|
|
|
state.uploads[payload].abort.abort();
|
|
|
|
|
|
|
|
delete state.uploads[payload];
|
|
|
|
}
|
2022-07-24 21:18:32 +00:00
|
|
|
}),
|
2019-08-04 21:58:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default files;
|