-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(users): Fetch Articles on Landing Page
- Creates ActionType for fetchArticles page feature - Creates Actions for fetchArticles page feature - Creates Reducer for fetchArticles page feature [Delivers #167994897]
- Loading branch information
1 parent
00ec84e
commit 0197e21
Showing
10 changed files
with
130 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ node_modules | |
dist | ||
.DS_Store | ||
examples | ||
yarn-error.log | ||
yarn.lock | ||
|
||
#cypress | ||
cypress/fixtures | ||
|
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,17 @@ | ||
import React from 'react'; | ||
|
||
const ArtricleRow = ({ props }) => { | ||
const { props: component } = props; | ||
return ( | ||
<div | ||
className="w-full flex flex-col items-center | ||
lg:flex-row lg:h-80 lg:justify-between" | ||
> | ||
{component[0] || ''} | ||
{component[0] || ''} | ||
{component[0] || ''} | ||
</div> | ||
); | ||
}; | ||
|
||
export default ArtricleRow; |
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,3 @@ | ||
export default { | ||
apiUrl: 'https://errorswag-staging.herokuapp.com/api/v1' | ||
}; |
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,24 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { Link } from 'react-router-dom'; | ||
import { fetchArticlesAction } from '../../store/modules/landingPage/actions/index'; | ||
|
||
const LandingPage = () => { | ||
const [page, setPage] = useState(0); | ||
const [totalPages, setTotalPages] = useState(1); | ||
const [limit, setLimit] = useState(12); | ||
const [articles, setArticles] = useState([]); | ||
useEffect(() => { | ||
fetchArticlesAction({ page, limit }); | ||
}, [page]); | ||
}; | ||
|
||
export const mapDispatchToProps = dispatch => ({ | ||
fetchArticles: ({ page, limit }) => | ||
dispatch(fetchArticlesAction({ page, limit })) | ||
}); | ||
|
||
export default connect( | ||
null, | ||
mapDispatchToProps | ||
)(LandingPage); |
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,14 +1,10 @@ | ||
import { createStore, applyMiddleware, compose, combineReducers } from 'redux'; | ||
import { createStore, applyMiddleware } from 'redux'; | ||
import thunkMiddleware from 'redux-thunk'; | ||
import rootReducer from '@modules/'; | ||
import { composeWithDevTools } from 'redux-devtools-extension'; | ||
import rootReducer from './modules/auth/reducer'; | ||
|
||
const storeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; | ||
const middlewares = [thunkMiddleware]; | ||
const middlewares = applyMiddleware(thunkMiddleware); | ||
|
||
const store = createStore( | ||
rootReducer, | ||
{}, | ||
storeEnhancers(applyMiddleware(...middlewares)) | ||
); | ||
const store = createStore(rootReducer, composeWithDevTools(middlewares)); | ||
|
||
export default store; |
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,3 @@ | ||
export const FETCHARTICLES_PENDING = 'FETCHARTICLES_PENDING'; | ||
export const FETCHARTICLES_SUCCESS = 'FETCHARTICLES_SUCCESS'; | ||
export const FETCHARTICLES_FAILURE = 'FETCHARTICLES_FAILURE'; |
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,51 @@ | ||
import axios from 'axios'; | ||
import { | ||
FETCHARTICLES_FAILURE, | ||
FETCHARTICLES_PENDING, | ||
FETCHARTICLES_SUCCESS | ||
} from '../actionTypes/index'; | ||
import config from '../../../../config'; | ||
|
||
export const fetchArticlesPending = () => ({ | ||
type: FETCHARTICLES_PENDING, | ||
payload: { | ||
status: 'fetchPending', | ||
error: null, | ||
articles: [] | ||
} | ||
}); | ||
|
||
export const fetchArticlesSuccess = articles => ({ | ||
type: FETCHARTICLES_SUCCESS, | ||
payload: { | ||
status: 'fetchSuccess', | ||
error: null, | ||
articles | ||
} | ||
}); | ||
|
||
export const fetchArticlesFailure = error => ({ | ||
type: FETCHARTICLES_FAILURE, | ||
payload: { | ||
status: 'fetchFailure', | ||
error, | ||
articles: [] | ||
} | ||
}); | ||
|
||
export const fetchArticlesAction = ({ limit, page }) => async dispatch => { | ||
dispatch(fetchArticlesPending()); | ||
try { | ||
const response = await axios({ | ||
method: 'get', | ||
url: `${config.apiUrl}/articles/popular?page=${page}$limit=${limit}` | ||
}); | ||
|
||
const { data } = response.data; | ||
console.log('this is the response from the database', data); | ||
dispatch(fetchArticlesSuccess(data)); | ||
} catch ({ response }) { | ||
const { message } = response.data; | ||
dispatch(fetchArticlesFailure(message)); | ||
} | ||
}; |
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,7 @@ | ||
export const landingPage = { | ||
status: 'rest', | ||
error: null, | ||
articles: [] | ||
}; | ||
|
||
export default landingPage; |
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,17 @@ | ||
import { | ||
FETCHARTICLES_PENDING, | ||
FETCHARTICLES_FAILURE, | ||
FETCHARTICLES_SUCCESS | ||
} from '../actionTypes/index'; | ||
import initialState from '../index'; | ||
|
||
const fetchArticlesTypes = [ | ||
FETCHARTICLES_PENDING, | ||
FETCHARTICLES_SUCCESS, | ||
FETCHARTICLES_FAILURE | ||
]; | ||
const fetcharticlesReducers = (state = initialState, { type, payload }) => { | ||
return fetchArticlesTypes.includes(type) ? { ...state, ...payload } : state; | ||
}; | ||
|
||
export default fetcharticlesReducers; |