-
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 artists.
- Loading branch information
Showing
14 changed files
with
345 additions
and
56 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
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 {Artist} from '../../models/Artist.model'; | ||
import {ArtistAttr} from '../../models/Artist-response.model'; | ||
|
||
const ArtistActionTypes = { | ||
GET_ARTISTS: '[ARTISTS] Get tracks', | ||
GET_ARTISTS_SUCCESS: '[ARTISTS] Get tracks success', | ||
GET_ARTISTS_FAILURE: '[ARTISTS] Get tracks failure', | ||
|
||
SET_ARTIST: '[ARTISTS] Set track', | ||
|
||
NEXT_PAGE: '[ARTISTS] Next page', | ||
PREV_PAGE: '[ARTISTS] Prev page', | ||
}; | ||
|
||
export const getArtists = () => { | ||
return {type: ArtistActionTypes.GET_ARTISTS}; | ||
}; | ||
|
||
export const getArtistsSuccess = (tracks: Artist[], paginator: ArtistAttr) => { | ||
return {type: ArtistActionTypes.GET_ARTISTS_SUCCESS, tracks, paginator}; | ||
}; | ||
|
||
export const getArtistsFailure = (error: string) => { | ||
return {type: ArtistActionTypes.GET_ARTISTS_FAILURE, error}; | ||
}; | ||
|
||
export const setArtist = (track: Artist) => { | ||
return {type: ArtistActionTypes.SET_ARTIST, track}; | ||
}; | ||
|
||
export const nextPage = (page: number) => { | ||
return {type: ArtistActionTypes.NEXT_PAGE, page}; | ||
}; | ||
|
||
export const prevPage = (page: number) => { | ||
return {type: ArtistActionTypes.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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import * as TrackActions from './track.actions'; | ||
import * as ArtistActions from './artist.actions'; | ||
|
||
export {TrackActions}; | ||
export {TrackActions, ArtistActions}; |
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 {ArtistActions} from '../actions'; | ||
import {ArtistService} from '../../services/artist-service'; | ||
import {ArtistResponse} from '../../models/Artist-response.model'; | ||
import {Artist} from '../../models/Artist.model'; | ||
import {Navigation} from 'react-native-navigation'; | ||
|
||
const artistService = new ArtistService(); | ||
|
||
export const fetchArtists = () => { | ||
return (dispatch: any, getState: any) => { | ||
dispatch({type: ArtistActions.getArtists}); | ||
|
||
const {page, limit} = getState().artistsScreenStore.paginator; | ||
|
||
artistService.getAllTopArtists(limit, page).then((resp) => { | ||
const data: ArtistResponse = resp.data; | ||
dispatch({ | ||
type: ArtistActions.getArtistsSuccess, | ||
artists: data.topartists.artist, | ||
paginator: data.topartists['@attr'], | ||
}); | ||
}); | ||
}; | ||
}; | ||
|
||
export const fetchNextPage = () => { | ||
return (dispatch: any, getState: any) => { | ||
let {page, totalPages} = getState().artistsScreenStore.paginator; | ||
|
||
if (page < totalPages) { | ||
page = parseInt(`${page}`, 10) + 1; | ||
|
||
dispatch({type: ArtistActions.nextPage, page}); | ||
dispatch(fetchArtists()); | ||
} | ||
}; | ||
}; | ||
|
||
export const fetchPrevPage = () => { | ||
return (dispatch: any, getState: any) => { | ||
let {page} = getState().artistsScreenStore.paginator; | ||
if (page > 1) { | ||
page = parseInt(`${page}`, 10) - 1; | ||
|
||
dispatch({type: ArtistActions.nextPage, page}); | ||
dispatch(fetchArtists()); | ||
} | ||
}; | ||
}; | ||
|
||
export const goToArtistDetails = (artist: Artist) => { | ||
return (dispatch: any) => { | ||
dispatch({type: ArtistActions.setArtist, artist}); | ||
Navigation.push('ArtistsScreen', { | ||
component: { | ||
name: 'ArtistDetailsScreen', | ||
id: 'ArtistDetailsScreen', | ||
options: { | ||
topBar: { | ||
title: { | ||
text: `Detalles de ${artist.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,65 @@ | ||
import {Artist} from '../../models/Artist.model'; | ||
import {ArtistActions} from '../actions'; | ||
import {ArtistAttr} from '../../models/Artist-response.model'; | ||
|
||
export interface ArtistState { | ||
artists: Artist[]; | ||
artist: Artist | null; | ||
loading: boolean; | ||
error: string; | ||
limit: number; | ||
paginator: ArtistAttr; | ||
} | ||
|
||
const initialState: ArtistState = { | ||
artists: [], | ||
artist: null, | ||
loading: false, | ||
error: '', | ||
limit: 10, | ||
paginator: {page: '1', totalPages: '0'}, | ||
}; | ||
|
||
export default function artistReducer( | ||
state: ArtistState = initialState, | ||
action: any, | ||
) { | ||
switch (action.type) { | ||
case ArtistActions.getArtists: | ||
return { | ||
...state, | ||
loading: true, | ||
}; | ||
|
||
case ArtistActions.getArtistsSuccess: | ||
const {artists, paginator} = action; | ||
return { | ||
...state, | ||
artists: [...artists], | ||
paginator, | ||
loading: false, | ||
}; | ||
|
||
case ArtistActions.getArtistsFailure: | ||
return { | ||
...state, | ||
loading: false, | ||
erorr: action.error, | ||
}; | ||
|
||
case ArtistActions.setArtist: | ||
return { | ||
...state, | ||
artist: action.artist, | ||
}; | ||
|
||
case ArtistActions.nextPage: | ||
return {...state, paginator: {...state.paginator, page: action.page}}; | ||
|
||
case ArtistActions.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import {combineReducers} from 'redux'; | ||
import trackReducer from './track.reducer'; | ||
import artistReducer from './artist.reducer'; | ||
|
||
const rootReducer = combineReducers({tracksScreenStore: trackReducer}); | ||
const rootReducer = combineReducers({ | ||
tracksScreenStore: trackReducer, | ||
artistsScreenStore: artistReducer, | ||
}); | ||
|
||
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
Oops, something went wrong.