Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. function useEntities(summonerName) {
  2. const summoner = useSelector(getSummonerSelector)
  3. const rankedSolo = useSelector(state => getSoloFlexRanked(state, 'RANKED_SOLO_5x5'))
  4. const league = useSelector(getLeagueSelector)
  5.  
  6. useSaga('summoner', Api.getSummonerByName, summonerName)
  7. useSaga('myleague', Api.getSummonerLeague, summoner.id)
  8. useSaga('league', Api.getSummonerLeagueName, rankedSolo.leagueId)
  9. useSaga('allchampions', Api.getAllChampions)
  10. useSaga('summonerspells', Api.getSummonerSpells)
  11. useSaga('items', Api.getItems)
  12. useSaga('matchlists', Api.getMatchList, summoner.accountId)
  13.  
  14. return { summoner, league }
  15. }
  16.  
  17. function DashboardContainer({
  18. match: {
  19. params: { summonerName },
  20. },
  21. }: Props) {
  22. const { summoner, league } = useEntities(summonerName)
  23. const isFetching = useSelector(isEntityFetching)
  24. const entityError = useSelector(hasEntityError)
  25. const rankedSolo = useSelector(state => getSoloFlexRanked(state, 'RANKED_SOLO_5x5'))
  26. const rankedFlex = useSelector(state => getSoloFlexRanked(state, 'RANKED_FLEX_SR'))
  27. const matchLists = useSelector(getMatchListsSelector)
  28.  
  29. if (!summonerName) return <Redirect to="/" />
  30. if (isFetching) return <div>Loading...</div>
  31. if (entityError) return <div>{`Error while fetching: ${entityError}`}</div>
  32.  
  33. return (
  34. <SummonerContext.Provider
  35. value={{
  36. summoner,
  37. league,
  38. rankedSolo,
  39. rankedFlex,
  40. matchLists,
  41. }}
  42. >
  43. <Dashboard summoner={summoner} rankedSolo={rankedSolo} />
  44. </SummonerContext.Provider>
  45. )
  46. }
  47.  
  48. export default DashboardContainer
  49.  
  50. export function useSaga(entityName, apiEndpoint, ...args) {
  51. const entity = useSelector(state => state.entities[entityName])
  52. const dispatch = useDispatch()
  53.  
  54. if (args.some(param => param === null || param === undefined)) return
  55.  
  56. if (!entity || isEmpty(entity))
  57. dispatch(
  58. fetchInfo({
  59. entityName,
  60. apiEndpoint,
  61. args,
  62. }),
  63. )
  64. }
  65.  
  66. // @flow
  67.  
  68. type TypeParams = {
  69. entityName: string,
  70. apiEndpoint: () => void,
  71. args: Array<String>,
  72. }
  73.  
  74. export const fetchInfo = ({ entityName, apiEndpoint, args }: TypeParams) => ({
  75. type: 'ENTITY_REQUEST',
  76. entityName,
  77. apiEndpoint,
  78. payload: args,
  79. })
  80.  
  81. export function* fetchEntity(action) {
  82. const { apiEndpoint, entityName, payload } = action
  83. let response = {}
  84. try {
  85. if (Array.isArray(payload[0])) {
  86. response = yield all(payload[0].map(key => call(apiEndpoint, key)))
  87. } else response = yield call(apiEndpoint, ...payload)
  88.  
  89. yield put({
  90. type: `${entityName.toUpperCase()}_SUCCESS`,
  91. entityName,
  92. payload: Array.isArray(response) ? response.map(item => item.data) : response.data,
  93. })
  94.  
  95. return response.data
  96. } catch (error) {
  97. yield put({
  98. type: `${entityName.toUpperCase()}_FAILURE`,
  99. entityName,
  100. error: error.message,
  101. })
  102. }
  103. }
  104.  
  105. export default function* saga() {
  106. yield takeEvery('ENTITY_REQUEST', fetchEntity)
  107. }
  108.  
  109. import { set } from 'app/utils/object'
  110.  
  111. function entitiesReducer(state = {}, action) {
  112. const { type, payload, error, entityName } = action
  113.  
  114. if (type.endsWith('REQUEST')) {
  115. return set({ ...state }, entityName.toLowerCase(), {
  116. isFetching: true,
  117. error: null,
  118. })
  119. }
  120.  
  121. if (type.endsWith('SUCCESS')) {
  122. return set({ ...state }, entityName.toLowerCase(), {
  123. ...payload,
  124. isFetching: false,
  125. error: null,
  126. })
  127. }
  128.  
  129. if (type.endsWith('FAILURE')) {
  130. return set({ ...state }, entityName.toLowerCase(), {
  131. error,
  132. isFetching: false,
  133. })
  134. }
  135.  
  136. return state
  137. }
  138.  
  139. export default entitiesReducer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement