Skip to content

Commit

Permalink
#104 extracted fetching github emojis to hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
artzub committed Mar 13, 2023
1 parent 4776cc1 commit 704fa10
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
21 changes: 2 additions & 19 deletions src/components/StageController/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';

import emojisSlice from '@/redux/modules/emojis';

import { useRouteMatches } from '@/shared/hooks/useRouteMatches';

import { useGithubEmojis } from './useGithubEmojis';
import { useStageBranches } from './useStageBranches';
import { useStageCommits } from './useStageCommits';
import { useStageProfiles } from './useStageProfiles';
import { useStageRepositories } from './useStageRepositories';

const StageController = () => {
const dispatch = useDispatch();

const { service, profile, repository, branch, commits } = useRouteMatches();

// fetch list of emojis
useEffect(
() => {
dispatch(emojisSlice.actions.fetch());

return () => {
dispatch(emojisSlice.actions.cancel());
};
},
[service, dispatch],
);

useGithubEmojis(service);
useStageProfiles(service, profile);
useStageRepositories(repository);
useStageBranches(branch);
Expand Down
26 changes: 26 additions & 0 deletions src/components/StageController/useGithubEmojis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';

import emojisSlice from '@/redux/modules/emojis';

export const useGithubEmojis = (service) => {
const dispatch = useDispatch();

const { items: emojis } = useSelector(emojisSlice.selectors.getState);

// fetch list of emojis
useEffect(
() => {
if (service !== 'github' || Object.values(emojis).length) {
return undefined;
}

dispatch(emojisSlice.actions.fetch());

return () => {
dispatch(emojisSlice.actions.cancel());
};
},
[service, dispatch],
);
};
9 changes: 4 additions & 5 deletions src/redux/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const startFetching = (state) => {
state.isFetching = true;
state._requests = (state._requests ?? 0) + 1;
state.error = '';
state.errorStatus = 0;
};

/**
Expand All @@ -29,18 +30,16 @@ export const stopFetching = (state) => {
*/
export const incrementFetching = (state, { payload: { data, append } }) => {
const fixed = Array.isArray(data) ? data : [];
state.items = append ? [
...state.items,
...fixed,
] : fixed;
state.items = append ? [...state.items, ...fixed] : fixed;
};

/**
* Stops fetching and set error message into state
* @param {*} state
* @param {Error} payload
*/
export const fail = (state, { payload: { message } }) => {
export const fail = (state, { payload: { message, status } }) => {
stopFetching(state);
state.error = message;
state.errorStatus = status;
};

0 comments on commit 704fa10

Please sign in to comment.