From f7a1ef21235dbe38c95d6843ce215b445e896640 Mon Sep 17 00:00:00 2001 From: Gan Eng Chin Date: Fri, 24 Feb 2023 23:45:10 +0800 Subject: [PATCH] Code refactor with getTotalFromResponse in getCampaigns. --- .../marketing/data-multichannel/resolvers.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/plugins/woocommerce-admin/client/marketing/data-multichannel/resolvers.ts b/plugins/woocommerce-admin/client/marketing/data-multichannel/resolvers.ts index 2ee2178872a..7cce5cb5455 100644 --- a/plugins/woocommerce-admin/client/marketing/data-multichannel/resolvers.ts +++ b/plugins/woocommerce-admin/client/marketing/data-multichannel/resolvers.ts @@ -56,6 +56,12 @@ export function* getRecommendedChannels() { } } +const getTotalFromResponse = ( response: Response ) => { + return ( + parseInt( response.headers.get( 'x-wp-total' ) || '0', 10 ) || undefined + ); +}; + export function* getCampaigns( page: number, perPage: number ) { try { const response: Response = yield apiFetch( { @@ -63,10 +69,7 @@ export function* getCampaigns( page: number, perPage: number ) { parse: false, } ); - const total = parseInt( - response.headers.get( 'x-wp-total' ) || '0', - 10 - ); + const total = getTotalFromResponse( response ); const payload: Campaign[] = yield awaitResponseJson( response ); yield receiveCampaignsSuccess( { @@ -80,10 +83,7 @@ export function* getCampaigns( page: number, perPage: number ) { } ); } catch ( error ) { if ( error instanceof Response ) { - const total = - parseInt( error.headers.get( 'x-wp-total' ) || '0', 10 ) || - undefined; - + const total = getTotalFromResponse( error ); const payload: ApiFetchError = yield awaitResponseJson( error ); yield receiveCampaignsError( {