2022-11-11 16:17:49 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Button } from '@wordpress/components';
|
|
|
|
import { useRef, useState } from '@wordpress/element';
|
|
|
|
import type { UniqueIdentifier } from '@dnd-kit/core';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { SettingsModal } from '../../shared-components';
|
|
|
|
import Form from './form';
|
|
|
|
import type { PickupLocation } from '../types';
|
|
|
|
|
|
|
|
const EditLocation = ( {
|
|
|
|
locationData,
|
|
|
|
editingLocation,
|
|
|
|
onClose,
|
|
|
|
onSave,
|
|
|
|
onDelete,
|
|
|
|
}: {
|
|
|
|
locationData: PickupLocation | null;
|
|
|
|
editingLocation: UniqueIdentifier | 'new';
|
|
|
|
onClose: () => void;
|
|
|
|
onSave: ( location: PickupLocation ) => void;
|
|
|
|
onDelete: () => void;
|
|
|
|
} ): JSX.Element | null => {
|
|
|
|
const formRef = useRef( null );
|
|
|
|
const [ values, setValues ] = useState< PickupLocation >(
|
|
|
|
locationData as PickupLocation
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( ! locationData ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SettingsModal
|
|
|
|
onRequestClose={ onClose }
|
|
|
|
title={
|
|
|
|
editingLocation === 'new'
|
2022-12-07 13:39:13 +00:00
|
|
|
? __( 'Pickup location', 'woo-gutenberg-products-block' )
|
2022-11-11 16:17:49 +00:00
|
|
|
: __(
|
2022-12-07 13:39:13 +00:00
|
|
|
'Edit pickup location',
|
2022-11-11 16:17:49 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
actions={
|
|
|
|
<>
|
|
|
|
{ editingLocation !== 'new' && (
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
className="button-link-delete"
|
|
|
|
onClick={ () => {
|
|
|
|
onDelete();
|
|
|
|
onClose();
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'Delete location',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</Button>
|
|
|
|
) }
|
|
|
|
<Button variant="secondary" onClick={ onClose }>
|
|
|
|
{ __( 'Cancel', 'woo-gutenberg-products-block' ) }
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
onClick={ () => {
|
2022-12-22 13:36:25 +00:00
|
|
|
const form =
|
|
|
|
formRef?.current as unknown as HTMLFormElement;
|
|
|
|
if ( form.reportValidity() ) {
|
|
|
|
onSave( values );
|
|
|
|
onClose();
|
|
|
|
}
|
2022-11-11 16:17:49 +00:00
|
|
|
} }
|
|
|
|
>
|
|
|
|
{ __( 'Done', 'woo-gutenberg-products-block' ) }
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Form
|
|
|
|
formRef={ formRef }
|
|
|
|
values={ values }
|
|
|
|
setValues={ setValues }
|
|
|
|
/>
|
|
|
|
</SettingsModal>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default EditLocation;
|