({
Body,
Header,
Footer,
relocation,
onSuccess,
closeModal,
}: Props)
| 22 | }); |
| 23 | |
| 24 | export function RelocationPauseModal({ |
| 25 | Body, |
| 26 | Header, |
| 27 | Footer, |
| 28 | relocation, |
| 29 | onSuccess, |
| 30 | closeModal, |
| 31 | }: Props) { |
| 32 | const currentStep = RelocationSteps[relocation.step]; |
| 33 | const options = Object.keys(RelocationSteps) |
| 34 | .filter( |
| 35 | step => |
| 36 | RelocationSteps[step as keyof typeof RelocationSteps] > currentStep && |
| 37 | step !== 'COMPLETED' |
| 38 | ) |
| 39 | .map(step => ({value: step, label: titleCase(step)})); |
| 40 | options.unshift({value: 'ASAP', label: 'As soon as possible'}); |
| 41 | |
| 42 | const mutation = useMutation({ |
| 43 | mutationFn: (data: {atStep?: string}) => |
| 44 | fetchMutation<Relocation>({ |
| 45 | method: 'PUT', |
| 46 | url: `/relocations/${relocation.uuid}/pause/`, |
| 47 | data, |
| 48 | options: {host: relocation.region.url}, |
| 49 | }), |
| 50 | onSuccess: rawRelocation => { |
| 51 | addSuccessMessage('An autopause has been scheduled for this relocation.'); |
| 52 | onSuccess?.(rawRelocation); |
| 53 | closeModal(); |
| 54 | }, |
| 55 | onError: (error: unknown) => { |
| 56 | const fallback = 'Failed to schedule pause.'; |
| 57 | if (!(error instanceof RequestError)) { |
| 58 | addErrorMessage(fallback); |
| 59 | return; |
| 60 | } |
| 61 | const detail = error.responseJSON?.detail; |
| 62 | const message = typeof detail === 'string' ? detail : detail?.message; |
| 63 | addErrorMessage(message ?? fallback); |
| 64 | }, |
| 65 | }); |
| 66 | |
| 67 | const form = useScrapsForm({ |
| 68 | ...defaultFormOptions, |
| 69 | defaultValues: {atStep: ''}, |
| 70 | validators: {onDynamic: schema}, |
| 71 | onSubmit: ({value}) => { |
| 72 | const payload: {atStep?: string} = {}; |
| 73 | if (value.atStep !== 'ASAP') { |
| 74 | payload.atStep = value.atStep; |
| 75 | } |
| 76 | return mutation.mutateAsync(payload).catch(() => {}); |
| 77 | }, |
| 78 | }); |
| 79 | |
| 80 | return ( |
| 81 | <form.AppForm form={form}> |
nothing calls this directly
no test coverage detected