Skip to content
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

feat: implement button adding favourites to cart #175

Merged
merged 1 commit into from
Dec 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions src/pages/profile/profile-favorites/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import api from '@services/api';
import ProductCard from '@components/product-card';
import ReturnBackButton from '@components/profile-components/return-back-button';
import { useProfile } from '@hooks/use-profile';
import { useCart } from '@hooks/use-cart-context';
import type { Product } from '@services/generated-api/data-contracts';
import { Link } from 'react-router-dom';

Expand All @@ -14,11 +15,17 @@ export default function ProfileFavorites() {
error: '',
});
const [products, setProducts] = useState<Array<Product>>([]);
const [checkboxesValues, setCheckboxesValue] = useState<boolean[]>([]);
const [checkboxesValues, setCheckboxesValue] = useState<Record<number, boolean>>({});
const { isMobileScreen } = useProfile();
const { updateCart } = useCart();

useEffect(() => {
products && setCheckboxesValue(products.map(() => false));
products &&
products.forEach((product) => {
setCheckboxesValue((prev) => {
return { ...prev, [product.id]: false };
});
});
}, [products]);

useEffect(() => {
Expand Down Expand Up @@ -46,18 +53,40 @@ export default function ProfileFavorites() {
});
}, []);

const onCheckButton = (index: number) => {
const onCheckButton = (id: number) => {
return () => {
setCheckboxesValue(
checkboxesValues.map((value, i) => (i === index ? !value : value))
);
setCheckboxesValue((prev) => {
return { ...prev, [id]: !prev[id] };
});
};
};

const isChooseAll = checkboxesValues.every((el) => el) || !products.length;
const isChooseAll =
Object.values(checkboxesValues).every((el) => el) || !products.length;

const toggleAll = () => {
setCheckboxesValue(checkboxesValues.map(() => (isChooseAll ? false : true)));
if (isChooseAll) {
Object.keys(checkboxesValues).forEach((key) => {
setCheckboxesValue((prev) => {
return { ...prev, [key]: false };
});
});
} else {
Object.keys(checkboxesValues).forEach((key) => {
setCheckboxesValue((prev) => {
return { ...prev, [key]: true };
});
});
}
};

const handleAddToCart = () => {
const dataToSend = Object.keys(checkboxesValues)
.filter((i) => checkboxesValues[i as unknown as keyof typeof checkboxesValues])
.map((item) => ({ id: Number(item), quantity: 1 }));
updateCart(dataToSend);

window.scroll(0, 0);
};

return (
Expand All @@ -71,7 +100,7 @@ export default function ProfileFavorites() {
</div>
<ul className={styles.favorites__list}>
{!productsLoadingStatus.inProcess && products.length ? (
products.map((product, index) => (
products.map((product) => (
<li className={styles.favorites__item} key={product.id}>
<ProductCard
cardName={product.name}
Expand All @@ -82,10 +111,10 @@ export default function ProfileFavorites() {
idCard={product.id}
category={product?.category?.category_slug}
checkboxControl={
checkboxesValues.length
Object.keys(checkboxesValues)?.length
? {
checked: checkboxesValues[index],
onChange: onCheckButton(index),
checked: checkboxesValues[product.id],
onChange: onCheckButton(product.id),
}
: undefined
}
Expand All @@ -102,7 +131,13 @@ export default function ProfileFavorites() {
</p>
)}
</ul>
<button className={styles.button}>В корзину</button>
<button
className={styles.button}
onClick={handleAddToCart}
disabled={!Object.values(checkboxesValues).some((i) => i)}
>
В корзину
</button>
</div>
);
}
Loading