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'
|
2023-12-12 22:12:36 +00:00
|
|
|
? __( 'Pickup location', 'woocommerce' )
|
2022-11-11 16:17:49 +00:00
|
|
|
: __(
|
2022-12-07 13:39:13 +00:00
|
|
|
'Edit pickup location',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2022-11-11 16:17:49 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
actions={
|
|
|
|
<>
|
|
|
|
{ editingLocation !== 'new' && (
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
className="button-link-delete"
|
|
|
|
onClick={ () => {
|
|
|
|
onDelete();
|
|
|
|
onClose();
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'Delete location',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2022-11-11 16:17:49 +00:00
|
|
|
) }
|
|
|
|
</Button>
|
|
|
|
) }
|
|
|
|
<Button variant="secondary" onClick={ onClose }>
|
2023-12-12 22:12:36 +00:00
|
|
|
{ __( 'Cancel', 'woocommerce' ) }
|
2022-11-11 16:17:49 +00:00
|
|
|
</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
|
|
|
} }
|
|
|
|
>
|
2023-12-12 22:12:36 +00:00
|
|
|
{ __( 'Done', 'woocommerce' ) }
|
2022-11-11 16:17:49 +00:00
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Form
|
|
|
|
formRef={ formRef }
|
|
|
|
values={ values }
|
|
|
|
setValues={ setValues }
|
|
|
|
/>
|
|
|
|
</SettingsModal>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default EditLocation;
|