Skip to content

Commit

Permalink
fix: geo location dropdown bugfixes in playground (#181)
Browse files Browse the repository at this point in the history
* fix: geo location dropdown bugfixes in playground

* feat: added 'PlaceholderMenuItem' component to display in case of no records for a dropdown list
  • Loading branch information
kabilansakthivelu authored Nov 18, 2024
1 parent 03b688c commit 01188b3
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Box, MenuItem, Select, Typography } from '@mui/material';
import { getAllCountries } from '@razorpay/i18nify-js';
import React, { useEffect, useState } from 'react';

import PlaceholderMenuItem from 'src/components/placeholderMenuItem';

const CountryDropdown = ({
label = 'Select Country',
value,
Expand Down Expand Up @@ -38,21 +40,23 @@ const CountryDropdown = ({
...selectStyle,
}}
>
{countryList.map((country) => (
<MenuItem key={country.code} value={country.code}>
<Box
sx={{
display: 'flex',
alignItems: 'center',
textOverflow: 'initial',
}}
>
<div width="30px">
{country.code} - {country.country_name}
</div>
</Box>
</MenuItem>
))}
{countryList.length === 0 ?
<PlaceholderMenuItem /> :
countryList.map((country) => (
<MenuItem key={country.code} value={country.code}>
<Box
sx={{
display: 'flex',
alignItems: 'center',
textOverflow: 'initial',
}}
>
<div width="30px">
{country.code} - {country.country_name}
</div>
</Box>
</MenuItem>
))}
</Select>
</>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './placeholderMenuItem';
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';
import { MenuItem } from '@mui/material';

import { MENU_ITEM_PLACEHOLDER_CONTENT } from 'src/constants/geo';

const PlaceholderMenuItem = (props) => {
const { content = MENU_ITEM_PLACEHOLDER_CONTENT } = props;
return <MenuItem disabled>{content}</MenuItem>;
};

export default PlaceholderMenuItem;
34 changes: 19 additions & 15 deletions packages/i18nify-playground/src/components/stateDropdown/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Box, MenuItem, Select, Typography } from '@mui/material';
import React from 'react';

import PlaceholderMenuItem from 'src/components/placeholderMenuItem';

const StateDropdown = ({ label = 'Select State', list, onChange, value }) => {
return (
<>
Expand All @@ -16,21 +18,23 @@ const StateDropdown = ({ label = 'Select State', list, onChange, value }) => {
mb: 4,
}}
>
{list.map((state) => (
<MenuItem key={state.code} value={state.code}>
<Box
sx={{
display: 'flex',
alignItems: 'center',
textOverflow: 'initial',
}}
>
<div width="30px">
{state.code} - {state.name}
</div>
</Box>
</MenuItem>
))}
{list.length === 0 ?
<PlaceholderMenuItem /> :
list.map((state) => (
<MenuItem key={state.code} value={state.code}>
<Box
sx={{
display: 'flex',
alignItems: 'center',
textOverflow: 'initial',
}}
>
<div width="30px">
{state.code} - {state.name}
</div>
</Box>
</MenuItem>
))}
</Select>
</>
);
Expand Down
1 change: 1 addition & 0 deletions packages/i18nify-playground/src/constants/geo/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const ALLOWED_COUNTRIES = ['IN', 'MY', 'FR', 'DE', 'US'];
export const MENU_ITEM_PLACEHOLDER_CONTENT = 'No data available';
37 changes: 23 additions & 14 deletions packages/i18nify-playground/src/pages/geo/getCities.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ALLOWED_COUNTRIES } from 'src/constants/geo';
import CodeEditor from 'src/components/codeEditor';
import CountryDropdown from 'src/components/countryDropdown';
import StateDropdown from 'src/components/stateDropdown';
import PlaceholderMenuItem from 'src/components/placeholderMenuItem';

// ----------------------------------------------------------------------

