impress-2020/src/app/OutfitResetModal.js

145 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-04-25 05:29:27 -07:00
import React from "react";
import gql from "graphql-tag";
import { useQuery } from "@apollo/react-hooks";
2020-04-25 05:29:27 -07:00
import {
Text,
Modal,
ModalOverlay,
ModalContent,
ModalHeader,
ModalCloseButton,
ModalBody,
Input,
Button,
ModalFooter,
FormErrorMessage,
FormControl,
2020-04-25 05:38:15 -07:00
Box,
2020-04-25 05:29:27 -07:00
} from "@chakra-ui/core";
2020-04-26 01:45:17 -07:00
/**
* OutfitResetModal gives the user the ability to reset their outfit, by either
* clearing out most of the data, or letting them type in a pet name to load
* from Neopets.com!
*/
2020-04-25 05:29:27 -07:00
function OutfitResetModal({ isOpen, onClose, dispatchToOutfit }) {
const [petName, setPetName] = React.useState("");
const [submittedPetName, submitPetName] = React.useState("");
2020-04-25 05:29:27 -07:00
const { loading, error } = useQuery(
gql`
query($petName: String!) {
petOnNeopetsDotCom(petName: $petName) {
color {
id
}
species {
id
}
items {
id
}
}
}
`,
{
variables: { petName: submittedPetName },
skip: !submittedPetName,
fetchPolicy: "network-only",
onCompleted: (data) => {
if (!data) return;
const { species, color, items } = data.petOnNeopetsDotCom;
dispatchToOutfit({
type: "reset",
name: petName,
speciesId: species.id,
colorId: color.id,
2020-05-03 01:08:43 -07:00
emotion: "HAPPY", // TODO: Ask PetService
genderPresentation: "FEMININE", // TODO: Ask PetService
wornItemIds: items.map((i) => i.id),
closetedItemIds: [],
});
onClose();
setPetName("");
submitPetName("");
},
}
2020-04-25 05:29:27 -07:00
);
2020-04-25 05:38:15 -07:00
const clearOutfit = () => {
dispatchToOutfit({
type: "reset",
name: "",
wornItemIds: [],
closetedItemIds: [],
});
onClose();
setPetName("");
submitPetName("");
2020-04-25 05:38:15 -07:00
};
2020-04-25 05:29:27 -07:00
return (
<Modal isOpen={isOpen} onClose={onClose}>
<ModalOverlay />
<ModalContent>
<form
onSubmit={(e) => {
e.preventDefault();
submitPetName(petName);
2020-04-25 05:29:27 -07:00
}}
>
<ModalHeader>
<Text fontFamily="Delicious">
Want to try your own pet?{" "}
<span role="img" aria-label="(surprise emoji)">
😮
</span>
</Text>
</ModalHeader>
<ModalCloseButton />
<ModalBody>
2020-04-25 05:38:15 -07:00
<Text>
2020-04-25 05:29:27 -07:00
Choose a pet from Neopets.com, and we'll pull their outfit data
into here for you to play with!
</Text>
2020-04-25 05:38:15 -07:00
<Box height="4" />
2020-04-25 05:29:27 -07:00
<FormControl isInvalid={error}>
<Input
placeholder="Enter a pet's name…"
value={petName}
onChange={(e) => setPetName(e.target.value)}
autoFocus
/>
{error && (
<FormErrorMessage>
We had trouble loading that pet, sorry{" "}
<span role="img" aria-label="(confounded emoji)">
😖
</span>
</FormErrorMessage>
)}
</FormControl>
</ModalBody>
<ModalFooter>
2020-04-25 05:38:15 -07:00
<Button
leftIcon="delete"
variant="outline"
variantColor="red"
onClick={clearOutfit}
>
Reset outfit
</Button>
<Box flex="1"></Box>
2020-04-25 05:29:27 -07:00
<Button type="submit" variantColor="green" isLoading={loading}>
Show me!
</Button>
</ModalFooter>
</form>
</ModalContent>
</Modal>
);
}
export default OutfitResetModal;