Files
pybricks-code/src/fileStorage/reducers.ts
T
David Lechner 1709591afc fileStorage: untangle file id/name
We were using file UUID and path interchangeable. This adds a new UUID
type for static checking and fixes a bunch of issues found.
2022-04-01 17:24:00 -05:00

42 lines
1.1 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (c) 2022 The Pybricks Authors
import { Reducer, combineReducers } from 'redux';
import {
FileMetadata,
fileStorageDidAddItem,
fileStorageDidChangeItem,
fileStorageDidInitialize,
fileStorageDidRemoveItem,
} from './actions';
const isInitialized: Reducer<boolean> = (state = false, action) => {
if (fileStorageDidInitialize.matches(action)) {
return true;
}
return state;
};
const files: Reducer<readonly FileMetadata[]> = (state = [], action) => {
if (fileStorageDidInitialize.matches(action)) {
return [...action.files];
}
if (fileStorageDidAddItem.matches(action)) {
return [...state, action.file];
}
if (fileStorageDidChangeItem.matches(action)) {
return [...state].map((f) => (f.uuid === action.file.uuid ? action.file : f));
}
if (fileStorageDidRemoveItem.matches(action)) {
return [...state].filter((value) => value.uuid !== action.file.uuid);
}
return state;
};
export default combineReducers({ isInitialized, files });