Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Convert checkout state context provider to Typescript (#4200)
Browse files Browse the repository at this point in the history
* git move to ts files

* Type the checkout state provider

* Restore for loop for error handling

* Types not needed

* Consolodate response handling

* Unused import

* Fix defaults for onCheckoutAfterProcessingWithSuccess etc

* Type useEmitResponse and remove isObject checks

* useEmitResponse as const

* Check that redirectUrl is string

* Define actions as const

* data.redirectUrl should be truthy

* Add redirectURL todo item as followup

* remove null fallback
  • Loading branch information
mikejolley authored and grogou committed Aug 20, 2021
1 parent fccc72b commit 5f84e7d
Show file tree
Hide file tree
Showing 17 changed files with 770 additions and 629 deletions.
59 changes: 0 additions & 59 deletions assets/js/base/context/hooks/use-emit-response.js

This file was deleted.

66 changes: 66 additions & 0 deletions assets/js/base/context/hooks/use-emit-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Internal dependencies
*/
import { isObject } from '../../utils/type-guards';

export enum responseTypes {
SUCCESS = 'success',
FAIL = 'failure',
ERROR = 'error',
}

export enum noticeContexts {
PAYMENTS = 'wc/payment-area',
EXPRESS_PAYMENTS = 'wc/express-payment-area',
}

export interface ResponseType extends Record< string, unknown > {
type: responseTypes;
retry?: boolean;
}

const isResponseOf = (
response: unknown,
type: string
): response is ResponseType => {
return isObject( response ) && 'type' in response && response.type === type;
};

export const isSuccessResponse = (
response: unknown
): response is ResponseType => {
return isResponseOf( response, responseTypes.SUCCESS );
};

export const isErrorResponse = (
response: unknown
): response is ResponseType => {
return isResponseOf( response, responseTypes.ERROR );
};

export const isFailResponse = (
response: unknown
): response is ResponseType => {
return isResponseOf( response, responseTypes.FAIL );
};

export const shouldRetry = ( response: unknown ): boolean => {
return (
! isObject( response ) ||
typeof response.retry === 'undefined' ||
response.retry === true
);
};

/**
* A custom hook exposing response utilities for emitters.
*/
export const useEmitResponse = () =>
( {
responseTypes,
noticeContexts,
shouldRetry,
isSuccessResponse,
isErrorResponse,
isFailResponse,
} as const );

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Internal dependencies
*/
import type { PaymentResultDataType } from './types';

export enum ACTION {
SET_IDLE = 'set_idle',
SET_PRISTINE = 'set_pristine',
SET_REDIRECT_URL = 'set_redirect_url',
SET_COMPLETE = 'set_checkout_complete',
SET_BEFORE_PROCESSING = 'set_before_processing',
SET_AFTER_PROCESSING = 'set_after_processing',
SET_PROCESSING_RESPONSE = 'set_processing_response',
SET_PROCESSING = 'set_checkout_is_processing',
SET_HAS_ERROR = 'set_checkout_has_error',
SET_NO_ERROR = 'set_checkout_no_error',
SET_CUSTOMER_ID = 'set_checkout_customer_id',
SET_ORDER_ID = 'set_checkout_order_id',
SET_ORDER_NOTES = 'set_checkout_order_notes',
INCREMENT_CALCULATING = 'increment_calculating',
DECREMENT_CALCULATING = 'decrement_calculating',
SET_SHOULD_CREATE_ACCOUNT = 'set_should_create_account',
}

export interface ActionType {
type: ACTION;
data?:
| Record< string, unknown >
| Record< string, never >
| PaymentResultDataType;
url?: string;
customerId?: number;
orderId?: number;
shouldCreateAccount?: boolean;
hasError?: boolean;
orderNotes?: string;
}

/**
* All the actions that can be dispatched for the checkout.
*/
export const actions = {
setPristine: () =>
( {
type: ACTION.SET_PRISTINE,
} as const ),
setIdle: () =>
( {
type: ACTION.SET_IDLE,
} as const ),
setProcessing: () =>
( {
type: ACTION.SET_PROCESSING,
} as const ),
setRedirectUrl: ( url: string ) =>
( {
type: ACTION.SET_REDIRECT_URL,
url,
} as const ),
setProcessingResponse: ( data: PaymentResultDataType ) =>
( {
type: ACTION.SET_PROCESSING_RESPONSE,
data,
} as const ),
setComplete: ( data: Record< string, unknown > = {} ) =>
( {
type: ACTION.SET_COMPLETE,
data,
} as const ),
setBeforeProcessing: () =>
( {
type: ACTION.SET_BEFORE_PROCESSING,
} as const ),
setAfterProcessing: () =>
( {
type: ACTION.SET_AFTER_PROCESSING,
} as const ),
setHasError: ( hasError = true ) =>
( {
type: hasError ? ACTION.SET_HAS_ERROR : ACTION.SET_NO_ERROR,
} as const ),
incrementCalculating: () =>
( {
type: ACTION.INCREMENT_CALCULATING,
} as const ),
decrementCalculating: () =>
( {
type: ACTION.DECREMENT_CALCULATING,
} as const ),
setCustomerId: ( customerId: number ) =>
( {
type: ACTION.SET_CUSTOMER_ID,
customerId,
} as const ),
setOrderId: ( orderId: number ) =>
( {
type: ACTION.SET_ORDER_ID,
orderId,
} as const ),
setShouldCreateAccount: ( shouldCreateAccount: boolean ) =>
( {
type: ACTION.SET_SHOULD_CREATE_ACCOUNT,
shouldCreateAccount,
} as const ),
setOrderNotes: ( orderNotes: string ) =>
( {
type: ACTION.SET_ORDER_NOTES,
orderNotes,
} as const ),
};

This file was deleted.

Loading

0 comments on commit 5f84e7d

Please sign in to comment.