Move state to top level.

pull/24/head
Sander Vocke 5 years ago
parent 86060497e2
commit 533cfcd7d1
  1. 29
      client/src/components/windows/ManageTagsWindow.tsx
  2. 2
      client/src/components/windows/Windows.tsx

@ -22,15 +22,27 @@ export interface TagChange {
} }
export interface ManageTagsWindowState extends WindowState { export interface ManageTagsWindowState extends WindowState {
fetchedTags: any[], fetchedTags: any[] | null,
pendingChanges: TagChange[], pendingChanges: TagChange[],
} }
export enum ManageTagsWindowActions { export enum ManageTagsWindowActions {
SetFetchedTags = "SetFetchedTags",
SetPendingChanges = "SetPendingChanges",
} }
export function ManageTagsWindowReducer(state: ManageTagsWindowState, action: any) { export function ManageTagsWindowReducer(state: ManageTagsWindowState, action: any) {
switch (action.type) { switch (action.type) {
case ManageTagsWindowActions.SetFetchedTags:
return {
...state,
fetchedTags: action.value,
}
case ManageTagsWindowActions.SetPendingChanges:
return {
...state,
pendingChanges: action.value,
}
default: default:
throw new Error("Unimplemented ManageTagsWindow state update.") throw new Error("Unimplemented ManageTagsWindow state update.")
} }
@ -126,16 +138,22 @@ export interface IProps {
} }
export default function ManageTagsWindow(props: IProps) { export default function ManageTagsWindow(props: IProps) {
const [tags, setTags] = React.useState<any[]>([]);
useEffect(() => { useEffect(() => {
if (props.state.fetchedTags !== null) {
return;
}
(async () => { (async () => {
const allTags = await getAllTags(); const allTags = await getAllTags();
// We have the tags in list form. Now, we want to organize // We have the tags in list form. Now, we want to organize
// them hierarchically by giving each tag a "children" prop. // them hierarchically by giving each tag a "children" prop.
setTags(organiseTags(allTags, null)); props.dispatch({
type: ManageTagsWindowActions.SetFetchedTags,
value: organiseTags(allTags, null),
});
})(); })();
}, []); }, [props.state.fetchedTags]);
const tags = props.state.fetchedTags || [];
return <Box width="100%" justifyContent="center" display="flex" flexWrap="wrap"> return <Box width="100%" justifyContent="center" display="flex" flexWrap="wrap">
<Box <Box
@ -158,7 +176,6 @@ export default function ManageTagsWindow(props: IProps) {
width="80%" width="80%"
> >
{tags && tags.length && tags.map((tag: any) => { {tags && tags.length && tags.map((tag: any) => {
console.log("Tags:", tags);
return <Box mt={1}><SingleTag tag={tag} prependElems={[]} /></Box>; return <Box mt={1}><SingleTag tag={tag} prependElems={[]} /></Box>;
})} })}
</Box> </Box>

@ -85,6 +85,8 @@ export const newWindowState = {
[WindowType.ManageTags]: () => { [WindowType.ManageTags]: () => {
return { return {
tabLabel: <><LoyaltyIcon/>Manage Tags</>, tabLabel: <><LoyaltyIcon/>Manage Tags</>,
fetchedTags: null,
pendingChanges: [],
} }
} }
} }
Loading…
Cancel
Save