forked from react-boilerplate/react-boilerplate-cra-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
react-boilerplate#32: done show category
- Loading branch information
1 parent
3e58b8a
commit 756afe0
Showing
18 changed files
with
287 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
import styled from 'styled-components'; | ||
import { LoadingIndicator } from '../LoadingIndicator'; | ||
import React from 'react'; | ||
|
||
export const CenteredLoading = () => { | ||
interface Props { | ||
minHeight?: string; | ||
} | ||
|
||
export const CenteredLoading: React.FC<Props> = ({ minHeight }) => { | ||
return ( | ||
<CenteredWrapper> | ||
<CenteredWrapper minHeight={minHeight}> | ||
<LoadingIndicator /> | ||
</CenteredWrapper> | ||
); | ||
}; | ||
|
||
const CenteredWrapper = styled.div` | ||
const CenteredWrapper = styled.div<{ minHeight?: string }>` | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
min-height: 200px; | ||
min-height: ${({ minHeight }) => (minHeight ? minHeight : '200px')}; | ||
`; |
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,58 @@ | ||
import { PayloadAction } from '@reduxjs/toolkit'; | ||
import { ProductCateState } from './type'; | ||
import { createSlice } from 'utils/@reduxjs/toolkit'; | ||
import { useInjectReducer, useInjectSaga } from 'utils/redux-injectors'; | ||
import { ICard } from 'types/Card'; | ||
import { ProductCateFromSaga } from './saga'; | ||
|
||
export const initialState: ProductCateState = { | ||
isLoading: false, | ||
isPageLoading: false, | ||
cateId: '0', | ||
take: 10, | ||
skip: 0, | ||
total: 0, | ||
isNext: true, | ||
itemPerPage: 10, | ||
products: [], | ||
}; | ||
|
||
const slice = createSlice({ | ||
name: 'productCateState', | ||
initialState, | ||
reducers: { | ||
loadProduct(state) { | ||
state.isLoading = true; | ||
}, | ||
productLoaded(state, actions: PayloadAction<ICard[]>) { | ||
state.isLoading = false; | ||
state.products = [...state.products, ...actions.payload]; | ||
}, | ||
nextPage(state) { | ||
state.skip = state.skip + state.itemPerPage; | ||
}, | ||
loadingPage(state) { | ||
state.isPageLoading = true; | ||
}, | ||
pageLoaded(state, actions: PayloadAction<ICard[]>) { | ||
state.isPageLoading = false; | ||
state.products = [...state.products, ...actions.payload]; | ||
}, | ||
setCateId(state, actions: PayloadAction<string | undefined>) { | ||
if (actions.payload) { | ||
state.cateId = actions.payload; | ||
} | ||
}, | ||
setTotal(state, action: PayloadAction<number>) { | ||
state.total = action.payload; | ||
}, | ||
}, | ||
}); | ||
|
||
export const { actions: ProductCateActions, reducer } = slice; | ||
|
||
export const useProductCateSlice = () => { | ||
useInjectReducer({ key: slice.name, reducer: slice.reducer }); | ||
useInjectSaga({ key: slice.name, saga: ProductCateFromSaga }); | ||
return { actions: slice.actions }; | ||
}; |
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,49 @@ | ||
import { call, put, select, takeLatest } from 'redux-saga/effects'; | ||
import { selectCateId, selectSkip, selectTake } from './selector'; | ||
import { request } from 'utils/request'; | ||
import { BASE_URL } from 'utils/url'; | ||
import { createQueryString } from 'utils/string'; | ||
import { ProductCateActions } from '.'; | ||
|
||
export function* getProductCate() { | ||
try { | ||
const cateId = yield select(selectCateId); | ||
const take = yield select(selectTake); | ||
const skip = yield select(selectSkip); | ||
|
||
const queryString = createQueryString({ cateId, take, skip }); | ||
|
||
const data = yield call(request, `${BASE_URL}/category?${queryString}`); | ||
|
||
if (data.data.data.length > 0) { | ||
yield put(ProductCateActions.productLoaded(data.data.data)); | ||
yield put(ProductCateActions.setTotal(data.data.paging.total)); | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
} | ||
|
||
export function* getProductPageCate() { | ||
try { | ||
const cateId = yield select(selectCateId); | ||
const take = yield select(selectTake); | ||
const skip = yield select(selectSkip); | ||
|
||
const queryString = createQueryString({ cateId, take, skip }); | ||
|
||
const data = yield call(request, `${BASE_URL}/category?${queryString}`); | ||
|
||
if (data.data.data.length > 0) { | ||
yield put(ProductCateActions.pageLoaded(data.data.data)); | ||
yield put(ProductCateActions.setTotal(data.data.paging.total)); | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
} | ||
|
||
export function* ProductCateFromSaga() { | ||
yield takeLatest(ProductCateActions.loadProduct, getProductCate); | ||
yield takeLatest(ProductCateActions.loadingPage, getProductPageCate); | ||
} |
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,38 @@ | ||
import { createSelector } from '@reduxjs/toolkit'; | ||
import { RootState } from 'types'; | ||
import { initialState } from '.'; | ||
|
||
export const selectCateId = createSelector( | ||
[(state: RootState) => state.productCateState || initialState], | ||
state => state.cateId, | ||
); | ||
|
||
export const selectIsLoading = createSelector( | ||
[(state: RootState) => state.productCateState || initialState], | ||
state => state.isLoading, | ||
); | ||
|
||
export const selectTake = createSelector( | ||
[(state: RootState) => state.productCateState || initialState], | ||
state => state.take, | ||
); | ||
|
||
export const selectSkip = createSelector( | ||
[(state: RootState) => state.productCateState || initialState], | ||
state => state.skip, | ||
); | ||
|
||
export const selectProductCate = createSelector( | ||
[(state: RootState) => state.productCateState || initialState], | ||
state => state.products, | ||
); | ||
|
||
export const selectIsLoadingPage = createSelector( | ||
[(state: RootState) => state.productCateState || initialState], | ||
state => state.isPageLoading, | ||
); | ||
|
||
export const selectTotal = createSelector( | ||
[(state: RootState) => state.productCateState || initialState], | ||
state => state.total, | ||
); |
Oops, something went wrong.