Skip to content

Commit

Permalink
frontend: rename fn names w/ 'fiat' to 'currency' in RatesProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
shonsirsha committed Mar 19, 2024
1 parent 59127c8 commit 85e72c7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
6 changes: 3 additions & 3 deletions frontends/web/src/components/rates/rates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ export const DefaultCurrencyRotator = ({
className = '',
tableRow = true
}: TDefaultCurrencyRotator) => {
const { rotateFiat, defaultCurrency } = useContext(RatesContext);
const { rotateDefaultCurrency, defaultCurrency } = useContext(RatesContext);
const displayedCurrency = activeUnit ? activeUnit : defaultCurrency;
const textStyle = `${className} ${style.rotatable}`;
if (!tableRow) {
return <span className={textStyle} onClick={rotateFiat}>{displayedCurrency}</span>;
return <span className={textStyle} onClick={rotateDefaultCurrency}>{displayedCurrency}</span>;
}
return <td className={textStyle} onClick={rotateFiat}>{displayedCurrency}</td>;
return <td className={textStyle} onClick={rotateDefaultCurrency}>{displayedCurrency}</td>;
};

export const formattedCurrencies = currenciesWithDisplayName.map((fiat) => ({ label: `${fiat.displayName} (${fiat.currency})`, value: fiat.currency }));
Expand Down
8 changes: 4 additions & 4 deletions frontends/web/src/contexts/RatesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ type RatesContextProps = {
defaultCurrency: Fiat;
activeCurrencies: Fiat[];
btcUnit?: BtcUnit;
rotateFiat: () => Promise<void>;
selectFiat: (fiat: Fiat) => Promise<void>;
updateDefaultFiat: (fiat: Fiat) => void;
rotateDefaultCurrency: () => Promise<void>;
addToActiveCurrencies: (fiat: Fiat) => Promise<void>;
updateDefaultCurrency: (fiat: Fiat) => void;
updateRatesConfig: () => Promise<void>;
unselectFiat: (fiat: Fiat) => Promise<void>;
removeFromActiveCurrencies: (fiat: Fiat) => Promise<void>;
}

const RatesContext = createContext<RatesContextProps>({} as RatesContextProps);
Expand Down
30 changes: 16 additions & 14 deletions frontends/web/src/contexts/RatesProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,33 @@ export const RatesProvider = ({ children }: TProps) => {
}
};

const rotateFiat = async () => {
const rotateDefaultCurrency = async () => {
const index = activeCurrencies.indexOf(defaultCurrency);
const fiat = activeCurrencies[(index + 1) % activeCurrencies.length];
await updateDefaultFiat(fiat);
await updateDefaultCurrency(fiat);
};

const updateDefaultFiat = async (fiat: Fiat) => {
// sets default currency both in config (mainFiat)
// and in RatesContext context's (local) state
const updateDefaultCurrency = async (fiat: Fiat) => {
if (!activeCurrencies.includes(fiat)) {
selectFiat(fiat);
addToActiveCurrencies(fiat);
}
await setConfig({ backend: { mainFiat: fiat } });
setDefaultCurrency(fiat);
};

//this is a method to select a fiat to be
//added into the selected fiat list
const selectFiat = async (fiat: Fiat) => {
// this is a method to select / add a currency
// into the active currencies list
const addToActiveCurrencies = async (fiat: Fiat) => {
const selected = [...activeCurrencies, fiat];
await setConfig({ backend: { fiatList: selected } });
handleChangeSelectedFiat(selected);
};

//this is a method to unselect a fiat to be
//removed from the selected fiat list
const unselectFiat = async (fiat: Fiat) => {
// this is a method to unselect / remove a currency
// from the active currencies list
const removeFromActiveCurrencies = async (fiat: Fiat) => {
const selected = activeCurrencies.filter(item => !equal(item, fiat));
await setConfig({ backend: { fiatList: selected } });
handleChangeSelectedFiat(selected);
Expand All @@ -91,11 +93,11 @@ export const RatesProvider = ({ children }: TProps) => {
defaultCurrency,
activeCurrencies,
btcUnit,
rotateFiat,
selectFiat,
updateDefaultFiat,
rotateDefaultCurrency,
addToActiveCurrencies,
updateDefaultCurrency,
updateRatesConfig,
unselectFiat
removeFromActiveCurrencies
}}
>
{children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { RatesContext } from '../../../../contexts/RatesContext';

export const DefaultCurrencyDropdownSetting = () => {
const { t } = useTranslation();
const { selectFiat, updateDefaultFiat, defaultCurrency, activeCurrencies } = useContext(RatesContext);
const { addToActiveCurrencies, updateDefaultCurrency, defaultCurrency, activeCurrencies } = useContext(RatesContext);
const valueLabel = currenciesWithDisplayName.find(fiat => fiat.currency === defaultCurrency)?.displayName;
const defaultValueLabel = valueLabel ? `${valueLabel} (${defaultCurrency})` : defaultCurrency;
return (
Expand All @@ -36,9 +36,9 @@ export const DefaultCurrencyDropdownSetting = () => {
<SingleDropdown
options={formattedCurrencies}
handleChange={async (fiat: Fiat) => {
updateDefaultFiat(fiat);
updateDefaultCurrency(fiat);
if (!activeCurrencies.includes(fiat)) {
await selectFiat(fiat);
await addToActiveCurrencies(fiat);
}
}}
defaultValue={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const ActiveCurrenciesDropdown = ({
const [search, setSearch] = useState('');
const { t } = useTranslation();

const { unselectFiat, selectFiat } = useContext(RatesContext);
const { removeFromActiveCurrencies, addToActiveCurrencies } = useContext(RatesContext);

useEffect(() => {
if (activeCurrencies.length > 0) {
Expand Down Expand Up @@ -77,12 +77,12 @@ export const ActiveCurrenciesDropdown = ({
switch (meta.action) {
case 'select-option':
if (meta.option) {
await selectFiat(meta.option.value);
await addToActiveCurrencies(meta.option.value);
}
break;
case 'deselect-option':
if (meta.option && meta.option.value !== defaultCurrency) {
await unselectFiat(meta.option.value);
await removeFromActiveCurrencies(meta.option.value);
}
}
}}
Expand Down

0 comments on commit 85e72c7

Please sign in to comment.