Skip to content

Commit 02db6d6

Browse files
authored
Merge pull request #126 from codeit-13-3team/feature/compare-storage
feat: 비교하기 상품 localStorage에 저장
2 parents ec2ef80 + 91c4b79 commit 02db6d6

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

src/pages/compare.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ const Compare = () => {
4444
gcTime: 0,
4545
});
4646

47+
useEffect(() => {
48+
const productA = JSON.parse(localStorage.getItem('productA')!);
49+
const productB = JSON.parse(localStorage.getItem('productB')!);
50+
if (productA !== null) {
51+
setProductA(productA);
52+
}
53+
if (productB !== null) {
54+
setProductB(productB);
55+
}
56+
}, []);
57+
4758
useEffect(() => {
4859
const handleClickOutside = (e: MouseEvent) => {
4960
if (
@@ -76,12 +87,14 @@ const Compare = () => {
7687

7788
const handleProductA = (item: Product) => {
7889
setProductA(item);
90+
localStorage.setItem('productA', JSON.stringify(item));
7991
setProductAName('');
8092
setHasCompare(false);
8193
setIsProductADropdownOpen(false);
8294
};
8395
const handleProductB = (item: Product) => {
8496
setProductB(item);
97+
localStorage.setItem('productB', JSON.stringify(item));
8598
setProductBName('');
8699
setHasCompare(false);
87100
setIsProductBDropdownOpen(false);
@@ -94,13 +107,15 @@ const Compare = () => {
94107
const colorClass =
95108
totals.A === totals.B ? 'text-gray-50' : totals.A > totals.B ? 'text-green' : 'text-pink';
96109

97-
const handleDeleteProductA = (product: Product) => {
110+
const handleDeleteProductA = () => {
98111
setProductA(null);
112+
localStorage.removeItem('productA');
99113
setHasCompare(false);
100114
};
101115

102-
const handleDeleteProductB = (product: Product) => {
116+
const handleDeleteProductB = () => {
103117
setProductB(null);
118+
localStorage.removeItem('productB');
104119
setHasCompare(false);
105120
};
106121

0 commit comments

Comments
 (0)