File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments