import { Button, Select, SectionMessage, TextInput, } from "@pantheon-systems/pds-toolkit-react"; import { useMutation } from "@tanstack/react-query"; import { Controller, useForm } from "react-hook-form"; import { apiClient } from "../../../api/client"; import { getErrorMessage } from "../../../lib/errors"; import { usePostTypeOptions } from "../../../hooks/usePostTypeOptions"; interface Props { onDone: () => void; onCancel: () => void; onLoadingChange: (loading: boolean) => void; onLoadingStepChange: (step: string) => void; } export default function StepCreateCollection({ onDone, onCancel, onLoadingChange, onLoadingStepChange, }: Props) { const postTypeOptions = usePostTypeOptions(); const { control, handleSubmit, formState: { isValid }, } = useForm<{ collectionUrl: string; publishAs: string; }>({ mode: "onChange", defaultValues: { collectionUrl: window.CPUB_BOOTSTRAP.site_url, publishAs: "post", }, }); const createCollectionFlow = useMutation({ mutationFn: async (type: string) => { onLoadingStepChange("Creating your collection..."); const siteResp = await apiClient.post("/site"); const siteId = siteResp.data; if (!siteId) { throw new Error("Failed to create collection. Please try again."); } onLoadingStepChange("Creating an access token for your collection..."); await apiClient.post("/api-key"); onLoadingStepChange("Registering webhooks for your collection..."); await apiClient.put("/webhook"); onLoadingStepChange("Finalizing setup..."); await apiClient.post("/collection", { site_id: siteId, post_type: type, }); }, onMutate: () => { onLoadingChange(true); }, onSuccess: onDone, onError: () => { onLoadingChange(false); }, }); const onSubmit = (data: { collectionUrl: string; publishAs: string }) => { if (createCollectionFlow.isPending) return; createCollectionFlow.mutate(data.publishAs); }; return (
{createCollectionFlow.error && ( )}
( field.onChange(e.target.value)} onBlur={field.onBlur} /> )} />
void; onBlur: () => void } }) => (