-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add product to favourites #16
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
107ee46
Add product to favourites
ihorhladkov 64a34a8
fixed function, add item count, use local storage
ihorhladkov 58b9562
deleted comments, rename hook
ihorhladkov b1305ba
Delete src/hooks/useLocalStorage.ts
ihorhladkov 74406f2
Merge branch 'master' into add-card-to-favourites
MaxSchmide d8f85f4
Update Card.tsx
MaxSchmide 529b317
resolve conflict
MaxSchmide File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useEffect } from 'react'; | ||
import { getInitialFavourites, useAppDispatch } from '../redux/'; | ||
|
||
export const useInitFavourites = () => { | ||
const dispatch = useAppDispatch(); | ||
|
||
useEffect(() => { | ||
dispatch(getInitialFavourites()); | ||
}, []); | ||
}; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,31 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
// import React, { useEffect, useState } from 'react'; | ||
import React from 'react'; | ||
import { Card } from '../components/Card'; | ||
import axios from 'axios'; | ||
import { IProduct } from '../types/Product'; | ||
// import axios from 'axios'; | ||
// import { IProduct } from '../types/Product'; | ||
import { useAppSelector } from '../redux'; | ||
|
||
export const FavouritesPage: React.FC = () => { | ||
// #region rewrite to Redux | ||
const [products, setProducts] = useState<IProduct[]>([]); | ||
const favouriteItems = useAppSelector(state => state.favourites.favouriteItems); | ||
|
||
// eslint-disable-next-line no-console | ||
// const [products, setProducts] = useState<IProduct[]>([]); | ||
// #endregion | ||
|
||
// #region rewrite to RTQ Query | ||
useEffect(() => { | ||
axios | ||
.get('https://dreamteam.onrender.com/products', { | ||
params: { page: 1, perPage: 8 }, | ||
}) | ||
.then((res) => { | ||
setProducts(res.data.data); | ||
}) | ||
.catch((e) => { | ||
throw new Error(e); | ||
}); | ||
}, []); | ||
// useEffect(() => { | ||
// axios | ||
// .get('https://dreamteam.onrender.com/products', { | ||
// params: { page: 1, perPage: 8 }, | ||
// }) | ||
// .then((res) => { | ||
// setProducts(res.data.data); | ||
// }) | ||
// .catch((e) => { | ||
// throw new Error(e); | ||
// }); | ||
// }, []); | ||
// #endregion | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please delete comments |
||
|
||
return ( | ||
|
@@ -30,11 +35,11 @@ export const FavouritesPage: React.FC = () => { | |
FavouritesPage | ||
</h1> | ||
<p className="text-sm mb-8 tablet:mb-9 font-semibold leading-[21px] text-secondary"> | ||
5 items | ||
{`${favouriteItems.length} items`} | ||
</p> | ||
</header> | ||
<div className="grid grid-cols-1 gap-4 tablet:grid-cols-2 desktop:grid-cols-4"> | ||
{products.map((pr) => ( | ||
{favouriteItems.map((pr) => ( | ||
<Card product={pr} key={pr._id} /> | ||
))} | ||
</div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
import { IProduct } from '../../types/Product'; | ||
|
||
type InitialState = { | ||
favouriteItems: IProduct[]; | ||
}; | ||
|
||
const initialState: InitialState = { | ||
favouriteItems: [], | ||
}; | ||
|
||
export const favouritesSlice = createSlice({ | ||
name: 'favourites', | ||
initialState, | ||
reducers: { | ||
getInitialFavourites: (state) => { | ||
state.favouriteItems = JSON.parse( | ||
localStorage.getItem('favourite-items') || '[]', | ||
) as IProduct[]; | ||
}, | ||
toggleFavourite: (state, action) => { | ||
const hasProductId = state.favouriteItems.some( | ||
(favouriteItem) => favouriteItem._id === action.payload._id, | ||
); | ||
|
||
if (hasProductId) { | ||
state.favouriteItems = state.favouriteItems.filter( | ||
(favouriteItem) => favouriteItem._id !== action.payload._id, | ||
); | ||
localStorage.setItem('favourite-items', JSON.stringify(state.favouriteItems)); | ||
} else { | ||
state.favouriteItems.push(action.payload); | ||
localStorage.setItem('favourite-items', JSON.stringify(state.favouriteItems)); | ||
} | ||
}, | ||
}, | ||
}); | ||
|
||
export const { toggleFavourite, getInitialFavourites } = favouritesSlice.actions; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GJ, but, please, rename file to your hook name