actions.ts
2 years ago
constants.ts
2 years ago
index.ts
2 years ago
reducer.ts
2 years ago
types.ts
2 years ago
actions.ts
196 lines
| 1 | /** |
| 2 | * External dependencies |
| 3 | */ |
| 4 | import apiFetch from '@wordpress/api-fetch'; |
| 5 | /** |
| 6 | * Types & Constants |
| 7 | */ |
| 8 | import { |
| 9 | ACTION_DECREASE_NEW_ASYNC_REQUEST_COUNTDOWN, |
| 10 | ACTION_DEQUEUE_ASYNC_REQUEST, |
| 11 | ACTION_ENQUEUE_ASYNC_REQUEST, |
| 12 | ACTION_FETCH_FROM_API, |
| 13 | ACTION_INCREASE_AI_ASSISTANT_REQUESTS_COUNT, |
| 14 | ACTION_REQUEST_AI_ASSISTANT_FEATURE, |
| 15 | ACTION_SET_PLANS, |
| 16 | ACTION_SET_AI_ASSISTANT_FEATURE_REQUIRE_UPGRADE, |
| 17 | ACTION_STORE_AI_ASSISTANT_FEATURE, |
| 18 | ENDPOINT_AI_ASSISTANT_FEATURE, |
| 19 | NEW_ASYNC_REQUEST_TIMER_INTERVAL, |
| 20 | ACTION_SET_TIER_PLANS_ENABLED, |
| 21 | } from './constants'; |
| 22 | import type { Plan } from './types'; |
| 23 | import type { AiFeatureProps } from './types'; |
| 24 | import type { SiteAIAssistantFeatureEndpointResponseProps } from '../../types'; |
| 25 | |
| 26 | /** |
| 27 | * Map the response from the `sites/$site/ai-assistant-feature` |
| 28 | * endpoint to the AI Assistant feature props. |
| 29 | * @param { SiteAIAssistantFeatureEndpointResponseProps } response - The response from the endpoint. |
| 30 | * @returns { AiFeatureProps } The AI Assistant feature props. |
| 31 | */ |
| 32 | export function mapAiFeatureResponseToAiFeatureProps( |
| 33 | response: SiteAIAssistantFeatureEndpointResponseProps |
| 34 | ): AiFeatureProps { |
| 35 | return { |
| 36 | hasFeature: !! response[ 'has-feature' ], |
| 37 | isOverLimit: !! response[ 'is-over-limit' ], |
| 38 | requestsCount: response[ 'requests-count' ], |
| 39 | requestsLimit: response[ 'requests-limit' ], |
| 40 | requireUpgrade: !! response[ 'site-require-upgrade' ], |
| 41 | errorMessage: response[ 'error-message' ], |
| 42 | errorCode: response[ 'error-code' ], |
| 43 | upgradeType: response[ 'upgrade-type' ], |
| 44 | usagePeriod: { |
| 45 | currentStart: response[ 'usage-period' ]?.[ 'current-start' ], |
| 46 | nextStart: response[ 'usage-period' ]?.[ 'next-start' ], |
| 47 | requestsCount: response[ 'usage-period' ]?.[ 'requests-count' ] || 0, |
| 48 | }, |
| 49 | currentTier: response[ 'current-tier' ], |
| 50 | nextTier: response[ 'next-tier' ], |
| 51 | tierPlansEnabled: !! response[ 'tier-plans-enabled' ], |
| 52 | costs: response.costs, |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | const actions = { |
| 57 | setPlans( plans: Array< Plan > ) { |
| 58 | return { |
| 59 | type: ACTION_SET_PLANS, |
| 60 | plans, |
| 61 | }; |
| 62 | }, |
| 63 | |
| 64 | fetchFromAPI( url: string ) { |
| 65 | return { |
| 66 | type: ACTION_FETCH_FROM_API, |
| 67 | url, |
| 68 | }; |
| 69 | }, |
| 70 | |
| 71 | storeAiAssistantFeature( feature: AiFeatureProps ) { |
| 72 | return { |
| 73 | type: ACTION_STORE_AI_ASSISTANT_FEATURE, |
| 74 | feature, |
| 75 | }; |
| 76 | }, |
| 77 | |
| 78 | /** |
| 79 | * Thunk action to fetch the AI Assistant feature from the API. |
| 80 | * |
| 81 | * @returns {Function} The thunk action. |
| 82 | */ |
| 83 | fetchAiAssistantFeature() { |
| 84 | return async ( { dispatch } ) => { |
| 85 | // Dispatch isFetching action. |
| 86 | dispatch( { type: ACTION_REQUEST_AI_ASSISTANT_FEATURE } ); |
| 87 | |
| 88 | try { |
| 89 | const response: SiteAIAssistantFeatureEndpointResponseProps = await apiFetch( { |
| 90 | path: ENDPOINT_AI_ASSISTANT_FEATURE, |
| 91 | } ); |
| 92 | |
| 93 | // Store the feature in the store. |
| 94 | dispatch( |
| 95 | actions.storeAiAssistantFeature( mapAiFeatureResponseToAiFeatureProps( response ) ) |
| 96 | ); |
| 97 | } catch ( err ) { |
| 98 | // @todo: Handle error. |
| 99 | console.error( err ); // eslint-disable-line no-console |
| 100 | } |
| 101 | }; |
| 102 | }, |
| 103 | |
| 104 | /** |
| 105 | * This thunk action is used to increase |
| 106 | * the requests count for the current usage period. |
| 107 | * @param {number} count - The number of requests to increase. Default is 1. |
| 108 | * @returns {Function} The thunk action. |
| 109 | */ |
| 110 | increaseAiAssistantRequestsCount( count: number = 1 ) { |
| 111 | return ( { dispatch } ) => { |
| 112 | dispatch( { |
| 113 | type: ACTION_INCREASE_AI_ASSISTANT_REQUESTS_COUNT, |
| 114 | count, |
| 115 | } ); |
| 116 | |
| 117 | // Every time the requests count is increased, decrease the countdown |
| 118 | dispatch( actions.decreaseAsyncRequestCountdownValue() ); |
| 119 | }; |
| 120 | }, |
| 121 | |
| 122 | /** |
| 123 | * This thunk action is used to decrease |
| 124 | * the countdown value for the new async request. |
| 125 | * When the countdown reaches 0, enqueue a new async request. |
| 126 | * |
| 127 | * @returns {Function} The thunk action. |
| 128 | */ |
| 129 | decreaseAsyncRequestCountdownValue() { |
| 130 | return async ( { dispatch, select } ) => { |
| 131 | dispatch( { type: ACTION_DECREASE_NEW_ASYNC_REQUEST_COUNTDOWN } ); |
| 132 | |
| 133 | const asyncCoundown = select.getAsyncRequestCountdownValue(); |
| 134 | if ( asyncCoundown <= 0 ) { |
| 135 | dispatch( actions.enqueueAiAssistantFeatureAsyncRequest() ); |
| 136 | } |
| 137 | }; |
| 138 | }, |
| 139 | |
| 140 | /** |
| 141 | * This thunk action is used to enqueue a new async request. |
| 142 | * If already exist an enqueue request, clear it and enqueue a new one. |
| 143 | * |
| 144 | * @returns {Function} The thunk action. |
| 145 | */ |
| 146 | enqueueAiAssistantFeatureAsyncRequest() { |
| 147 | return ( { dispatch } ) => { |
| 148 | // Check if there is already a timer running |
| 149 | dispatch.dequeueAiAssistantFeatureAsyncRequest(); |
| 150 | |
| 151 | const contdownTimerId = setTimeout( () => { |
| 152 | dispatch( actions.fetchAiAssistantFeature() ); |
| 153 | }, NEW_ASYNC_REQUEST_TIMER_INTERVAL ); // backend process requires a delay to be able to see the new value |
| 154 | |
| 155 | dispatch( { type: ACTION_ENQUEUE_ASYNC_REQUEST, timerId: contdownTimerId } ); |
| 156 | }; |
| 157 | }, |
| 158 | |
| 159 | /** |
| 160 | * This thunk action is used to dequeue a new async request. |
| 161 | * It will clear the timer if there is one, |
| 162 | * canceling the enqueue async request. |
| 163 | * |
| 164 | * @returns {Function} The thunk action. |
| 165 | */ |
| 166 | dequeueAiAssistantFeatureAsyncRequest() { |
| 167 | return ( { dispatch, select } ) => { |
| 168 | dispatch( { type: ACTION_DEQUEUE_ASYNC_REQUEST, timerId: 0 } ); |
| 169 | |
| 170 | const timerId = select.getAsyncRequestCountdownTimerId(); |
| 171 | // If there is no timer, there is nothing to clear |
| 172 | if ( ! timerId ) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | window?.clearTimeout( timerId ); |
| 177 | }; |
| 178 | }, |
| 179 | |
| 180 | setAiAssistantFeatureRequireUpgrade( requireUpgrade: boolean = true ) { |
| 181 | return { |
| 182 | type: ACTION_SET_AI_ASSISTANT_FEATURE_REQUIRE_UPGRADE, |
| 183 | requireUpgrade, |
| 184 | }; |
| 185 | }, |
| 186 | |
| 187 | setTierPlansEnabled( tierPlansEnabled: boolean = true ) { |
| 188 | return { |
| 189 | type: ACTION_SET_TIER_PLANS_ENABLED, |
| 190 | tierPlansEnabled, |
| 191 | }; |
| 192 | }, |
| 193 | }; |
| 194 | |
| 195 | export default actions; |
| 196 |