Skip to content

Commit

Permalink
feat: catalog-api
Browse files Browse the repository at this point in the history
  • Loading branch information
maik791277 committed Nov 14, 2023
1 parent d150d52 commit db47be2
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 22 deletions.
13 changes: 12 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ import PopupRegistration from '@components/popups/popup-registration';
function App() {
const { isLoggedIn } = useAuth();

// useEffect(() => {
// api.productsList("?category=nuts-dried-fruits")
// .then((data) => {
// console.log(data)
// })
// api.categoriesList()
// .then((data) => {
// console.log(data)
// })
// }, [])

return (
<div className="app">
<Layout>
Expand All @@ -31,7 +42,7 @@ function App() {
path={URLS.PROFILE}
element={<ProtectedRoute element={Profile} loggedIn={isLoggedIn} />}
/>
<Route path="/catalog/:category/:subcategory/:id" element={<Product />} />
<Route path="/catalog/:category/:id" element={<Product />} />
</Routes>
</Layout>
<PopupLogin />
Expand Down
5 changes: 3 additions & 2 deletions src/components/navigation/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React from 'react';
import { Link } from 'react-router-dom';
import styles from './navigation.module.scss';
import Button from '@components/Button';

const Navigation: React.FC = () => {
return (
<nav className={styles.navigation}>
<ul className={styles.navigation__list}>
<Button buttonText="Каталог" buttonStyle="header-button"></Button>
<Link className={styles.navigation_catalog} to={'/catalog'}>
Каталог
</Link>
<Link className={`${styles.navigation__item} `} to={''}>
О нас
</Link>
Expand Down
30 changes: 30 additions & 0 deletions src/components/navigation/navigation.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,36 @@
align-items: center;
}

.navigation_catalog {
@extend %zero-padding-margin;

display: flex;
height: 51px;
width: 117px;
padding: 8px 16px;
justify-content: center;
align-items: center;
color: #1a1a1a;
border-radius: 16px;
border: 1px solid var(--primary-700, #2b7a0f);
box-sizing: border-box;
text-decoration: none;
transition: 0.3s;

/* text */
font-family: $ubuntu-font;
font-size: 22px;
font-style: normal;
font-weight: 500;
line-height: 140%;

&:hover {
background: $accent-color-bright-green;
border: none;
color: white;
}
}

.navigation__item {
color: #1a1a1a;
text-align: center;
Expand Down
6 changes: 3 additions & 3 deletions src/components/product-card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ type ProductCardProps = {

const ProductCard = ({ cardName, price, weight, buttonText }: ProductCardProps) => {
return (
<li className={styles.card}>
<div className={styles.card}>
{/* вместо /login подставить нужный роут */}
{/*Предполагается в будущем открывать страницу товара по пути id и категории*/}
<Link className={styles.card__link} to={`catalog/category/subcategory/id`}>
<Link className={styles.card__link} to={`category/id`}>
<img className={styles.card__image} src={cardImage} alt="карточка товара" />
<h2 className={styles.card__caption}>{cardName}</h2>
</Link>
Expand All @@ -27,7 +27,7 @@ const ProductCard = ({ cardName, price, weight, buttonText }: ProductCardProps)
<button className={styles['card__cart-button']}>{buttonText}</button>
<button className={styles['card__like-button']} />
</div>
</li>
</div>
);
};

Expand Down
8 changes: 4 additions & 4 deletions src/data/dataExamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ export const mainPageBlockLinks = [
{
title: 'Овощи',
link: 'vegetables',
backgroundImage: '/4e039f6d2c33797f4fd913bd642549f0.jpeg',
backgroundImage: '/383bea75365411fd84f9c6c47e081cab.jpeg',
},
{
title: 'Фрукты',
link: 'fruits',
backgroundImage: '/5b5f8ca8a8f4f583ac88b6e80a646e10.jpeg',
backgroundImage: '/e8ab3ec81c9d3e4cc473a3d6ae86bc5a.jpeg',
},
{
title: 'Орехи',
Expand All @@ -19,12 +19,12 @@ export const mainPageBlockLinks = [
{
title: 'Молочные продукты',
link: 'diary',
backgroundImage: '/383bea75365411fd84f9c6c47e081cab.jpeg',
backgroundImage: '/5b5f8ca8a8f4f583ac88b6e80a646e10.jpeg',
},
{
title: 'Мясо и птица',
link: 'meat',
backgroundImage: '/e8ab3ec81c9d3e4cc473a3d6ae86bc5a.jpeg',
backgroundImage: '/4e039f6d2c33797f4fd913bd642549f0.jpeg',
},
];

Expand Down
1 change: 0 additions & 1 deletion src/hooks/use-form-and-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export function useFormAndValidation(): FormAndValidationHook {
}));
}
} else {
console.log('a');
setErrors({ ...errors, [name]: e.target.validationMessage });
}

Expand Down
30 changes: 22 additions & 8 deletions src/pages/catalog/index.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import styles from './catalog.module.scss';
import SliderComponent from '@components/slider-component';
import CardCatalogLink from '@components/card-catalog-link';
import { products } from '@data/dataExamples';
import api from '@services/api.ts';

type Catalog = {
id: number;
name: string;
};

const Catalog: React.FC = () => {
const [catalogs, setCatalogs] = useState<Catalog[]>([]);

useEffect(() => {
api.categoriesList().then((data) => {
setCatalogs(data);
});
}, []);
return (
<>
<SliderComponent />
<section className={styles.catalogBlock}>
<div className={styles.catalog}>
<h1>Каталог товаров</h1>
<div className={styles.catalog__cardlist}>
<CardCatalogLink title="Овощи" type="single-row" array={products} />
<CardCatalogLink title="Фрукты" type="single-row" array={products} />
<CardCatalogLink
title="Молочные продукты"
type="single-row"
array={products}
/>
{catalogs.map((catalog) => (
<CardCatalogLink
key={catalog.id}
title={catalog.name}
type="single-row"
array={products}
/>
))}
</div>
</div>
</section>
Expand Down
14 changes: 11 additions & 3 deletions src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ class Api {
}

/* ----------------------------- Products ----------------------------- */
productsList() {
return this._request('products/', {
productsList(slug: string) {
return this._request(`products${'/' + slug}`, {
method: 'GET',
});
}
Expand Down Expand Up @@ -313,6 +313,10 @@ class Api {
/* -------------------------- FavoriteProducts -------------------------- */
favoriteProductsList() {
return this._request('favorite-products/', {
headers: {
'Content-Type': 'application/json',
Authorization: `Token ${Cookies.get('token')}`,
},
method: 'GET',
});
}
Expand Down Expand Up @@ -358,7 +362,11 @@ class Api {

/* ----------------------------- Categories ----------------------------- */
categoriesList() {
return this._request('categories/', {
return this._request('categories', {
headers: {
'Content-Type': 'application/json',
Authorization: `Token ${Cookies.get('token')}`,
},
method: 'GET',
});
}
Expand Down

0 comments on commit db47be2

Please sign in to comment.