Added fetchPopularLinks action in links redux
• Added: - fetchPopularLinks action in links redux
This commit is contained in:
parent
fc7cbbb457
commit
15d9714252
@ -4,9 +4,17 @@ export const LINK_FETCH_REQUEST = 'LINK_FETCH_REQUEST'
|
|||||||
export const LINK_FETCH_SUCCESS = 'LINK_FETCH_SUCCESS'
|
export const LINK_FETCH_SUCCESS = 'LINK_FETCH_SUCCESS'
|
||||||
export const LINK_FETCH_FAIL = 'LINK_FETCH_FAIL'
|
export const LINK_FETCH_FAIL = 'LINK_FETCH_FAIL'
|
||||||
|
|
||||||
|
export const POPULAR_LINKS_FETCH_REQUEST = 'POPULAR_LINKS_FETCH_REQUEST'
|
||||||
|
export const POPULAR_LINKS_FETCH_SUCCESS = 'POPULAR_LINKS_FETCH_SUCCESS'
|
||||||
|
export const POPULAR_LINKS_FETCH_FAIL = 'POPULAR_LINKS_FETCH_FAIL'
|
||||||
|
|
||||||
export const IMPORT_LINK_CARDS = 'IMPORT_LINK_CARDS'
|
export const IMPORT_LINK_CARDS = 'IMPORT_LINK_CARDS'
|
||||||
|
|
||||||
export const fetchLinkCard = (cardId) => (dispatch, getState) => {
|
export const fetchLinkCard = (cardId) => (dispatch, getState) => {
|
||||||
|
//If card exists, don't refetch
|
||||||
|
const card = getState().getIn(['links', 'items', `${cardId}`])
|
||||||
|
if (!!card) return
|
||||||
|
|
||||||
dispatch(fetchLinkCardRequest(cardId))
|
dispatch(fetchLinkCardRequest(cardId))
|
||||||
|
|
||||||
api(getState).get(`/api/v1/links/${cardId}`).then(({ data }) => {
|
api(getState).get(`/api/v1/links/${cardId}`).then(({ data }) => {
|
||||||
@ -34,4 +42,30 @@ export const fetchLinkCardFail = (error, cardId) => ({
|
|||||||
export const importLinkCards = (cards) => ({
|
export const importLinkCards = (cards) => ({
|
||||||
type: IMPORT_LINK_CARDS,
|
type: IMPORT_LINK_CARDS,
|
||||||
cards,
|
cards,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export const fetchPopularLinks = () => (dispatch, getState) => {
|
||||||
|
const isFetched = getState().getIn(['links', 'popular', 'isFetched'], false)
|
||||||
|
if (isFetched) return
|
||||||
|
|
||||||
|
dispatch(fetchPopularLinksRequest())
|
||||||
|
|
||||||
|
api(getState).get(`/api/v1/popular_links?type=links`).then(({ data }) => {
|
||||||
|
dispatch(fetchPopularLinksSuccess(data))
|
||||||
|
})
|
||||||
|
.catch((err) => dispatch(fetchPopularLinksFail(err)))
|
||||||
|
}
|
||||||
|
|
||||||
|
export const fetchPopularLinksRequest = () => ({
|
||||||
|
type: POPULAR_LINKS_FETCH_REQUEST,
|
||||||
|
})
|
||||||
|
|
||||||
|
export const fetchPopularLinksSuccess = (cards) => ({
|
||||||
|
type: POPULAR_LINKS_FETCH_SUCCESS,
|
||||||
|
cards,
|
||||||
|
})
|
||||||
|
|
||||||
|
export const fetchPopularLinksFail = (error) => ({
|
||||||
|
type: POPULAR_LINKS_FETCH_FAIL,
|
||||||
|
error,
|
||||||
|
})
|
||||||
|
@ -8,13 +8,20 @@ import {
|
|||||||
LINK_FETCH_SUCCESS,
|
LINK_FETCH_SUCCESS,
|
||||||
LINK_FETCH_FAIL,
|
LINK_FETCH_FAIL,
|
||||||
IMPORT_LINK_CARDS,
|
IMPORT_LINK_CARDS,
|
||||||
|
POPULAR_LINKS_FETCH_REQUEST,
|
||||||
|
POPULAR_LINKS_FETCH_SUCCESS,
|
||||||
|
POPULAR_LINKS_FETCH_FAIL,
|
||||||
} from '../actions/links'
|
} from '../actions/links'
|
||||||
|
|
||||||
const initialState = ImmutableMap({
|
const initialState = ImmutableMap({
|
||||||
isFetched: false,
|
isFetched: false,
|
||||||
isError: false,
|
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
items: ImmutableMap(),
|
items: ImmutableMap(),
|
||||||
|
popular: ImmutableMap({
|
||||||
|
isLoading: false,
|
||||||
|
isFetched: false,
|
||||||
|
items: ImmutableList(),
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
export default function links(state = initialState, action) {
|
export default function links(state = initialState, action) {
|
||||||
@ -26,20 +33,35 @@ export default function links(state = initialState, action) {
|
|||||||
mutable.setIn(['items', `${action.card.id}`], fromJS(action.card))
|
mutable.setIn(['items', `${action.card.id}`], fromJS(action.card))
|
||||||
mutable.set('isLoading', false)
|
mutable.set('isLoading', false)
|
||||||
mutable.set('isFetched', false)
|
mutable.set('isFetched', false)
|
||||||
mutable.set('isError', false)
|
|
||||||
})
|
})
|
||||||
case IMPORT_LINK_CARDS:
|
case IMPORT_LINK_CARDS:
|
||||||
return state.withMutations((mutable) => {
|
return state.withMutations((mutable) => {
|
||||||
action.cards.forEach((card) => mutable.setIn(['items', `${card.id}`], fromJS(card)))
|
action.cards.forEach((card) => mutable.setIn(['items', `${card.id}`], fromJS(card)))
|
||||||
mutable.set('isLoading', false)
|
mutable.set('isLoading', false)
|
||||||
mutable.set('isFetched', false)
|
mutable.set('isFetched', false)
|
||||||
mutable.set('isError', false)
|
|
||||||
})
|
})
|
||||||
case LINK_FETCH_FAIL:
|
case LINK_FETCH_FAIL:
|
||||||
return state.withMutations((mutable) => {
|
return state.withMutations((mutable) => {
|
||||||
mutable.set('isLoading', false)
|
mutable.set('isLoading', false)
|
||||||
mutable.set('isFetched', false)
|
mutable.set('isFetched', false)
|
||||||
mutable.set('isError', true)
|
})
|
||||||
|
case POPULAR_LINKS_FETCH_REQUEST:
|
||||||
|
return state.setIn(['popular', 'isLoading'], true)
|
||||||
|
case POPULAR_LINKS_FETCH_SUCCESS:
|
||||||
|
return state.withMutations((mutable) => {
|
||||||
|
let popularIds = []
|
||||||
|
action.cards.forEach((card) => {
|
||||||
|
mutable.setIn(['items', `${card.id}`], fromJS(card))
|
||||||
|
popularIds.push(`${card.id}`)
|
||||||
|
})
|
||||||
|
mutable.setIn(['popular', 'items'], fromJS(popularIds))
|
||||||
|
mutable.setIn(['popular', 'isFetched'], true)
|
||||||
|
})
|
||||||
|
case POPULAR_LINKS_FETCH_FAIL:
|
||||||
|
return state.withMutations((mutable) => {
|
||||||
|
mutable.setIn(['popular', 'isLoading'], false)
|
||||||
|
mutable.setIn(['popular', 'isFetched'], true)
|
||||||
|
mutable.setIn(['popular', 'items'], ImmutableList())
|
||||||
})
|
})
|
||||||
default:
|
default:
|
||||||
return state
|
return state
|
||||||
|
Loading…
x
Reference in New Issue
Block a user