import { __ } from '@wordpress/i18n'; import { useRef, useState } from '@wordpress/element'; import { Button, CheckboxControl, Notice, SelectControl, TextControl, TextareaControl, } from '@wordpress/components'; import type { FormEvent, ReactNode } from 'react'; import { claimAction, newIdempotencyKey, serverAnswered, unitAction, } from '../api'; import type { ApiError, Claim, Unit } from '../types'; export interface Field { name: string; label: string; type: 'date' | 'text' | 'textarea' | 'select' | 'checkbox'; options?: { label: string; value: string }[]; required?: boolean; initial?: string | boolean; help?: string; /** Dates are up to today unless the field is about the future (a new end of the guarantee). */ allowFuture?: boolean; } interface Props { /** Claim action, or unit action when `unit` is set. */ target: { claim: Claim } | { unit: Unit }; type: string; title: string; submitLabel: string; fields: Field[]; secondary?: boolean; intro?: ReactNode; /** Sent with the fields, not shown (what the screen showed, for example). */ extra?: Record< string, unknown >; onDone: ( result: Claim | Unit ) => void; /** The record changed meanwhile: reload, showing the server's explanation. */ onStale: ( message?: string ) => void; } const STALE_CODES = [ 'abcreate_wd_version_conflict', 'abcreate_wd_informativa_uncertain', 'abcreate_wd_informativa_not_recorded', 'abcreate_wd_order_changed', 'abcreate_wd_facts_changed', ]; export default function ActionForm( props: Props ) { const defaultFor = ( f: Field ): string | boolean => { if ( f.initial !== undefined ) { return f.initial; } switch ( f.type ) { case 'checkbox': return false; case 'date': // Empty on purpose: the operator picks the real day of the event, // never today's date by accident (one click on "Today" if it is). return ''; case 'select': return f.options?.[ 0 ]?.value ?? ''; default: return ''; } }; const initial = () => Object.fromEntries( props.fields.map( ( f ) => [ f.name, defaultFor( f ) ] ) ); const [ values, setValues ] = useState< Record< string, string | boolean > >( initial ); const [ busy, setBusy ] = useState( false ); const [ error, setError ] = useState< string | null >( null ); // One key per intention: a network retry reuses it, so nothing is recorded twice. const key = useRef( newIdempotencyKey() ); const valid = props.fields.every( ( f ) => ! f.required || ( typeof values[ f.name ] === 'string' ? ( values[ f.name ] as string ).trim() !== '' : true ) ); const set = ( name: string, value: string | boolean ) => setValues( ( old ) => ( { ...old, [ name ]: value } ) ); const submit = async ( event: FormEvent ) => { event.preventDefault(); if ( ! valid || busy ) { return; } setBusy( true ); setError( null ); const body: Record< string, unknown > = { ...( props.extra ?? {} ) }; for ( const f of props.fields ) { const v = values[ f.name ]; if ( typeof v === 'string' ) { if ( v.trim() !== '' ) { body[ f.name ] = [ 'refund_id', 'months' ].includes( f.name ) ? Number( v ) : v.trim(); } } else { body[ f.name ] = v; } } try { const result = 'claim' in props.target ? await claimAction( props.target.claim.id, { type: props.type, expected_version: props.target.claim.version, ...body, }, key.current ) : await unitAction( props.target.unit.id ?? 0, { type: props.type, expected_version: props.target.unit.version, ...body, }, key.current ); key.current = newIdempotencyKey(); setValues( initial() ); props.onDone( result ); } catch ( e ) { const err = e as ApiError; if ( serverAnswered( err ) ) { // The server answered: this intention is finished, the next try is a new one. key.current = newIdempotencyKey(); } setError( err?.message ?? __( 'Network error. You can retry safely.', 'abcreate-warranty-desk' ) ); if ( err?.code && STALE_CODES.includes( err.code ) ) { props.onStale( err.message ); } } finally { setBusy( false ); } }; return (

{ props.title }

{ props.intro } { error && ( { error } ) }
{ props.fields.map( ( f ) => { const label = f.required ? `${ f.label } *` : f.label; switch ( f.type ) { case 'checkbox': return ( set( f.name, v ) } /> ); case 'select': return ( set( f.name, v ) } /> ); case 'textarea': return ( set( f.name, v ) } rows={ 2 } /> ); default: return (
set( f.name, v ) } /> { f.type === 'date' && ! f.allowFuture && values[ f.name ] !== window.abcreateWd.today && ( ) }
); } } ) }
); }