import React from "react"; import gql from "graphql-tag"; import { useQuery } from "@apollo/react-hooks"; import { Text, Modal, ModalOverlay, ModalContent, ModalHeader, ModalCloseButton, ModalBody, Input, Button, ModalFooter, FormErrorMessage, FormControl, Box, } from "@chakra-ui/core"; /** * 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! */ function OutfitResetModal({ isOpen, onClose, dispatchToOutfit }) { const [petName, setPetName] = React.useState(""); const [submittedPetName, submitPetName] = React.useState(""); 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, wornItemIds: items.map((i) => i.id), closetedItemIds: [], }); onClose(); setPetName(""); submitPetName(""); }, } ); const clearOutfit = () => { dispatchToOutfit({ type: "reset", name: "", wornItemIds: [], closetedItemIds: [], }); onClose(); setPetName(""); submitPetName(""); }; return (
{ e.preventDefault(); submitPetName(petName); }} > Want to try your own pet?{" "} 😮 Choose a pet from Neopets.com, and we'll pull their outfit data into here for you to play with! setPetName(e.target.value)} autoFocus /> {error && ( We had trouble loading that pet, sorry{" "} 😖 )}
); } export default OutfitResetModal;