Skip to content

Commit

Permalink
внедрить Api в footer
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturKhelshtein committed Feb 1, 2024
1 parent ec816fb commit 9f306e9
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 9 deletions.
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 @@ -4,11 +4,13 @@ import { LoginSchema } from '@/features/login/model/types/types'
import { BrandSchema } from '@/widgets/BrandBlock/types/types'
import { ApiInstance } from '@/shared/api/api'
import { StoreReviewsSchema } from '@/widgets/ReviewsBlock/model/types/types'
import { CoreBaseFooterSchema } from '@/widgets/Footer/model/types/types'

export interface StateSchema {
login: LoginSchema
storeReviews: StoreReviewsSchema
category: CategorySchema
coreBaseFooter: CoreBaseFooterSchema
brand: BrandSchema
searchResult: SearchResultSchema
}
Expand Down
2 changes: 2 additions & 0 deletions src/app/providers/StoreProvider/config/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import categorySlice from '@/entities/Category/slice/categorySlice'
import brandSlice from '@/widgets/BrandBlock/slice/brandSlice'
import searchProductSlice from '@/features/SearchProduct/slice/searchProductSlice'
import { storeReviewsReducer } from '@/widgets/ReviewsBlock/model/slice/reviewsSlice'
import footerSlice from '@/widgets/Footer/model/slice/footerSlice'

export type RootState = StateSchema

const rootReducer: ReducersMapObject<RootState> = {
login: loginReducer,
category: categorySlice,
coreBaseFooter: footerSlice,
brand: brandSlice,
searchResult: searchProductSlice,
storeReviews: storeReviewsReducer
Expand Down
3 changes: 2 additions & 1 deletion src/shared/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export enum ApiRoutes {
BRANDS = 'catalogue/brand',
SEARCH = 'search',
STORE_REVIEWS = 'store-reviews',
CATEGORIES = 'catalogue/category'
CATEGORIES = 'catalogue/category',
CORE_BASE = 'core/base'
}

export enum ApiErrorTypes {
Expand Down
28 changes: 20 additions & 8 deletions src/widgets/Footer/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import { useState } from 'react'
import { coreBaseData } from '@/mockData/coreBaseData'
import Logo from '@/shared/ui/logo/Logo'
import Link from '@/shared/ui/Link/Link'
import { Button } from '@/shared/ui/Button/Button'
import Modal from '@/shared/ui/Modal/Modal'
import { useState, useEffect } from 'react'
import { useDispatch, useSelector } from 'react-redux'

import { AppDispatch } from '@/app/providers/StoreProvider/config/store'
import Payments from '@/entities/Payments/Payments'
import SubscribeForm from '@/features/SubscribeForm/SubscribeForm'
import CallBack from '@/features/CallBack'
import styles from './footer.module.scss'
import SubscribeForm from '@/features/SubscribeForm/SubscribeForm'
import { Button } from '@/shared/ui/Button/Button'
import Link from '@/shared/ui/Link/Link'
import Logo from '@/shared/ui/Logo/Logo'
import Modal from '@/shared/ui/Modal/Modal'
import Paragraph from '@/shared/ui/Paragraph/Paragraph'

import styles from './footer.module.scss'
import { getCoreBaseFooterSelector } from './model/selectors/selectors'
import { getCoreBase } from './model/services/getCoreBase'

function Footer() {
const dispatch = useDispatch<AppDispatch>()
const coreBaseData = useSelector(getCoreBaseFooterSelector)
const [isModalOpen, setIsModalOpen] = useState(false)
const [isModalClosing, setIsModalClosing] = useState(false)

const changeModalState = () => {
setIsModalOpen(!isModalOpen)
}

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

const onSubmitHandler = () => {}
return (
<>
Expand Down
3 changes: 3 additions & 0 deletions src/widgets/Footer/model/selectors/selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { StateSchema } from '@/app/providers/StoreProvider'

export const getCoreBaseFooterSelector = (state: StateSchema) => state.coreBaseFooter
23 changes: 23 additions & 0 deletions src/widgets/Footer/model/services/getCoreBase.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 { CoreBaseFooterData } from '../types/types'

export const getCoreBase = createAsyncThunk<CoreBaseFooterData, void, ThunkConfig<ApiError>>(
//void1- выходные данные, void2- входные данные , thunkConfig- тип store
'core/base', // action type, первый аргумент
async (_, thunkAPI) => {
// второй аргумент- асинхронная функция , кот вызовет dispatch в компоненте
const { rejectWithValue, extra } = thunkAPI
try {
const response = await extra.api.get(`api/${ApiRoutes.CORE_BASE}`)

return response.data.footer as CoreBaseFooterData
} catch (error) {
return rejectWithValue(apiErrorIdentify(error, ApiErrorTypes.DATA_EMPTY_ERROR))
}
}
)
45 changes: 45 additions & 0 deletions src/widgets/Footer/model/slice/footerSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { createSlice } from '@reduxjs/toolkit'

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

import { getCoreBase } from '../services/getCoreBase'
import { CoreBaseFooterSchema } from '../types/types'

const initialState: CoreBaseFooterSchema = {
footer: {
company_info: '',
disclaimer: '',
support_work_time: '',
main_logo: {
image: '',
url: '',
title: ''
},
additional_logos: [],
support: {
callback: '',
phone_number: ''
}
},
error: undefined
}

const footerSlice = createSlice({
name: 'footer',
initialState,
reducers: {},
extraReducers: builder => {
builder
.addCase(getCoreBase.pending, state => {
state.error = undefined
})
.addCase(getCoreBase.fulfilled, (state, { payload }) => {
state.footer = payload
})
.addCase(getCoreBase.rejected, (state, { payload }) => {
state.error = rejectedPayloadHandle(payload)
})
}
})

export default footerSlice.reducer
24 changes: 24 additions & 0 deletions src/widgets/Footer/model/types/types.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
interface Logo {
image: string
url: string
title: string
}

interface Support {
callback: string
phone_number: string
}

export interface CoreBaseFooterData {
company_info: string
disclaimer: string
support_work_time: string
main_logo: Logo
additional_logos: Logo[]
support: Support
}

export interface CoreBaseFooterSchema {
footer: CoreBaseFooterData
error?: string | string[]
}

0 comments on commit 9f306e9

Please sign in to comment.