diff --git a/template/src/app/common/method/index.ts b/template/src/app/common/method/index.ts index e7102275..713c07d7 100644 --- a/template/src/app/common/method/index.ts +++ b/template/src/app/common/method/index.ts @@ -55,6 +55,19 @@ export const propsToStyle = >( }, {} as Record); }; +/** + * return true when success and false when error + */ + export const handleErrorResponse = ( + response: ResponseBase, +): response is ResponseBase => { + if (!response.status) { + // TODO: handle error + return false; + } + return true; +}; + export const execFunc = any>( func?: Fn, ...args: Parameters diff --git a/template/src/app/config/type.ts b/template/src/app/config/type.ts index 0c594f2e..f6478217 100644 --- a/template/src/app/config/type.ts +++ b/template/src/app/config/type.ts @@ -1,13 +1,17 @@ -export interface ResponseBase> { +export type ResponseBase = { code: number; +} & (TStatus extends true + ? { + data: T; - msg?: string | null; + status: true; + } + : { + status: false; - data?: T; - - status: boolean; -} + msg?: string |null; + }); export interface ParamsNetwork { url: string; params?: Record; diff --git a/template/src/app/library/networking/helper.ts b/template/src/app/library/networking/helper.ts index 96878aa4..c4a70f56 100644 --- a/template/src/app/library/networking/helper.ts +++ b/template/src/app/library/networking/helper.ts @@ -14,7 +14,6 @@ const responseDefault: ResponseBase> = { code: -500, status: false, msg: translate('error:errorData'), - data: {}, }; export const onPushLogout = async () => { @@ -28,7 +27,7 @@ export const handleResponseAxios = >( res: AxiosResponse, ): ResponseBase => { if (res.data) { - return { code: CODE_SUCCESS, status: true, data: res.data, msg: null }; + return { code: CODE_SUCCESS, status: true, data: res.data, }; } return responseDefault as ResponseBase; }; diff --git a/template/src/app/redux/listener/login.ts b/template/src/app/redux/listener/login.ts index f7ab663c..8d1c94cd 100644 --- a/template/src/app/redux/listener/login.ts +++ b/template/src/app/redux/listener/login.ts @@ -1,3 +1,4 @@ +import { handleErrorResponse } from '@common' import { takeLatestListeners } from '@listener'; import { ApiConstants, NetWorkService } from '@networking'; @@ -16,5 +17,8 @@ takeLatestListeners(true)({ if (!response) { return; } + if (handleErrorResponse(response)) { + // TODO: do something when login success + } }, });