Simplify receive campaigns success and error.

This commit is contained in:
Gan Eng Chin 2023-03-02 03:02:14 +08:00
parent 5af90c5e77
commit 9feaf55a60
No known key found for this signature in database
GPG Key ID: 94D5D972860ADB01
4 changed files with 24 additions and 39 deletions

View File

@ -7,6 +7,5 @@ export const TYPES = {
'RECEIVE_RECOMMENDED_CHANNELS_SUCCESS' as const,
RECEIVE_RECOMMENDED_CHANNELS_ERROR:
'RECEIVE_RECOMMENDED_CHANNELS_ERROR' as const,
RECEIVE_CAMPAIGNS_SUCCESS: 'RECEIVE_CAMPAIGNS_SUCCESS' as const,
RECEIVE_CAMPAIGNS_ERROR: 'RECEIVE_CAMPAIGNS_ERROR' as const,
RECEIVE_CAMPAIGNS: 'RECEIVE_CAMPAIGNS' as const,
};

View File

@ -43,33 +43,32 @@ export const receiveRecommendedChannelsError = ( error: ApiFetchError ) => {
};
};
export const receiveCampaignsSuccess = ( success: {
type CampaignsSuccessResponse = {
payload: Array< Campaign >;
error: boolean;
error: false;
meta: {
page: number;
perPage: number;
total?: number;
};
} ) => {
return {
type: TYPES.RECEIVE_CAMPAIGNS_SUCCESS,
...success,
};
};
export const receiveCampaignsError = ( error: {
type CampaignsFailResponse = {
payload: ApiFetchError;
error: boolean;
error: true;
meta: {
page: number;
perPage: number;
total?: number;
};
} ) => {
};
type CampaignsResponse = CampaignsSuccessResponse | CampaignsFailResponse;
export const receiveCampaigns = ( response: CampaignsResponse ) => {
return {
type: TYPES.RECEIVE_CAMPAIGNS_ERROR,
...error,
type: TYPES.RECEIVE_CAMPAIGNS,
...response,
};
};
@ -78,6 +77,5 @@ export type Action = ReturnType<
| typeof receiveRegisteredChannelsError
| typeof receiveRecommendedChannelsSuccess
| typeof receiveRecommendedChannelsError
| typeof receiveCampaignsSuccess
| typeof receiveCampaignsError
| typeof receiveCampaigns
>;

View File

@ -61,31 +61,20 @@ export const reducer: Reducer< State, Action > = (
},
};
case TYPES.RECEIVE_CAMPAIGNS_SUCCESS:
case TYPES.RECEIVE_CAMPAIGNS:
return {
...state,
campaigns: {
perPage: action.meta.perPage,
pages: {
...state.campaigns.pages,
[ action.meta.page ]: {
data: action.payload,
},
},
total: action.meta.total,
},
};
case TYPES.RECEIVE_CAMPAIGNS_ERROR:
return {
...state,
campaigns: {
perPage: action.meta.perPage,
pages: {
...state.campaigns.pages,
[ action.meta.page ]: {
error: action.payload,
},
[ action.meta.page ]: action.error
? {
error: action.payload,
}
: {
data: action.payload,
},
},
total: action.meta.total,
},

View File

@ -11,8 +11,7 @@ import {
receiveRegisteredChannelsError,
receiveRecommendedChannelsSuccess,
receiveRecommendedChannelsError,
receiveCampaignsSuccess,
receiveCampaignsError,
receiveCampaigns,
} from './actions';
import { awaitResponseJson } from './controls';
import {
@ -81,7 +80,7 @@ export function* getCampaigns( page: number, perPage: number ) {
const total = getTotalFromResponse( response );
const payload: Campaign[] = yield awaitResponseJson( response );
yield receiveCampaignsSuccess( {
yield receiveCampaigns( {
payload,
error: false,
meta: {
@ -95,7 +94,7 @@ export function* getCampaigns( page: number, perPage: number ) {
const total = getTotalFromResponse( error );
const payload: ApiFetchError = yield awaitResponseJson( error );
yield receiveCampaignsError( {
yield receiveCampaigns( {
payload,
error: true,
meta: {