import React from "react"; import { Text, Modal, ModalOverlay, ModalContent, ModalHeader, ModalCloseButton, ModalBody, Input, Button, ModalFooter, FormErrorMessage, FormControl, Box, } from "@chakra-ui/core"; function OutfitResetModal({ isOpen, onClose, dispatchToOutfit }) { const [petName, setPetName] = React.useState(""); const onComplete = ({ custom_pet, object_info_registry }) => { dispatchToOutfit({ type: "reset", name: custom_pet.name, speciesId: custom_pet.species_id, colorId: custom_pet.color_id, wornItemIds: Object.values(object_info_registry).map( (o) => o.obj_info_id ), closetedItemIds: [], }); onClose(); setPetName(""); }; const { loading, error, loadOutfitData } = useLoadOutfitData( petName, onComplete ); const clearOutfit = () => { dispatchToOutfit({ type: "reset", name: "", wornItemIds: [], closetedItemIds: [], }); onClose(); setPetName(""); }; return (
{ e.preventDefault(); loadOutfitData(); }} > 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{" "} 😖 )}
); } function useLoadOutfitData(petName, onComplete) { const [loading, setLoading] = React.useState(false); const [error, setError] = React.useState(null); const loadOutfitData = async () => { setLoading(true); setError(null); let json; try { const res = await fetch( `http://www.neopets.com/amfphp/json.php/CustomPetService.getViewerData` + `/${petName}` ); if (!res.ok) { throw new Error(res.statusText); } json = await res.json(); if (!json.custom_pet) { throw new Error(`missing custom_pet data`); } } catch (e) { setLoading(false); setError(e); return; } setLoading(false); onComplete(json); }; return { loading, error, loadOutfitData }; } export default OutfitResetModal;