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
index.ts
106 lines
| 1 | /** |
| 2 | * External dependencies |
| 3 | */ |
| 4 | import { createReduxStore, register, select } from '@wordpress/data'; |
| 5 | /** |
| 6 | * Internal dependencies |
| 7 | */ |
| 8 | import actions from './actions'; |
| 9 | import reducer from './reducer'; |
| 10 | /** |
| 11 | * Types |
| 12 | */ |
| 13 | import type { AiFeatureProps, PlanStateProps } from './types'; |
| 14 | |
| 15 | const store = 'wordpress-com/plans'; |
| 16 | |
| 17 | const wordpressPlansStore = createReduxStore( store, { |
| 18 | __experimentalUseThunks: true, |
| 19 | |
| 20 | actions, |
| 21 | |
| 22 | reducer, |
| 23 | |
| 24 | selectors: { |
| 25 | /* |
| 26 | * Return the plan with the given slug. |
| 27 | * |
| 28 | * @param {Object} state - The Plans state tree. |
| 29 | * @param {string} planSlug - The plan slug to find. |
| 30 | * @return {Object} The plan. |
| 31 | */ |
| 32 | getPlan( state: PlanStateProps, planSlug: string ) { |
| 33 | return state.plans.find( plan => plan.product_slug === planSlug ); |
| 34 | }, |
| 35 | |
| 36 | /** |
| 37 | * Return the AI Assistant feature. |
| 38 | * |
| 39 | * @param {PlanStateProps} state - The Plans state tree. |
| 40 | * @returns {AiFeatureProps} The AI Assistant feature data. |
| 41 | */ |
| 42 | getAiAssistantFeature( state: PlanStateProps ): AiFeatureProps { |
| 43 | // Clean up the _meta property. |
| 44 | const data = { ...state.features.aiAssistant }; |
| 45 | delete data._meta; |
| 46 | |
| 47 | return data; |
| 48 | }, |
| 49 | |
| 50 | /** |
| 51 | * Get the isRequesting flag for the AI Assistant feature. |
| 52 | * |
| 53 | * @param {PlanStateProps} state - The Plans state tree. |
| 54 | * @returns {boolean} The isRequesting flag. |
| 55 | */ |
| 56 | getIsRequestingAiAssistantFeature( state: PlanStateProps ): boolean { |
| 57 | return state.features.aiAssistant?._meta?.isRequesting; |
| 58 | }, |
| 59 | |
| 60 | getAsyncRequestCountdownValue( state: PlanStateProps ): number { |
| 61 | return state.features.aiAssistant?._meta?.asyncRequestCountdown; |
| 62 | }, |
| 63 | |
| 64 | getAsyncRequestCountdownTimerId( state: PlanStateProps ): number { |
| 65 | return state.features.aiAssistant?._meta?.asyncRequestTimerId; |
| 66 | }, |
| 67 | }, |
| 68 | |
| 69 | controls: { |
| 70 | FETCH_FROM_API( { url } ) { |
| 71 | // We cannot use `@wordpress/api-fetch` here since it unconditionally sends |
| 72 | // the `X-WP-Nonce` header, which is disallowed by WordPress.com. |
| 73 | // (To reproduce, note that you need to call `apiFetch` with ` |
| 74 | // `{ credentials: 'same-origin', mode: 'cors' }`, since its defaults are |
| 75 | // different from `fetch`'s.) |
| 76 | return fetch( url ).then( response => response.json() ); |
| 77 | }, |
| 78 | }, |
| 79 | |
| 80 | resolvers: { |
| 81 | *getPlan() { |
| 82 | const url = 'https://public-api.wordpress.com/rest/v1.5/plans'; |
| 83 | const plans = yield actions.fetchFromAPI( url ); |
| 84 | return actions.setPlans( plans ); |
| 85 | }, |
| 86 | |
| 87 | getAiAssistantFeature: ( state: PlanStateProps ) => { |
| 88 | if ( state?.features?.aiAssistant ) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | return actions.fetchAiAssistantFeature(); |
| 93 | }, |
| 94 | }, |
| 95 | } ); |
| 96 | |
| 97 | register( wordpressPlansStore ); |
| 98 | |
| 99 | /* |
| 100 | * Ensure to request the AI Assistant feature data |
| 101 | * by calling the selector. Resolver will take care. |
| 102 | */ |
| 103 | if ( window.Jetpack_Editor_Initial_State?.[ 'ai-assistant' ]?.[ 'is-enabled' ] ) { |
| 104 | select( store ).getAiAssistantFeature(); |
| 105 | } |
| 106 |