impress-2020/src/WardrobePage.js

219 lines
5.6 KiB
JavaScript
Raw Normal View History

2020-04-21 20:32:53 -07:00
import React from "react";
import {
Box,
2020-04-22 02:39:06 -07:00
Editable,
EditablePreview,
EditableInput,
2020-04-21 20:32:53 -07:00
Grid,
2020-04-22 02:39:06 -07:00
Icon,
2020-04-22 01:10:24 -07:00
IconButton,
2020-04-22 02:39:06 -07:00
Input,
InputGroup,
InputLeftElement,
InputRightElement,
2020-04-21 20:32:53 -07:00
PseudoBox,
2020-04-22 15:24:38 -07:00
Skeleton,
2020-04-22 02:39:06 -07:00
Stack,
useToast,
2020-04-21 20:32:53 -07:00
} from "@chakra-ui/core";
2020-04-24 21:17:03 -07:00
import { Delay, Heading1, Heading2 } from "./util";
2020-04-22 15:24:38 -07:00
import ItemList, { ItemListSkeleton } from "./ItemList";
import OutfitPreview from "./OutfitPreview";
2020-04-24 21:17:03 -07:00
import SearchPanel from "./SearchPanel";
import useOutfitState from "./useOutfitState.js";
2020-04-21 20:32:53 -07:00
function WardrobePage() {
2020-04-24 19:16:24 -07:00
const { loading, error, outfitState, dispatchToOutfit } = useOutfitState();
2020-04-22 02:39:06 -07:00
const [searchQuery, setSearchQuery] = React.useState("");
const toast = useToast();
2020-04-22 15:24:38 -07:00
React.useEffect(() => {
if (error) {
2020-04-24 21:17:03 -07:00
console.log(error);
2020-04-22 15:24:38 -07:00
toast({
title: "We couldn't load this outfit 😖",
description: "Please reload the page to try again. Sorry!",
status: "error",
isClosable: true,
2020-04-24 21:17:03 -07:00
duration: 999999999,
2020-04-22 15:24:38 -07:00
});
}
}, [error, toast]);
2020-04-21 20:32:53 -07:00
return (
2020-04-24 00:46:24 -07:00
<Box position="absolute" top="0" bottom="0" left="0" right="0">
<Grid
// Fullscreen, split into a vertical stack on smaller screens
// or a horizontal stack on larger ones!
templateAreas={{
base: `"outfit"
2020-04-24 17:48:22 -07:00
"search"
"items"`,
2020-04-24 00:46:24 -07:00
lg: `"outfit search"
2020-04-24 17:48:22 -07:00
"outfit items"`,
2020-04-24 00:46:24 -07:00
}}
templateRows={{
base: "minmax(100px, 1fr) auto minmax(300px, 1fr)",
lg: "auto 1fr",
}}
templateColumns={{
base: "100%",
lg: "50% 50%",
}}
height="100%"
width="100%"
>
<Box gridArea="outfit" backgroundColor="gray.900">
2020-04-24 19:16:24 -07:00
<OutfitPreview outfitState={outfitState} />
2020-04-22 02:39:06 -07:00
</Box>
2020-04-24 00:46:24 -07:00
<Box gridArea="search" boxShadow="sm">
<Box px="5" py="3">
<SearchToolbar query={searchQuery} onChange={setSearchQuery} />
</Box>
2020-04-21 20:32:53 -07:00
</Box>
2020-04-24 00:46:24 -07:00
<Box gridArea="items" overflow="auto">
<Box px="5" py="5">
{searchQuery ? (
<SearchPanel
query={searchQuery}
2020-04-24 19:16:24 -07:00
outfitState={outfitState}
2020-04-24 18:39:38 -07:00
dispatchToOutfit={dispatchToOutfit}
2020-04-24 00:46:24 -07:00
/>
) : (
<ItemsPanel
loading={loading}
2020-04-24 19:16:24 -07:00
outfitState={outfitState}
2020-04-24 18:39:38 -07:00
dispatchToOutfit={dispatchToOutfit}
2020-04-24 00:46:24 -07:00
/>
)}
</Box>
</Box>
</Grid>
</Box>
2020-04-21 20:32:53 -07:00
);
}
2020-04-22 02:39:06 -07:00
function SearchToolbar({ query, onChange }) {
return (
<InputGroup>
<InputLeftElement>
<Icon name="search" color="gray.400" />
</InputLeftElement>
<Input
2020-04-22 03:13:47 -07:00
placeholder="Search for items to add…"
2020-04-22 02:39:06 -07:00
focusBorderColor="green.600"
color="green.800"
value={query}
onChange={(e) => onChange(e.target.value)}
2020-04-22 04:07:34 -07:00
onKeyDown={(e) => {
if (e.key === "Escape") {
onChange("");
e.target.blur();
}
}}
2020-04-22 02:39:06 -07:00
/>
{query && (
<InputRightElement>
<IconButton
icon="close"
color="gray.400"
variant="ghost"
variantColor="green"
aria-label="Clear search"
onClick={() => onChange("")}
/>
</InputRightElement>
)}
</InputGroup>
);
}
2020-04-24 19:16:24 -07:00
function ItemsPanel({ outfitState, loading, dispatchToOutfit }) {
const { zonesAndItems, wornItemIds } = outfitState;
2020-04-21 20:32:53 -07:00
return (
<Box color="green.800">
2020-04-24 23:29:26 -07:00
<OutfitHeading
outfitState={outfitState}
dispatchToOutfit={dispatchToOutfit}
/>
2020-04-22 01:10:24 -07:00
<Stack spacing="10">
2020-04-22 15:24:38 -07:00
{loading &&
[1, 2, 3].map((i) => (
<Box key={i}>
<Delay>
<Skeleton height="2.3rem" width="12rem" mb="3" />
2020-04-24 21:23:03 -07:00
<ItemListSkeleton count={3} />
</Delay>
2020-04-22 15:24:38 -07:00
</Box>
))}
{!loading &&
2020-04-24 19:16:24 -07:00
zonesAndItems.map(({ zone, items }) => (
<Box key={zone.id}>
<Heading2 mb="3">{zone.label}</Heading2>
2020-04-22 15:24:38 -07:00
<ItemList
items={items}
2020-04-24 19:16:24 -07:00
wornItemIds={items
.map((i) => i.id)
.filter((id) => wornItemIds.includes(id))}
2020-04-24 18:39:38 -07:00
dispatchToOutfit={dispatchToOutfit}
2020-04-22 15:24:38 -07:00
/>
</Box>
))}
2020-04-22 01:10:24 -07:00
</Stack>
</Box>
2020-04-21 20:32:53 -07:00
);
}
2020-04-24 23:29:26 -07:00
function OutfitHeading({ outfitState, dispatchToOutfit }) {
return (
2020-04-22 15:24:38 -07:00
<Box>
2020-04-24 23:29:26 -07:00
<PseudoBox role="group" d="inline-block" position="relative" width="100%">
2020-04-22 15:24:38 -07:00
<Heading1 mb="6">
2020-04-24 23:29:26 -07:00
<Editable
value={outfitState.name}
placeholder="Untitled outfit (click to edit)"
onChange={(value) =>
dispatchToOutfit({ type: "rename", outfitName: value })
}
>
2020-04-22 15:24:38 -07:00
{({ isEditing, onRequestEdit }) => (
<>
<EditablePreview />
<EditableInput />
{!isEditing && (
<OutfitNameEditButton onRequestEdit={onRequestEdit} />
)}
</>
)}
</Editable>
</Heading1>
</PseudoBox>
</Box>
);
}
2020-04-22 04:04:02 -07:00
function OutfitNameEditButton({ onRequestEdit }) {
return (
<PseudoBox
d="inline-block"
opacity="0"
transition="opacity 0.5s"
_groupHover={{ opacity: "1" }}
onClick={onRequestEdit}
position="absolute"
>
<IconButton
icon="edit"
variant="link"
color="green.600"
aria-label="Edit outfit name"
title="Edit outfit name"
/>
</PseudoBox>
);
}
2020-04-21 20:32:53 -07:00
export default WardrobePage;