function reducer()

in frontend/src/js/components/Uploads/UploadFiles.tsx [80:119]


function reducer(state: State, action: Action): State {
    switch(action.type) {
        case "Reset":
            return action.state;

        case "Set_Target":
            return { ...state, target: action.target };

        case "Add_Files": {
            for(const [path, file] of action.files) {
                state.files.set(path, { file, state: { description: 'pending' }});
            }

            // Create a new object to make React re-render
            return { ...state };
        }

        case "Remove_Path": {
            for(const [key] of state.files) {
                if(key.startsWith(action.path)) {
                    state.files.delete(key);
                }
            }

            // Create a new object to make React re-render
            return { ...state };
        }

        case "Set_Upload_State": {
            const existing = state.files.get(action.file);

            if(existing) {
                state.files.set(action.file, { ...existing, state: action.state });
            }

            // Create a new object to make React re-render
            return { ...state };
        }
    }
}