Skip to content

Commit f50ae75

Browse files
committed
add currency starring feature
1 parent fed61eb commit f50ae75

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

src/components/CalculationView/CalculationView.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import clsx from "clsx";
33

44
import { convert } from "../../utils/convert";
55
import { CurrencyMapContext } from "../../context/CurrencyMapContext";
6+
import { StorageContext } from "../../context/StorageContext";
67

78
import { Gears } from "../Gears";
89
import { CurrencySelection } from "./CurrencySelection";
@@ -13,7 +14,8 @@ import { HiddenDataInfo } from "./HiddenDataInfo";
1314
import { currencies } from "../../constant";
1415

1516
export function CalculationView() {
16-
const [selected, setSelected] = useState<CurrencyKey | "">("");
17+
const { preferences } = useContext(StorageContext);
18+
const [selected, setSelected] = useState<CurrencyKey | "">(preferences.starred ?? "");
1719
const [value, setValue] = useState("");
1820
const currencyMap = useContext(CurrencyMapContext);
1921

src/components/CalculationView/CurrencyInput.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
import type { ChangeEvent } from "react";
2-
import { useCallback } from "react";
2+
import { useCallback, useContext } from "react";
3+
import { Star, StarOff } from "lucide-react";
34

45
import { Currency } from "../Currency";
56
import { Input } from "../shadcn/Input";
7+
import { StorageContext } from "../../context/StorageContext";
8+
9+
function StarButton({ currency }: { currency: CurrencyKey }) {
10+
const { setPreferences, preferences } = useContext(StorageContext);
11+
const isSelected = preferences.starred === currency;
12+
13+
return (
14+
<button
15+
title={`Select "${currency}" on page load`}
16+
className='ml-10'
17+
onClick={() => setPreferences(isSelected ? { starred: null } : { starred: currency })}>
18+
{isSelected ? <StarOff className='w-4 h-4' /> : <Star className='w-4 h-4' />}
19+
</button>
20+
);
21+
}
622

723
type Props = {
824
value: string;
@@ -36,6 +52,8 @@ export function CurrencyInput({ value, setValue, selected }: Props) {
3652
<span className='ml-[-5px]'>×</span>
3753
<Currency name={selected} />
3854
<span> equals</span>
55+
56+
<StarButton currency={selected} />
3957
</div>
4058
);
4159
}

src/context/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ interface IStorage {
22
cache: ReturnType<typeof getCache>;
33
setCache: (data: RateDefinitions) => void;
44
preferences: Preference;
5-
setPreferences: (pref: Preference) => void;
5+
setPreferences: (pref: Partial<Preference>) => void;
66
}

src/utils/storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const cacheKey = "currencyMapCache";
22
const preferencesKey = "preferences";
3-
const initialPreferences: Preference = { pinned: null };
3+
const initialPreferences: Preference = { pinned: null, starred: null };
44

55
export function getCache() {
66
const cacheData = localStorage.getItem(cacheKey);

src/utils/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
type Preference = { pinned: null | { primary: CurrencyKey; secondary: CurrencyKey } };
1+
type Preference = { pinned: null | { primary: CurrencyKey; secondary: CurrencyKey }; starred: CurrencyKey | null };

0 commit comments

Comments
 (0)