Attachment form and table, MusicXML renderer
This commit is contained in:
110
packages/frontend/src/FileReducer.ts
Normal file
110
packages/frontend/src/FileReducer.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import { mapProp, Update } from "./store";
|
||||
|
||||
export function FileReducer(prev: FileReducer.State, action: FileReducer.Action): FileReducer.State {
|
||||
switch (action.type) {
|
||||
case "reset":
|
||||
return FileReducer.initial;
|
||||
case "file":
|
||||
if (prev.file !== null) {
|
||||
if (action.file !== null) {
|
||||
if (prev.file.name === prev.filename && prev.file.type === prev.mediaType) {
|
||||
return Object.freeze<FileReducer.State>({
|
||||
filename: action.file.name,
|
||||
mediaType: action.file.type,
|
||||
file: action.file,
|
||||
});
|
||||
} else {
|
||||
return mapProp("file", action.file as File | null)(prev);
|
||||
}
|
||||
} else {
|
||||
if (prev.file.name === prev.filename && prev.file.type === prev.mediaType) {
|
||||
return FileReducer.initial;
|
||||
} else {
|
||||
return mapProp("file", action.file as File | null)(prev);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (action.file !== null) {
|
||||
if (prev.filename === "" && prev.mediaType === "") {
|
||||
return Object.freeze<FileReducer.State>({
|
||||
filename: action.file.name,
|
||||
mediaType: action.file.type,
|
||||
file: action.file,
|
||||
});
|
||||
} else {
|
||||
return mapProp("file", action.file as File | null)(prev);
|
||||
}
|
||||
} else {
|
||||
return prev;
|
||||
}
|
||||
}
|
||||
case "filename":
|
||||
return mapProp("filename", action.filename)(prev);
|
||||
case "mediaType":
|
||||
return mapProp("mediaType", action.mediaType)(prev);
|
||||
}
|
||||
}
|
||||
|
||||
export namespace FileReducer {
|
||||
|
||||
export interface ResetAction {
|
||||
readonly type: "reset";
|
||||
}
|
||||
|
||||
export const reset: ResetAction = Object.freeze<ResetAction>({
|
||||
type: "reset",
|
||||
});
|
||||
|
||||
export interface SetFileAction {
|
||||
readonly type: "file";
|
||||
readonly file: File | null;
|
||||
}
|
||||
|
||||
export function setFile(file: File | null): SetFileAction {
|
||||
return Object.freeze<SetFileAction>({
|
||||
type: "file",
|
||||
file,
|
||||
});
|
||||
}
|
||||
|
||||
export interface SetFilenameAction {
|
||||
readonly type: "filename";
|
||||
readonly filename: Update<string>;
|
||||
}
|
||||
|
||||
export function setFilename(filename: Update<string>): SetFilenameAction {
|
||||
return Object.freeze<SetFilenameAction>({
|
||||
type: "filename",
|
||||
filename,
|
||||
});
|
||||
}
|
||||
|
||||
export interface SetMediaTypeAction {
|
||||
readonly type: "mediaType";
|
||||
readonly mediaType: Update<string>;
|
||||
}
|
||||
|
||||
export function setMediaType(mediaType: Update<string>): SetMediaTypeAction {
|
||||
return Object.freeze<SetMediaTypeAction>({
|
||||
type: "mediaType",
|
||||
mediaType,
|
||||
});
|
||||
}
|
||||
|
||||
export type Action = ResetAction |
|
||||
SetFileAction |
|
||||
SetFilenameAction |
|
||||
SetMediaTypeAction;
|
||||
|
||||
export interface State {
|
||||
readonly filename: string;
|
||||
readonly mediaType: string;
|
||||
readonly file: File | null;
|
||||
}
|
||||
|
||||
export const initial: State = Object.freeze<State>({
|
||||
filename: "",
|
||||
mediaType: "",
|
||||
file: null,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user