Expand All @@ -38,6 +39,9 @@ export default function GetCities() {

useEffect(() => {
setStateInp('');
setStateList([]);
setCityInp('');
setCities([]);
setCode('');
getStates(countryInp).then((res) => {
const states = Object.entries(res).map(([_code, state]) => ({
Expand All @@ -50,6 +54,8 @@ export default function GetCities() {

useEffect(() => {
if (!stateInp) return;
setCityInp('');
setCities([]);
getCities(countryInp, stateInp).then((res) => {
setCities(res);
setCityInp(res[0].name);
Expand Down Expand Up @@ -106,7 +112,7 @@ export default function GetCities() {
/>
<StateDropdown
value={stateInp}
onChange={(e) => setStateInp(e)}
onChange={(state) => setStateInp(state)}
list={stateList}
/>

Expand All @@ -119,20 +125,23 @@ export default function GetCities() {
marginRight: 1,
width: '100%',
}}
onChange={(e) => setCityInp(e.target.value || '')}
>
{cities.map((city) => (
<MenuItem key={city.name} value={city.name}>
<Box
sx={{
display: 'flex',
alignItems: 'center',
textOverflow: 'initial',
}}
>
<div width="30px">{city.name}</div>
</Box>
</MenuItem>
))}
{cities.length === 0 ?
<PlaceholderMenuItem /> :
cities.map((city) => (
<MenuItem key={city.name} value={city.name}>
<Box
sx={{
display: 'flex',
alignItems: 'center',
textOverflow: 'initial',
}}
>
<div width="30px">{city.name}</div>
</Box>
</MenuItem>
))}
</Select>
</Grid>
{!isMobile && (
Expand Down
6 changes: 4 additions & 2 deletions packages/i18nify-playground/src/pages/geo/getStates.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export default function GetStates() {
}, []);

useEffect(() => {
setStateVal('');
setStateList([]);
getStates(countryInp).then((res) => {
setCode(JSON.stringify(res, null, 2));
const data = Object.keys(res).map((stateCode) => ({
Expand Down Expand Up @@ -80,13 +82,13 @@ export default function GetStates() {
<CountryDropdown
value={countryInp}
list={countryList}
onChange={(e) => setCountryInp(e)}
onChange={(country) => setCountryInp(country)}
/>
<StateDropdown
label="List of States"
value={stateVal}
list={stateList}
onChange={(e) => setStateVal(e)}
onChange={(state) => setStateVal(state)}
/>
</Grid>
{!isMobile && (
Expand Down
37 changes: 23 additions & 14 deletions packages/i18nify-playground/src/pages/geo/getZipcodes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ALLOWED_COUNTRIES } from 'src/constants/geo';
import CodeEditor from 'src/components/codeEditor';
import CountryDropdown from 'src/components/countryDropdown';
import StateDropdown from 'src/components/stateDropdown';
import PlaceholderMenuItem from 'src/components/placeholderMenuItem';

// ----------------------------------------------------------------------

Expand All @@ -38,6 +39,9 @@ export default function GetZipcodes() {

useEffect(() => {
setStateInp('');
setStateList([]);
setZipcodeInp('');
setZipcodes([]);
setCode('');
getStates(countryInp).then((res) => {
const states = Object.entries(res).map(([_code, state]) => ({
Expand All @@ -50,6 +54,8 @@ export default function GetZipcodes() {

useEffect(() => {
if (!stateInp) return;
setZipcodeInp('');
setZipcodes([]);
getZipcodes(countryInp, stateInp).then((res) => {
setZipcodes(res);
setZipcodeInp(res[0]);
Expand Down Expand Up @@ -103,7 +109,7 @@ export default function GetZipcodes() {
/>
<StateDropdown
value={stateInp}
onChange={(e) => setStateInp(e)}
onChange={(state) => setStateInp(state)}
list={stateList}
/>

Expand All @@ -116,20 +122,23 @@ export default function GetZipcodes() {
marginRight: 1,
width: '100%',
}}
onChange={(e) => setZipcodeInp(e.target.value || '')}
>
{zipcodes.map((zipcode) => (
<MenuItem key={zipcode} value={zipcode}>
<Box
sx={{
display: 'flex',
alignItems: 'center',
textOverflow: 'initial',
}}
>
<div width="30px">{zipcode}</div>
</Box>
</MenuItem>
))}
{zipcodes.length === 0 ?
<PlaceholderMenuItem /> :
zipcodes.map((zipcode) => (
<MenuItem key={zipcode} value={zipcode}>
<Box
sx={{
display: 'flex',
alignItems: 'center',
textOverflow: 'initial',
}}
>
<div width="30px">{zipcode}</div>
</Box>
</MenuItem>
))}
</Select>
</Grid>
{!isMobile && (
Expand Down

0 comments on commit 01188b3

Please sign in to comment.