split item query & colors query in Support UI

My hope is that this can help with perf! The colors are very likely to be cached, and we don't need to re-request them from the server here!
This commit is contained in:
Emi Matchu 2020-07-31 23:40:05 -07:00
parent cae2f1a977
commit a1d5669ac6

View file

@ -75,15 +75,9 @@ function ItemSupportDrawer({ item, isOpen, onClose }) {
function SpecialColorFields({ item }) { function SpecialColorFields({ item }) {
const supportSecret = useSupportSecret(); const supportSecret = useSupportSecret();
const { loading, error, data } = useQuery( const { loading: itemLoading, error: itemError, data: itemData } = useQuery(
gql` gql`
query ItemSupportDrawerSpecialColorFields($itemId: ID!) { query ItemSupportDrawerManualSpecialColor($itemId: ID!) {
allColors {
id
name
isStandard
}
item(id: $itemId) { item(id: $itemId) {
manualSpecialColor { manualSpecialColor {
id id
@ -94,9 +88,25 @@ function SpecialColorFields({ item }) {
{ variables: { itemId: item.id } } { variables: { itemId: item.id } }
); );
const {
loading: colorsLoading,
error: colorsError,
data: colorsData,
} = useQuery(
gql`
query ItemSupportDrawerAllColors {
allColors {
id
name
isStandard
}
}
`
);
const [ const [
mutate, mutate,
{ loading: loading2, error: error2, data: data2 }, { loading: mutationLoading, error: mutationError, data: mutationData },
] = useMutation(gql` ] = useMutation(gql`
mutation ItemSupportDrawerSetManualSpecialColor( mutation ItemSupportDrawerSetManualSpecialColor(
$itemId: ID! $itemId: ID!
@ -116,24 +126,29 @@ function SpecialColorFields({ item }) {
} }
`); `);
const nonStandardColors = data?.allColors?.filter((c) => !c.isStandard) || []; const nonStandardColors =
colorsData?.allColors?.filter((c) => !c.isStandard) || [];
nonStandardColors.sort((a, b) => a.name.localeCompare(b.name)); nonStandardColors.sort((a, b) => a.name.localeCompare(b.name));
return ( return (
<FormControl isInvalid={error ? true : false}> <FormControl
isInvalid={colorsError || itemError || mutationError ? true : false}
>
<FormLabel>Special color</FormLabel> <FormLabel>Special color</FormLabel>
<Select <Select
placeholder={ placeholder={
loading ? "Loading…" : "Default: Auto-detect from item description" colorsLoading || itemLoading
? "Loading…"
: "Default: Auto-detect from item description"
} }
icon={ icon={
loading || loading2 ? ( colorsLoading || itemLoading || mutationLoading ? (
<Spinner /> <Spinner />
) : data2 ? ( ) : mutationData ? (
<CheckCircleIcon /> <CheckCircleIcon />
) : undefined ) : undefined
} }
value={data?.item?.manualSpecialColor?.id} value={itemData?.item?.manualSpecialColor?.id}
onChange={(e) => { onChange={(e) => {
const colorId = e.target.value; const colorId = e.target.value;
const color = const color =
@ -169,9 +184,14 @@ function SpecialColorFields({ item }) {
</option> </option>
))} ))}
</Select> </Select>
{error && <FormErrorMessage>{error.message}</FormErrorMessage>} {colorsError && (
{error2 && <FormErrorMessage>{error2.message}</FormErrorMessage>} <FormErrorMessage>{colorsError.message}</FormErrorMessage>
{!error && ( )}
{itemError && <FormErrorMessage>{itemError.message}</FormErrorMessage>}
{mutationError && (
<FormErrorMessage>{mutationError.message}</FormErrorMessage>
)}
{!colorsError && !itemError && !mutationError && (
<FormHelperText> <FormHelperText>
This controls which previews we show on the{" "} This controls which previews we show on the{" "}
<Link <Link