impress-2020/src/app/WardrobePage/support/ItemSupportDrawer.js

214 lines
5.4 KiB
JavaScript
Raw Normal View History

import * as React from "react";
2020-07-31 22:11:32 -07:00
import gql from "graphql-tag";
import { useQuery, useMutation } from "@apollo/client";
import {
Badge,
Box,
Drawer,
DrawerBody,
DrawerCloseButton,
DrawerContent,
DrawerHeader,
DrawerOverlay,
FormControl,
2020-07-31 22:11:32 -07:00
FormErrorMessage,
FormHelperText,
FormLabel,
Link,
Select,
2020-07-31 22:11:32 -07:00
Spinner,
useBreakpointValue,
} from "@chakra-ui/core";
import { CheckCircleIcon, ExternalLinkIcon } from "@chakra-ui/icons";
import useSupportSecret from "./useSupportSecret";
/**
* ItemSupportDrawer shows Support UI for the item when open.
*
* This component controls the drawer element. The actual content is imported
* from another lazy-loaded component!
*/
function ItemSupportDrawer({ item, isOpen, onClose }) {
2020-07-31 22:11:32 -07:00
const placement = useBreakpointValue({
base: "bottom",
lg: "right",
// TODO: There's a bug in the Chakra RC that doesn't read the breakpoint
// specification correctly - we need these extra keys until it's fixed!
// https://github.com/chakra-ui/chakra-ui/issues/1444
0: "bottom",
1: "bottom",
2: "right",
3: "right",
});
return (
<Drawer
2020-07-31 22:11:32 -07:00
placement={placement}
isOpen={isOpen}
onClose={onClose}
// blockScrollOnMount doesn't matter on our fullscreen UI, but the
// default implementation breaks out layout somehow 🤔 idk, let's not!
blockScrollOnMount={false}
>
<DrawerOverlay>
<DrawerContent>
<DrawerCloseButton />
<DrawerHeader>
{item.name}
2020-07-31 22:11:32 -07:00
<Badge colorScheme="pink" marginLeft="3">
Support <span aria-hidden="true">💖</span>
</Badge>
</DrawerHeader>
<DrawerBody>
<Box paddingBottom="5">
<SpecialColorFields item={item} />
</Box>
</DrawerBody>
</DrawerContent>
</DrawerOverlay>
</Drawer>
);
}
function SpecialColorFields({ item }) {
const supportSecret = useSupportSecret();
const { loading: itemLoading, error: itemError, data: itemData } = useQuery(
gql`
query ItemSupportDrawerManualSpecialColor($itemId: ID!) {
item(id: $itemId) {
manualSpecialColor {
id
}
}
2020-07-31 22:11:32 -07:00
}
`,
{ variables: { itemId: item.id } }
);
2020-07-31 22:11:32 -07:00
const {
loading: colorsLoading,
error: colorsError,
data: colorsData,
} = useQuery(
gql`
query ItemSupportDrawerAllColors {
allColors {
id
name
isStandard
}
}
`
);
const [
mutate,
{ loading: mutationLoading, error: mutationError, data: mutationData },
] = useMutation(gql`
mutation ItemSupportDrawerSetManualSpecialColor(
$itemId: ID!
$colorId: ID
$supportSecret: String!
) {
setManualSpecialColor(
itemId: $itemId
colorId: $colorId
supportSecret: $supportSecret
) {
manualSpecialColor {
__typename
id
}
}
}
`);
const nonStandardColors =
colorsData?.allColors?.filter((c) => !c.isStandard) || [];
2020-07-31 22:11:32 -07:00
nonStandardColors.sort((a, b) => a.name.localeCompare(b.name));
return (
<FormControl
isInvalid={colorsError || itemError || mutationError ? true : false}
>
<FormLabel>Special color</FormLabel>
2020-07-31 22:11:32 -07:00
<Select
placeholder={
colorsLoading || itemLoading
? "Loading…"
: "Default: Auto-detect from item description"
}
icon={
colorsLoading || itemLoading || mutationLoading ? (
<Spinner />
) : mutationData ? (
<CheckCircleIcon />
) : undefined
}
value={itemData?.item?.manualSpecialColor?.id}
onChange={(e) => {
const colorId = e.target.value;
const color =
colorId != null ? { __typename: "Color", id: colorId } : null;
console.log({
__typename: "Mutation",
setManualSpecialColor: {
__typename: "Item",
id: item.id,
manualSpecialColor: color,
},
});
mutate({
variables: {
itemId: item.id,
colorId,
supportSecret,
},
optimisticResponse: {
__typename: "Mutation",
setManualSpecialColor: {
__typename: "Item",
id: item.id,
manualSpecialColor: color,
},
},
});
}}
2020-07-31 22:11:32 -07:00
>
{nonStandardColors.map((color) => (
<option key={color.id} value={color.id}>
{color.name}
</option>
))}
</Select>
{colorsError && (
<FormErrorMessage>{colorsError.message}</FormErrorMessage>
)}
{itemError && <FormErrorMessage>{itemError.message}</FormErrorMessage>}
{mutationError && (
<FormErrorMessage>{mutationError.message}</FormErrorMessage>
)}
{!colorsError && !itemError && !mutationError && (
2020-07-31 22:11:32 -07:00
<FormHelperText>
This controls which previews we show on the{" "}
<Link
href={`https://impress.openneo.net/items/${
item.id
}-${item.name.replace(/ /g, "-")}`}
color="green.500"
isExternal
>
item page <ExternalLinkIcon />
</Link>
.
</FormHelperText>
)}
</FormControl>
);
}
export default ItemSupportDrawer;