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

#210-api-inserted-to-StoriesBlock #228

Merged
merged 5 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/app/providers/StoreProvider/config/StateSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ShopNewsSchema } from '@/widgets/NewsBlock/model/types/types'
import { StoreReviewsSchema } from '@/widgets/ReviewsBlock/model/types/types'
import { IBlogPostsSchema } from '@/widgets/BlogBlock/model/types/types'
import { CoreBaseFooterSchema } from '@/widgets/Footer/model/types/types'
import { IStoriesSchema } from '@/widgets/StoriesBlock/model/types/types'
import { CoreBaseHeaderSchema } from '@/widgets/Header/model/types/types'
import { TProductSchema } from '@/pages/ProductPage/model/types/productTypes'

Expand All @@ -19,6 +20,7 @@ export interface StateSchema {
brand: BrandSchema
searchResult: SearchResultSchema
shopNews: ShopNewsSchema
stories: IStoriesSchema
blogPosts: IBlogPostsSchema
product: TProductSchema
}
Expand Down
5 changes: 3 additions & 2 deletions src/app/providers/StoreProvider/config/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { configureStore, ReducersMapObject } from '@reduxjs/toolkit'

import { loginReducer } from '@/features/login/model/slice/loginSlice'
import { $api } from '@/shared/api/api'
import categorySlice from '@/entities/Category/slice/categorySlice'
Expand All @@ -8,9 +7,9 @@ import searchProductSlice from '@/features/SearchProduct/slice/searchProductSlic
import { storeReviewsReducer } from '@/widgets/ReviewsBlock/model/slice/reviewsSlice'
import footerSlice from '@/widgets/Footer/model/slice/footerSlice'
import { shopNewsReducer } from '@/widgets/NewsBlock/model/slice/shopNewsSlice'
import { storiesReducer } from '@/widgets/StoriesBlock/model/slice/storiesSlice'
import { blogPostsReducer } from '@/widgets/BlogBlock/model/slice/blogPostsSlice'
import headerSlice from '@/widgets/Header/model/slice/headerSlice'

import { StateSchema, ThunkExtraArg } from './StateSchema'
import { productSliceReducer } from '@/pages/ProductPage/model/slice/productSlice'

Expand All @@ -25,8 +24,10 @@ const rootReducer: ReducersMapObject<RootState> = {
searchResult: searchProductSlice,
storeReviews: storeReviewsReducer,
shopNews: shopNewsReducer,
stories: storiesReducer,
blogPosts: blogPostsReducer,
product: productSliceReducer

}

