This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert checkout state context provider to Typescript (#4200)
* 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
1 parent
fccc72b
commit 5f84e7d
Showing
17 changed files
with
770 additions
and
629 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
82 changes: 0 additions & 82 deletions
82
assets/js/base/context/providers/cart-checkout/checkout-state/actions.js
This file was deleted.
Oops, something went wrong.
110 changes: 110 additions & 0 deletions
110
assets/js/base/context/providers/cart-checkout/checkout-state/actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ), | ||
}; |
53 changes: 0 additions & 53 deletions
53
assets/js/base/context/providers/cart-checkout/checkout-state/constants.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.