-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #189 from Studio-Yandex-Practicum/api-146-insert-t…
…o-ReviewsBlock #146-api-inserted-to-ReviewsBlock
- Loading branch information
Showing
12 changed files
with
153 additions
and
31 deletions.
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
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,5 @@ | ||
import { StateSchema } from '@/app/providers/SroreProvider' | ||
|
||
export const getStoreReviewsSelector = (state: StateSchema) => { | ||
return state.storeReviews.reviews | ||
} |
21 changes: 21 additions & 0 deletions
21
src/widgets/ReviewsBlock/model/services/getStoreReviews.ts
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,21 @@ | ||
import { createAsyncThunk } from '@reduxjs/toolkit' | ||
import { ThunkConfig } from '@/app/providers/SroreProvider/config/StateSchema' | ||
import { ApiError, ApiErrorTypes, ApiRoutes } from '@/shared/api/types' | ||
import { apiErrorIdentify } from '@/shared/api/apiErrorIdentify' | ||
import { StoreReviewData } from '../types/types' | ||
|
||
export const getStoreReviews = createAsyncThunk<StoreReviewData[], void, ThunkConfig<ApiError>>( | ||
//void1- выходные данные, void2- входные данные , thunkConfig- тип store | ||
'store-reviews', // action type, первый аргумент | ||
async (_, thunkAPI) => { | ||
// второй аргумент- асинхронная функция , кот вызовет dispatch в компоненте | ||
const { rejectWithValue, extra } = thunkAPI | ||
try { | ||
const { data } = await extra.api.get(ApiRoutes.STORE_REVIEWS) | ||
|
||
return data.results | ||
} catch (error) { | ||
return rejectWithValue(apiErrorIdentify(error, ApiErrorTypes.DATA_EMPTY_ERROR)) | ||
} | ||
} | ||
) |
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,33 @@ | ||
import { createSlice } from '@reduxjs/toolkit' | ||
import { StoreReviewsSchema } from '../types/types' | ||
import { getStoreReviews } from '../services/getStoreReviews' | ||
|
||
const initialState: StoreReviewsSchema = { | ||
isLoading: false, | ||
reviews: [] | ||
} | ||
|
||
export const reviewsSlice = createSlice({ | ||
name: 'storeReviews', | ||
initialState, | ||
reducers: { | ||
// для обычных actions, не thunk | ||
}, | ||
extraReducers: builder => { | ||
// для thunk actions | ||
builder | ||
.addCase(getStoreReviews.pending, state => { | ||
state.isLoading = true | ||
}) | ||
.addCase(getStoreReviews.fulfilled, (state, { payload }) => { | ||
state.isLoading = false | ||
state.reviews = payload | ||
//state.reviews = payload.test2 // StoreeviewsData с сервера переклыдвается в StoreReviewsSchema (наше Redux хранилище) | ||
}) | ||
.addCase(getStoreReviews.rejected, state => { | ||
state.isLoading = false | ||
}) | ||
} | ||
}) | ||
|
||
export const { actions: reviewsActions, reducer: storeReviewsReducer } = reviewsSlice |
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,29 @@ | ||
export interface GetStoreReviewsResponse { | ||
count: number | ||
previous: string | ||
next: string | ||
results: StoreReviewData[] | ||
} | ||
|
||
export interface StoreReviewData { | ||
pk: number | ||
text: string | ||
pub_date: string | ||
author_name: string | ||
author_email: string | ||
average_score: number | ||
delivery_speed_score: number | ||
quality_score: number | ||
price_score: number | ||
replay: StoreReviewReplay | ||
} | ||
export interface StoreReviewReplay { | ||
text: string | ||
pub_date: string | ||
name: string | ||
} | ||
|
||
export interface StoreReviewsSchema { | ||
isLoading: boolean | ||
reviews: StoreReviewData[] | ||
} |
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