Commit a139d008 authored by Axel Bocciarelli's avatar Axel Bocciarelli
Browse files

Populate address drop-downs with shipping and return addresses if not already present

parent 00871c1e
Pipeline #31691 passed with stage
in 3 minutes and 8 seconds
......@@ -20,6 +20,7 @@ import FieldAlert from '../Form/FieldAlert';
import TextFieldGroup from '../Form/TextFieldGroup';
import CreateAddress from './CreateAddress';
import styles from './ShippingForm.module.css';
import { addAddressIfAbsent } from '../../helpers';
const FORM_LAYOUT = { labelCol: { sm: 4 }, fieldCol: { sm: 8 } };
......@@ -38,23 +39,26 @@ function ShippingForm(props) {
name: 'shipment',
...shipment,
defaultShippingAddress: defaultShippingAddress?._id,
defaultReturnAddress: defaultReturnAddress?._id,
defaultReturnAddress: defaultReturnAddress && defaultReturnAddress._id,
},
});
const {
handleSubmit,
errors,
register,
formState,
setValue,
watch,
} = methods;
const { isSubmitting } = formState;
const { handleSubmit, errors, register, setValue, watch } = methods;
const { isSubmitting } = methods.formState;
const watchedShippingAddressId = watch('defaultShippingAddress');
const watchedReturnAddressId = watch('defaultReturnAddress') || null;
const addresses = useResource(AddressResource.listShape(), {});
const noAddress = addresses.length === 0;
const userAddresses = useResource(AddressResource.listShape(), {});
const shippingAddressOptions = addAddressIfAbsent(
userAddresses,
defaultShippingAddress
);
const returnAddressOptions = addAddressIfAbsent(
userAddresses,
defaultReturnAddress
);
const [alert, setAlert] = useState();
async function handleFormSubmit(values) {
......@@ -116,7 +120,7 @@ function ShippingForm(props) {
inputRef={register({ required: true })}
>
<option value="">Select default sending address...</option>
{addresses.map((addr) => (
{shippingAddressOptions.map((addr) => (
<option key={addr._id} value={addr._id}>
{addressFormatter(addr)}
</option>
......@@ -138,7 +142,7 @@ function ShippingForm(props) {
/>
)}
<HelpBlock>
{noAddress && 'No addresses found! '}
{shippingAddressOptions.length === 0 && 'No addresses found! '}
<CreateAddress
investigation={investigation}
type="shipping"
......@@ -170,7 +174,7 @@ function ShippingForm(props) {
inputRef={register}
>
<option value="">No default return address</option>
{addresses.map((addr) => (
{returnAddressOptions.map((addr) => (
<option key={addr._id} value={addr._id}>
{addressFormatter(addr)}
</option>
......@@ -191,7 +195,7 @@ function ShippingForm(props) {
/>
)}
<HelpBlock>
{noAddress && 'No addresses found! '}
{returnAddressOptions.length === 0 && 'No addresses found! '}
<CreateAddress
investigation={investigation}
type="return"
......
......@@ -93,3 +93,14 @@ export function getSamplePageURL(sample) {
return `${uiConfig.samplePageTemplateURL}${parameter.value}`;
}
export function addAddressIfAbsent(addresses, addressToAdd) {
if (
!addressToAdd ||
addresses.find((addr) => addr._id === addressToAdd._id)
) {
return addresses;
}
return [addressToAdd, ...addresses];
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment