mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-15 02:54:07 +00:00
We were using file UUID and path interchangeable. This adds a new UUID type for static checking and fixes a bunch of issues found.
42 lines
1.1 KiB
TypeScript
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 });
|