-
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.
✨ Applying Redux in the screen of get top tracks.
- Loading branch information
Showing
16 changed files
with
461 additions
and
64 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,4 +1,4 @@ | ||
{ | ||
"name": "LastFMLevelOne", | ||
"displayName": "LastFMLevelOne" | ||
"name": "LastFMLevelTwoAndThree", | ||
"displayName": "LastFMLevelTwoAndThree" | ||
} |
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,16 @@ | ||
import {createStore, applyMiddleware} from 'redux'; | ||
import {composeWithDevTools} from 'redux-devtools-extension'; | ||
import thunk from 'redux-thunk'; | ||
import Reducers from '../store/reducers'; | ||
|
||
const middleware = [thunk]; | ||
|
||
const configureStore = () => { | ||
const store = createStore( | ||
Reducers, | ||
composeWithDevTools(applyMiddleware(...middleware)), | ||
); | ||
return store; | ||
}; | ||
|
||
export default configureStore; |
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,3 @@ | ||
import * as TrackActions from './track.actions'; | ||
|
||
export {TrackActions}; |
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,37 @@ | ||
import {Track} from '../../models/Track.model'; | ||
import {TracksAttr} from './../../models/Tracks-response.model'; | ||
|
||
const TrackActionTypes = { | ||
GET_TRACKS: '[TRACKS] Get tracks', | ||
GET_TRACKS_SUCCESS: '[TRACKS] Get tracks success', | ||
GET_TRACKS_FAILURE: '[TRACKS] Get tracks failure', | ||
|
||
SET_TRACK: '[TRACKS] Set track', | ||
|
||
NEXT_PAGE: '[TRACKS] Next page', | ||
PREV_PAGE: '[TRACKS] Prev page', | ||
}; | ||
|
||
export const getTracks = () => { | ||
return {type: TrackActionTypes.GET_TRACKS}; | ||
}; | ||
|
||
export const getTracksSuccess = (tracks: Track[], paginator: TracksAttr) => { | ||
return {type: TrackActionTypes.GET_TRACKS_SUCCESS, tracks, paginator}; | ||
}; | ||
|
||
export const getTracksFailure = (error: string) => { | ||
return {type: TrackActionTypes.GET_TRACKS_FAILURE, error}; | ||
}; | ||
|
||
export const setTrack = (track: Track) => { | ||
return {type: TrackActionTypes.SET_TRACK, track}; | ||
}; | ||
|
||
export const nextPage = (page: number) => { | ||
return {type: TrackActionTypes.NEXT_PAGE, page}; | ||
}; | ||
|
||
export const prevPage = (page: number) => { | ||
return {type: TrackActionTypes.PREV_PAGE, page}; | ||
}; |
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,68 @@ | ||
import {TrackActions} from '../actions'; | ||
import {TrackService} from './../../services/track-service'; | ||
import {TracksResponse} from './../../models/Tracks-response.model'; | ||
import {Track} from './../../models/Track.model'; | ||
import {Navigation} from 'react-native-navigation'; | ||
|
||
const trackService = new TrackService(); | ||
|
||
export const fetchTracks = () => { | ||
return (dispatch: any, getState: any) => { | ||
dispatch({type: TrackActions.getTracks}); | ||
|
||
const {page, limit} = getState().tracksScreenStore.paginator; | ||
|
||
trackService.getAllTopTracks(limit, page).then((resp) => { | ||
const data: TracksResponse = resp.data; | ||
dispatch({ | ||
type: TrackActions.getTracksSuccess, | ||
tracks: data.tracks.track, | ||
paginator: data.tracks['@attr'], | ||
}); | ||
}); | ||
}; | ||
}; | ||
|
||
export const fetchNextPage = () => { | ||
return (dispatch: any, getState: any) => { | ||
let {page, totalPages} = getState().tracksScreenStore.paginator; | ||
|
||
if (page < totalPages) { | ||
page = parseInt(`${page}`, 10) + 1; | ||
|
||
dispatch({type: TrackActions.nextPage, page}); | ||
dispatch(fetchTracks()); | ||
} | ||
}; | ||
}; | ||
|
||
export const fetchPrevPage = () => { | ||
return (dispatch: any, getState: any) => { | ||
let {page} = getState().tracksScreenStore.paginator; | ||
if (page > 1) { | ||
page = parseInt(`${page}`, 10) - 1; | ||
|
||
dispatch({type: TrackActions.nextPage, page}); | ||
dispatch(fetchTracks()); | ||
} | ||
}; | ||
}; | ||
|
||
export const goToTrackDetails = (track: Track) => { | ||
return (dispatch: any) => { | ||
dispatch({type: TrackActions.setTrack, track}); | ||
Navigation.push('TracksScreen', { | ||
component: { | ||
name: 'TrackDetailsScreen', | ||
id: 'TrackDetailsScreen', | ||
options: { | ||
topBar: { | ||
title: { | ||
text: `Detalles de ${track.name}`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}; | ||
}; |
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,6 @@ | ||
import {combineReducers} from 'redux'; | ||
import trackReducer from './track.reducer'; | ||
|
||
const rootReducer = combineReducers({tracksScreenStore: trackReducer}); | ||
|
||
export default rootReducer; |
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,65 @@ | ||
import {Track} from './../../models/Track.model'; | ||
import {TrackActions} from '../actions'; | ||
import {TracksAttr} from './../../models/Tracks-response.model'; | ||
|
||
export interface TrackState { | ||
tracks: Track[]; | ||
track: Track | null; | ||
loading: boolean; | ||
error: string; | ||
limit: number; | ||
paginator: TracksAttr; | ||
} | ||
|
||
const initialState: TrackState = { | ||
tracks: [], | ||
track: null, | ||
loading: false, | ||
error: '', | ||
limit: 10, | ||
paginator: {page: '1', totalPages: '0'}, | ||
}; | ||
|
||
export default function trackReducer( | ||
state: TrackState = initialState, | ||
action: any, | ||
) { | ||
switch (action.type) { | ||
case TrackActions.getTracks: | ||
return { | ||
...state, | ||
loading: true, | ||
}; | ||
|
||
case TrackActions.getTracksSuccess: | ||
const {tracks, paginator} = action; | ||
return { | ||
...state, | ||
tracks: [...tracks], | ||
paginator, | ||
loading: false, | ||
}; | ||
|
||
case TrackActions.getTracksFailure: | ||
return { | ||
...state, | ||
loading: false, | ||
erorr: action.error, | ||
}; | ||
|
||
case TrackActions.setTrack: | ||
return { | ||
...state, | ||
track: action.track, | ||
}; | ||
|
||
case TrackActions.nextPage: | ||
return {...state, paginator: {...state.paginator, page: action.page}}; | ||
|
||
case TrackActions.prevPage: | ||
return {...state, paginator: {...state.paginator, page: action.page}}; | ||
|
||
default: | ||
return state; | ||
} | ||
} |
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
Oops, something went wrong.