export function createReduxStore(initialState: RootState) {
Expand Down
3 changes: 1 addition & 2 deletions src/entities/BlogCard/BlogCard.module.scss
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
@use '@/app/styles/index' as var;

.card {
max-width: 340px;
min-width: 340px;
width: 340px;
position: relative;
transition: transform 0.3s ease-in-out;
display: flex;
Expand Down
3 changes: 1 addition & 2 deletions src/entities/NewsCard/NewsCard.module.scss
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
@use '@/app/styles/index' as var;

.card {
max-width: 340px;
min-width: 340px;
width: 340px;
position: relative;
transition: transform 0.3s ease-in-out;
display: flex;
Expand Down
11 changes: 9 additions & 2 deletions src/entities/StoryCard/StoryCard.module.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@use '@/app/styles/index' as var;

.card {

.card {
width: 160px;
position: relative;
transition: transform 0.3s ease-in-out;
display: flex;
Expand All @@ -13,9 +15,14 @@
transition: transform 0.3s ease-in-out;
}

img {

.img {
height: 240px;
width: 100%;
border-radius: 6px;
transition: transform 0.3s ease-in-out;
scroll-snap-align: start;
object-fit: cover;
}
}

7 changes: 2 additions & 5 deletions src/entities/StoryCard/StoryCard.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ type Story = StoryObj<typeof meta>

export const Default: Story = {
args: {
card: {
id: 1,
src: Img1,
alt: 'Stock image'
}
pictures: [Img1],
link: 'https://gealit.ru/api/catalogue/3w-clinic--krem-dlya-glaz-s-ekstraktom-chernogo-zhemchuga-black-pearl-eye-cream-whitening-40-ml-410158271/'
}
}
20 changes: 13 additions & 7 deletions src/entities/StoryCard/StoryCard.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import { FC } from 'react'

import { TCard } from '@/models/CardModel'
import NoImage from '@/assets/icons/image-not-found-icon.svg'
import Link from '@/shared/ui/Link/Link'

import styles from './StoryCard.module.scss'

export type Props = {
card: TCard
type TProps = {
link: string
pictures: string[]
}

/**
* Карточка из блока группы историй
* @param {TCard} card - параметры карточки из группы историй
* @param {string} link - ссылка, которая будет, вероятно, вести на страницу истории
* @param {string[]} pictures - массив картинок, относящийся к одной story
*/

const StoryCard: FC<Props> = ({ card }) => {
const StoryCard: FC<TProps> = ({ link, pictures }) => {
return (
<Link to={''} className={styles.card}>
<img src={card.src} alt={card.alt} draggable="false" />
<Link to={link} className={styles.card}>
{pictures[0] ? (
<img src={pictures[0]} alt={'новость'} draggable="false" className={styles.img} />
) : (
<NoImage className={styles.img} />
)}
</Link>
)
}
Expand Down
2 changes: 2 additions & 0 deletions src/shared/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export enum ApiRoutes {
SHOP_NEWS = 'shopnews',
BLOG_POSTS = 'shopblog/posts',
CORE_BASE = 'core/base',
STORIES = 'stories',
PRODUCT = 'catalogue'

}

export enum ApiErrorTypes {
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/BlogBlock/ui/BlogBlock.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
width: 1.25rem;
height: 0.5625rem;
margin-left: 0.5rem;
vertical-align: middle;
vertical-align: middle;
}

ul {
Expand Down
4 changes: 3 additions & 1 deletion src/widgets/NewsBlock/model/slice/shopNewsSlice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createSlice } from '@reduxjs/toolkit'

import { rejectedPayloadHandle } from '@/shared/api/rejectedPayloadHandle'
import { REDUCER_SHOP_NEWS } from '@/shared/constants/constants'

import { getShopNews } from '../services/getShopNews'
Expand All @@ -23,8 +24,9 @@ export const shopNewsSlice = createSlice({
state.isLoading = false
state.news = payload
})
.addCase(getShopNews.rejected, state => {
.addCase(getShopNews.rejected, (state, { payload }) => {
state.isLoading = false
state.error = rejectedPayloadHandle(payload)
})
}
})
Expand Down
1 change: 1 addition & 0 deletions src/widgets/NewsBlock/model/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export interface ShopNewsData {
export interface ShopNewsSchema {
isLoading: boolean
news: ShopNewsData[]
error?: string | string[]
}
5 changes: 4 additions & 1 deletion src/widgets/ReviewsBlock/model/slice/reviewsSlice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { createSlice } from '@reduxjs/toolkit'

import { rejectedPayloadHandle } from '@/shared/api/rejectedPayloadHandle'

import { getStoreReviews } from '../services/getStoreReviews'
import { StoreReviewsSchema } from '../types/types'

Expand All @@ -25,8 +27,9 @@ export const reviewsSlice = createSlice({
state.reviews = payload
//state.reviews = payload.test2 // StoreeviewsData с сервера переклыдвается в StoreReviewsSchema (наше Redux хранилище)
})
.addCase(getStoreReviews.rejected, state => {
.addCase(getStoreReviews.rejected, (state, { payload }) => {
state.isLoading = false
state.error = rejectedPayloadHandle(payload)
})
}
})
Expand Down
1 change: 1 addition & 0 deletions src/widgets/ReviewsBlock/model/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export interface StoreReviewReplay {
export interface StoreReviewsSchema {
isLoading: boolean
reviews: StoreReviewData[]
error?: string | string[]
}
5 changes: 5 additions & 0 deletions src/widgets/StoriesBlock/model/selectors/selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { StateSchema } from '@/app/providers/StoreProvider'

export const getStoriesSelector = (state: StateSchema) => {
return state.stories.stories
}
20 changes: 20 additions & 0 deletions src/widgets/StoriesBlock/model/services/getStories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createAsyncThunk } from '@reduxjs/toolkit'

import { ThunkConfig } from '@/app/providers/StoreProvider/config/StateSchema'
import { apiErrorIdentify } from '@/shared/api/apiErrorIdentify'
import { ApiError, ApiErrorTypes, ApiRoutes } from '@/shared/api/types'

import { IStoriesData } from '../types/types'

export const getStories = createAsyncThunk<IStoriesData[], void, ThunkConfig<ApiError>>(
'stories',
async (_, thunkAPI) => {
const { rejectWithValue, extra } = thunkAPI
try {
const { data } = await extra.api.get(ApiRoutes.STORIES)
return data.results
} catch (error) {
return rejectWithValue(apiErrorIdentify(error, ApiErrorTypes.DATA_EMPTY_ERROR))
}
}
)
33 changes: 33 additions & 0 deletions src/widgets/StoriesBlock/model/slice/storiesSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createSlice } from '@reduxjs/toolkit'

import { rejectedPayloadHandle } from '@/shared/api/rejectedPayloadHandle'

import { getStories } from '../services/getStories'
import { IStoriesSchema } from '../types/types'

const initialState: IStoriesSchema = {
isLoading: false,
stories: []
}

export const storiesSlice = createSlice({
name: 'stories',
initialState,
reducers: {},
extraReducers: builder => {
builder
.addCase(getStories.pending, state => {
state.isLoading = true
})
.addCase(getStories.fulfilled, (state, { payload }) => {
state.isLoading = false
state.stories = payload
})
.addCase(getStories.rejected, (state, { payload }) => {
state.isLoading = false
JuliaAvramenko marked this conversation as resolved.
Show resolved Hide resolved
state.error = rejectedPayloadHandle(payload)
})
}
})

export const { actions: storiesActions, reducer: storiesReducer } = storiesSlice
22 changes: 22 additions & 0 deletions src/widgets/StoriesBlock/model/types/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export interface IPaginatedResponse<T> {
count: number
previous: string
next: string
results: T[]
}
export interface IStoriesPicturesData {
image: string
}

export interface IStoriesData {
id: number
name: string
link: string
pictures: IStoriesPicturesData[]
}

export interface IStoriesSchema {
isLoading: boolean
stories: IStoriesData[]
error?: string | string[]
}
19 changes: 15 additions & 4 deletions src/widgets/StoriesBlock/ui/StoriesBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
import { FC } from 'react'
import { FC, useEffect } from 'react'
import { useSelector } from 'react-redux'

import StoryCard from '@/entities/StoryCard/StoryCard'
import { storiesData } from '@/mockData/storiesData'
import { useAppDispatch } from '@/shared/libs/hooks/store'
import Heading, { HeadingType } from '@/shared/ui/Heading/Heading'
import Scroll from '@/shared/ui/Scroll/Scroll'

import { getStoriesSelector } from '../model/selectors/selectors'
import { getStories } from '../model/services/getStories'

import styles from './StoriesBlock.module.scss'

/**
* Блок группы историй
*/
const StoriesBlock: FC = () => {
const dispatch = useAppDispatch()
const stories = useSelector(getStoriesSelector)

useEffect(() => {
dispatch(getStories())
}, [])

return (
<section className={styles.wrapper}>
<article>
<Heading type={HeadingType.NORMAL}>Истории</Heading>
</article>
<Scroll>
{storiesData.map(item => (
<StoryCard key={item.id} card={item} />
{stories.map(item => (
<StoryCard key={item.id} link={item.link} pictures={item.pictures.map(item => item.image)} />
))}
</Scroll>
</section>
Expand Down
Loading