Skip to content

Commit

Permalink
prettier pass
Browse files Browse the repository at this point in the history
  • Loading branch information
spersico committed Apr 10, 2023
1 parent b08bd4a commit e983c4d
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 253 deletions.
34 changes: 21 additions & 13 deletions components/Form/components/CountrySelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,35 @@ const ItemTemplate = ({ item }) => {
const secondaryName = item.names.length > 1 ? item.names[0] : '';
return (
<div className={styles.item}>
<div>{primaryName}<span className={styles.secondaryNames}>{secondaryName}</span></div>
</div >
<div>
{primaryName}
<span className={styles.secondaryNames}>{secondaryName}</span>
</div>
</div>
);
};

const buildCountryLookup = (countries = []) => {
return countries.map((country) => ({
...country, search: country.names
.join('||')
.toLowerCase()

...country,
search: country.names.join('||').toLowerCase(),
}));
};

const compareByName = (country = {}, query = '') => country.search.includes(query);
const compareByName = (country = {}, query = '') =>
country.search.includes(query);

export default function CountrySelect({ value, countries, onChange }) {
const [selectedCountry, setSelectedCountry] = useState(null);
const [countriesLookup] = useState((buildCountryLookup(countries)));
const [countriesLookup] = useState(buildCountryLookup(countries));
const [filteredCountries, setFilteredCountries] = useState([]);

const searchCountry = (event) => {
const query = event.query.trim().toLowerCase();

const result = query ?
countriesLookup.filter((country) => compareByName(country, query)) : [...countriesLookup];
const result = query
? countriesLookup.filter((country) => compareByName(country, query))
: [...countriesLookup];
setFilteredCountries(result);
};

Expand All @@ -48,7 +51,7 @@ export default function CountrySelect({ value, countries, onChange }) {
}, [selectedCountry]);

return (
<div >
<div>
<label htmlFor='countryPicker'>Country</label>
<AutoComplete
id='countryPicker'
Expand All @@ -60,10 +63,15 @@ export default function CountrySelect({ value, countries, onChange }) {
dropdownAutoFocus
onBlur={(event) => {
const value = event.target.value;
if (!value) { setSelectedCountry(null); return; };
if (!value) {
setSelectedCountry(null);
return;
}
if (!selectedCountry?.code && typeof value === 'string') {
const query = value.trim().toLowerCase();
const maybeCountry = countriesLookup.find((country) => compareByName(country, query));
const maybeCountry = countriesLookup.find((country) =>
compareByName(country, query)
);
setSelectedCountry(maybeCountry);
}
}}
Expand Down
42 changes: 21 additions & 21 deletions components/Form/components/DateSelect.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { Calendar } from 'primereact/calendar';

export default function DateSelect({
firstDate,
setDate,
selectedDate,
disabled = true,
firstDate,
setDate,
selectedDate,
disabled = true,
}) {
return (
<div>
<label htmlFor='monthPicker'>Starting Date</label>
<Calendar
inputId='monthPicker'
value={selectedDate}
disabled={disabled}
onChange={(e) => setDate(e.value)}
view='month'
minDate={new Date(`${firstDate[0]}-${firstDate[1]}-15`)}
maxDate={new Date()}
dateFormat='mm/yy'
tooltipOptions={{ position: 'mouse' }}
tooltip="Since when do you have that income?"
/>
</div>
);
return (
<div>
<label htmlFor='monthPicker'>Starting Date</label>
<Calendar
inputId='monthPicker'
value={selectedDate}
disabled={disabled}
onChange={(e) => setDate(e.value)}
view='month'
minDate={new Date(`${firstDate[0]}-${firstDate[1]}-15`)}
maxDate={new Date()}
dateFormat='mm/yy'
tooltipOptions={{ position: 'mouse' }}
tooltip='Since when do you have that income?'
/>
</div>
);
}
36 changes: 16 additions & 20 deletions components/Form/components/SalaryInput.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { InputNumber } from 'primereact/inputnumber';

export default function SalaryInput({
salary,
setSalary,
selectedCountry,
}) {
return (
<div>
<label htmlFor='salaryInput'>Monthly Income</label>
<InputNumber
inputId="salaryInput"
value={salary}
onValueChange={(e) => setSalary(e.value)}
mode="currency"
currency={selectedCountry?.currencyCode || 'USD'}
disabled={!selectedCountry}
tooltipOptions={{ position: 'bottom', appendTo: 'self' }}
tooltip="What&apos;s your monthly income, in the selected country currency?"
/>
</div>
);
export default function SalaryInput({ salary, setSalary, selectedCountry }) {
return (
<div>
<label htmlFor='salaryInput'>Monthly Income</label>
<InputNumber
inputId='salaryInput'
value={salary}
onValueChange={(e) => setSalary(e.value)}
mode='currency'
currency={selectedCountry?.currencyCode || 'USD'}
disabled={!selectedCountry}
tooltipOptions={{ position: 'bottom', appendTo: 'self' }}
tooltip="What's your monthly income, in the selected country currency?"
/>
</div>
);
}
18 changes: 12 additions & 6 deletions components/Form/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ import DateSelect from './components/DateSelect.js';
import SalaryInput from './components/SalaryInput.js';

export default function Form({ countries }) {
const { salary, setSalary, setCountry, setDate, firstDate, selectedCountry, selectedDate, showResults } =
useFormData(countries);
const {
salary,
setSalary,
setCountry,
setDate,
firstDate,
selectedCountry,
selectedDate,
showResults,
} = useFormData(countries);

return (
<form className='p-fluid'>
Expand All @@ -36,10 +44,8 @@ export default function Form({ countries }) {
disabled={!selectedCountry}
/>
</div>
<Button onClick={showResults}>
Show me my buying power evolution
</Button>
</div >
<Button onClick={showResults}>Show me my buying power evolution</Button>
</div>
</form>
);
}
23 changes: 19 additions & 4 deletions components/Form/useFormData.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export default function useFormData(countries) {
const query = useSearchParams();

const [selectedCountry, setSelectedCountry] = useState(null);
const [firstDate, setSelectedCountryFirstDate] = useState([new Date().getFullYear(), '01']);
const [firstDate, setSelectedCountryFirstDate] = useState([
new Date().getFullYear(),
'01',
]);
const [selectedDate, setDate] = useState(null);
const [salary, setSalary] = useState(null);
const [loading, setLoading] = useState(false);
Expand All @@ -31,15 +34,27 @@ export default function useFormData(countries) {

const setCountry = (country) => {
setSelectedCountry(country);
setSelectedCountryFirstDate(country ? country.first.split('-') : [new Date().getFullYear(), '01']);
setSelectedCountryFirstDate(
country ? country.first.split('-') : [new Date().getFullYear(), '01']
);
};

const showResults = async (e) => {
e.preventDefault();
const [year, month] = [selectedDate.getFullYear(), selectedDate.getMonth() + 1];
const [year, month] = [
selectedDate.getFullYear(),
selectedDate.getMonth() + 1,
];
const code = selectedCountry.code;
setLoading(true);
router.push(`/results?${new URLSearchParams({ code, year, month, salary }).toString()}`);
router.push(
`/results?${new URLSearchParams({
code,
year,
month,
salary,
}).toString()}`
);
};

return {
Expand Down
Loading

1 comment on commit e983c4d

@vercel
Copy link

@vercel vercel bot commented on e983c4d Apr 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

raise-calc – ./

raise-calc-git-master-spersico.vercel.app
raise-calc.vercel.app
raise-calc-spersico.vercel.app

Please sign in to comment.