import { action, Action } from 'easy-peasy'; import { cleanDirectoryPath } from '@/helpers'; export interface FileUploadData { loaded: number; readonly abort: AbortController; readonly total: number; } export interface ServerFileStore { directory: string; selectedFiles: string[]; uploads: Record; setDirectory: Action; setSelectedFiles: Action; appendSelectedFile: Action; removeSelectedFile: Action; pushFileUpload: Action; setUploadProgress: Action; clearFileUploads: Action; removeFileUpload: Action; } const files: ServerFileStore = { directory: '/', selectedFiles: [], uploads: {}, setDirectory: action((state, payload) => { state.directory = cleanDirectoryPath(payload); }), setSelectedFiles: action((state, payload) => { state.selectedFiles = payload; }), appendSelectedFile: action((state, payload) => { state.selectedFiles = state.selectedFiles.filter((f) => f !== payload).concat(payload); }), removeSelectedFile: action((state, payload) => { state.selectedFiles = state.selectedFiles.filter((f) => f !== payload); }), clearFileUploads: action((state) => { Object.values(state.uploads).forEach((upload) => upload.abort.abort()); state.uploads = {}; }), pushFileUpload: action((state, payload) => { state.uploads[payload.name] = payload.data; }), setUploadProgress: action((state, { name, loaded }) => { if (state.uploads[name]) { state.uploads[name].loaded = loaded; } }), removeFileUpload: action((state, payload) => { 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]; } }), }; export default files;