Add ?loadPet param to support custom search engine

This commit is contained in:
Emi Matchu 2021-04-16 03:15:24 -07:00
parent c04d071a7c
commit e1f9f87695

View file

@ -183,10 +183,11 @@ function SubmitPetForm() {
const history = useHistory(); const history = useHistory();
const theme = useTheme(); const theme = useTheme();
const toast = useToast(); const toast = useToast();
const location = useLocation();
const [petName, setPetName] = React.useState(""); const [petName, setPetName] = React.useState("");
const [loadPet, { loading }] = useLazyQuery( const [loadPetQuery, { loading }] = useLazyQuery(
gql` gql`
query SubmitPetForm($petName: String!) { query SubmitPetForm($petName: String!) {
petOnNeopetsDotCom(petName: $petName) { petOnNeopetsDotCom(petName: $petName) {
@ -230,10 +231,9 @@ function SubmitPetForm() {
} }
); );
const onSubmit = (e) => { const loadPet = React.useCallback(
e.preventDefault(); (petName) => {
loadPetQuery({ variables: { petName } });
loadPet({ variables: { petName } });
// Start preloading the WardrobePage, too! // Start preloading the WardrobePage, too!
// eslint-disable-next-line no-unused-expressions // eslint-disable-next-line no-unused-expressions
@ -242,7 +242,23 @@ function SubmitPetForm() {
// failures will happen elsewhere, and trigger reloads! // failures will happen elsewhere, and trigger reloads!
console.error(e); console.error(e);
}); });
}; },
[loadPetQuery]
);
// If the ?loadPet= query param is provided, auto-load the pet immediately.
// This isn't used in-app, but is a helpful hook for things like link-ins and
// custom search engines. (I feel like a route or a different UX would be
// better, but I don't really know enough to commit to one… let's just keep
// this simple for now, I think. We might change this someday!)
React.useEffect(() => {
const params = new URLSearchParams(location.search);
const petName = params.get("loadPet");
if (petName) {
setPetName(petName);
loadPet(petName);
}
}, [location, loadPet]);
const { brightBackground } = useCommonStyles(); const { brightBackground } = useCommonStyles();
const inputBorderColor = useColorModeValue("green.600", "green.500"); const inputBorderColor = useColorModeValue("green.600", "green.500");
@ -253,7 +269,12 @@ function SubmitPetForm() {
return ( return (
<ClassNames> <ClassNames>
{({ css }) => ( {({ css }) => (
<form onSubmit={onSubmit}> <form
onSubmit={(e) => {
e.preventDefault();
loadPet(petName);
}}
>
<Flex> <Flex>
<Input <Input
value={petName} value={petName}