Skip to content

Commit

Permalink
feat(app-signup): add loading and error management
Browse files Browse the repository at this point in the history
  • Loading branch information
rams23 committed Dec 28, 2020
1 parent 06d3eea commit 7cf12bd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function createRequestHook<T extends Array<any>>(
triggerAction: (...args: T) => Action,
options?: HookOptions,
): () => GeneralHookResponse<T> {
return function (): GeneralHookResponse<T> {
const hook = function useRequest(): GeneralHookResponse<T> {
const t = useTranslate();
const dispatch = useDispatch();
const keySelector = useMemo(() => selectRequestStatus(requestKey), []);
Expand Down Expand Up @@ -53,4 +53,11 @@ export function createRequestHook<T extends Array<any>>(

return { loading, success, error, translatedError, call, reset };
};

Object.defineProperty(hook, 'name', {
value: `use${requestKey}Request`,
configurable: true,
});

return hook;
}
2 changes: 2 additions & 0 deletions packages/game-app/src/_shared/store/reducers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { name as i18nName, reducer as i18nReducer } from '@pipeline/i18n';
import { name as authName, reducer as authReducer } from '@pipeline/auth';
import { name as requestsStatusName, reducer as requestsStatusReducer } from '@pipeline/requests-status';

const reducers = {
[i18nName]: i18nReducer,
[authName]: authReducer,
[requestsStatusName]: requestsStatusReducer,
};

export default reducers;
9 changes: 6 additions & 3 deletions packages/game-app/src/signup/components/Signup/Signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ const Signup: React.FC<Props> = ({}) => {

const { handleSubmit } = methods;

const { execute } = useSignup();
const { call: signup, loading, translatedError, success } = useSignup();

const submit = useMemo(
() =>
handleSubmit((info: SignupInfo) => {
execute(info);
signup(info);
}),
[handleSubmit],
[signup, handleSubmit],
);

return (
Expand All @@ -73,6 +73,9 @@ const Signup: React.FC<Props> = ({}) => {
<button id="signup-button" onClick={submit}>
Signup
</button>
{loading ? <span>Loading</span> : null}
{translatedError ? <span className="error-message">{translatedError}</span> : null}
{loading ? <span>Success</span> : null}
</FormProvider>
</div>
</div>
Expand Down
18 changes: 5 additions & 13 deletions packages/game-app/src/signup/hooks/useSignup.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { useCallback } from 'react';
import { SignupInfo } from '../types/signupInfo';
import { useDispatch } from 'react-redux';
import * as actions from '../actions';
import { createRequestHook } from '@pipeline/requests-status';

export default function useSignup() {
const dispatch = useDispatch();
const useSignup = createRequestHook('signup', actions.signup, {
errorMessagesScope: 'signup',
});

const execute = useCallback((info: SignupInfo) => {
dispatch(actions.signup(info));
}, []);

return {
execute,
};
}
export default useSignup;
7 changes: 5 additions & 2 deletions packages/game-app/src/signup/sagas/signup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import * as actions from '../actions';
import { executeSignup } from '../apis/executeSignup';
import { call, takeEvery, takeLeading } from 'redux-saga/effects';
import { User } from '../../_shared/auth/slice';
import { addRequestStatusManagement } from '@pipeline/requests-status';

function* signupSaga(action: ReturnType<typeof actions.signup>) {
try {
const user: User = yield call(executeSignup, action.payload);
} catch (e) {}
} catch (e) {
throw e;
}
}

export default function* runSignup() {
yield takeLeading(actions.signup, signupSaga);
yield takeLeading(actions.signup, addRequestStatusManagement(signupSaga, 'signup'));
}

0 comments on commit 7cf12bd

Please sign in to comment.