Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Clean up the code
Browse files Browse the repository at this point in the history
  • Loading branch information
tarhi-saad committed Feb 14, 2023
1 parent b62483e commit 64eee99
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import { getSetting } from '@woocommerce/settings';
import type { PickupLocation } from '../types';

// Outputs the list of countries and states in a single dropdown select.
const countryStateDropdownOptions = (
selectedCountry = '',
selectedState = ''
) => {
const countryStateDropdownOptions = () => {
const countries = getSetting< Record< string, string > >( 'countries', [] );
const states = getSetting< Record< string, Record< string, string > > >(
'countryStates',
Expand All @@ -40,29 +37,14 @@ const countryStateDropdownOptions = (
} ) );
return {
label: countries[ country ],
options: [
// {
// value: country,
// label: countries[ country ],
// },
...stateOptions,
],
options: [ ...stateOptions ],
};
} );
const selectedCountryState = `${ selectedCountry }:${ selectedState }`;
const selectedCountryStateOption = countryStateOptions
.map( ( option ) => option.options )
.flat()
.find( ( option ) => option.value === selectedCountryState );
// const selectedCountryOption = countryStateOptions.find(
// ( option ) => option.options[ 0 ].value === selectedCountry
// );
const selectedOption = selectedCountryStateOption || '';
return {
options: countryStateOptions,
selected: selectedOption,
};
};
const countryStateOptions = countryStateDropdownOptions();

const Form = ( {
formRef,
Expand All @@ -74,10 +56,6 @@ const Form = ( {
setValues: React.Dispatch< React.SetStateAction< PickupLocation > >;
} ) => {
const { country: selectedCountry, state: selectedState } = values.address;
const countryStateOptions = countryStateDropdownOptions(
selectedCountry,
selectedState
);
const states = getSetting< Record< string, Record< string, string > > >(
'countryStates',
[]
Expand Down Expand Up @@ -161,12 +139,12 @@ const Form = ( {
<SelectControl
name="location_country_state"
label={ __(
'Country / Region',
'Country / State',
'woo-gutenberg-products-block'
) }
hideLabelFromVision={ true }
placeholder={ __(
'Country / Region',
'Country / State',
'woo-gutenberg-products-block'
) }
value={ `${ selectedCountry }${
Expand Down Expand Up @@ -214,7 +192,7 @@ const Form = ( {
{ ! countryHasStates && (
<TextControl
placeholder={ __(
'State / County',
'State',
'woo-gutenberg-products-block'
) }
value={ selectedState }
Expand Down

This file was deleted.

0 comments on commit 64eee99

Please sign in to comment.