-
Notifications
You must be signed in to change notification settings - Fork 835
Forms: add integrations store #45372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7699eed
Forms: add store for integrations
edanzer 4adbba3
changelog
edanzer bf6737e
Fix linting issues
edanzer 81bbaeb
Translate error message
edanzer 5572556
Revert "Translate error message"
edanzer 4284278
Translate error messages
edanzer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/packages/forms/changelog/add-forms-integrations-store
This file contains hidden or 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,4 @@ | ||
Significance: minor | ||
Type: added | ||
|
||
Forms: add integrations store. |
This file contains hidden or 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
4 changes: 4 additions & 0 deletions
4
projects/packages/forms/src/store/integrations/action-types.ts
This file contains hidden or 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,4 @@ | ||
export const RECEIVE_INTEGRATIONS = 'RECEIVE_INTEGRATIONS'; | ||
export const INVALIDATE_INTEGRATIONS = 'INVALIDATE_INTEGRATIONS'; | ||
export const SET_INTEGRATIONS_LOADING = 'SET_INTEGRATIONS_LOADING'; | ||
export const SET_INTEGRATIONS_ERROR = 'SET_INTEGRATIONS_ERROR'; |
This file contains hidden or 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,30 @@ | ||
import { | ||
RECEIVE_INTEGRATIONS, | ||
INVALIDATE_INTEGRATIONS, | ||
SET_INTEGRATIONS_LOADING, | ||
SET_INTEGRATIONS_ERROR, | ||
} from './action-types'; | ||
import { getIntegrations } from './resolvers'; | ||
import type { Integration } from '../../types'; | ||
|
||
export const receiveIntegrations = ( items: Integration[] ) => ( { | ||
type: RECEIVE_INTEGRATIONS, | ||
items, | ||
} ); | ||
|
||
export const invalidateIntegrations = () => ( { | ||
type: INVALIDATE_INTEGRATIONS, | ||
} ); | ||
|
||
export const setIntegrationsLoading = ( isLoading: boolean ) => ( { | ||
type: SET_INTEGRATIONS_LOADING, | ||
isLoading, | ||
} ); | ||
|
||
export const setIntegrationsError = ( error: string | null ) => ( { | ||
type: SET_INTEGRATIONS_ERROR, | ||
error, | ||
} ); | ||
|
||
// Thunk-like action to immediately refresh from the endpoint | ||
export const refreshIntegrations = () => getIntegrations(); |
This file contains hidden or 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,20 @@ | ||
import { createReduxStore, register } from '@wordpress/data'; | ||
import * as actions from './actions'; | ||
import reducer from './reducer'; | ||
import * as resolvers from './resolvers'; | ||
import * as selectors from './selectors'; | ||
|
||
export const INTEGRATIONS_STORE = 'jetpack/forms/integrations'; | ||
|
||
export const store = createReduxStore( INTEGRATIONS_STORE, { | ||
reducer, | ||
actions, | ||
selectors, | ||
resolvers, | ||
} ); | ||
|
||
register( store ); | ||
|
||
export * from './actions'; | ||
export * from './selectors'; | ||
export * from './types'; |
This file contains hidden or 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,56 @@ | ||
import { __ } from '@wordpress/i18n'; | ||
import { | ||
RECEIVE_INTEGRATIONS, | ||
INVALIDATE_INTEGRATIONS, | ||
SET_INTEGRATIONS_LOADING, | ||
SET_INTEGRATIONS_ERROR, | ||
} from './action-types'; | ||
import type { IntegrationsState, IntegrationsAction } from './types'; | ||
|
||
const DEFAULT_STATE: IntegrationsState = { | ||
items: null, | ||
isLoading: false, | ||
error: null, | ||
}; | ||
|
||
/** | ||
* Integrations store reducer. | ||
* | ||
* @param state - Current state | ||
* @param action - Dispatched action | ||
* @return Updated state | ||
*/ | ||
export default function reducer( | ||
state: IntegrationsState = DEFAULT_STATE, | ||
action: IntegrationsAction | ||
): IntegrationsState { | ||
switch ( action.type ) { | ||
case SET_INTEGRATIONS_LOADING: | ||
return { | ||
...state, | ||
isLoading: !! action.isLoading, | ||
error: action.isLoading ? null : state.error, | ||
}; | ||
case SET_INTEGRATIONS_ERROR: | ||
return { | ||
...state, | ||
isLoading: false, | ||
error: action.error ?? __( 'Unknown error', 'jetpack-forms' ), | ||
}; | ||
case RECEIVE_INTEGRATIONS: | ||
return { | ||
...state, | ||
items: action.items, | ||
isLoading: false, | ||
error: null, | ||
}; | ||
case INVALIDATE_INTEGRATIONS: | ||
return { | ||
...state, | ||
items: null, | ||
isLoading: false, | ||
}; | ||
default: | ||
return state; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
projects/packages/forms/src/store/integrations/resolvers.ts
This file contains hidden or 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,27 @@ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { addQueryArgs } from '@wordpress/url'; | ||
import { INVALIDATE_INTEGRATIONS } from './action-types'; | ||
import { receiveIntegrations, setIntegrationsError, setIntegrationsLoading } from './actions'; | ||
import type { IntegrationsAction } from './types'; | ||
import type { Integration } from '../../types'; | ||
|
||
export const getIntegrations = | ||
() => | ||
async ( { dispatch }: { dispatch: ( action: IntegrationsAction ) => void } ) => { | ||
dispatch( setIntegrationsLoading( true ) ); | ||
try { | ||
const path = addQueryArgs( '/wp/v2/feedback/integrations', { version: 2 } ); | ||
const result = await apiFetch< Integration[] >( { path } ); | ||
dispatch( receiveIntegrations( result ) ); | ||
} catch ( e ) { | ||
const message = e instanceof Error ? e.message : __( 'Unknown error', 'jetpack-forms' ); | ||
dispatch( setIntegrationsError( message ) ); | ||
} finally { | ||
dispatch( setIntegrationsLoading( false ) ); | ||
} | ||
}; | ||
|
||
// Attach invalidation rule | ||
getIntegrations.shouldInvalidate = ( action: IntegrationsAction ) => | ||
action.type === INVALIDATE_INTEGRATIONS; |
This file contains hidden or 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,6 @@ | ||
import type { IntegrationsState } from './types'; | ||
import type { Integration } from '../../types'; | ||
|
||
export const getIntegrations = ( state: IntegrationsState ): Integration[] | null => state.items; | ||
export const isIntegrationsLoading = ( state: IntegrationsState ): boolean => state.isLoading; | ||
export const getIntegrationsError = ( state: IntegrationsState ): string | null => state.error; |
This file contains hidden or 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,28 @@ | ||
import { INTEGRATIONS_STORE } from '.'; | ||
import type { Integration } from '../../types'; | ||
|
||
export type IntegrationsState = { | ||
items: Integration[] | null; | ||
isLoading: boolean; | ||
error: string | null; | ||
}; | ||
|
||
export type IntegrationsAction = { | ||
type: string; | ||
items?: Integration[]; | ||
isLoading?: boolean; | ||
error?: string | null; | ||
}; | ||
|
||
export type IntegrationsSelectors = { | ||
getIntegrations: () => Integration[] | null; | ||
isIntegrationsLoading: () => boolean; | ||
getIntegrationsError: () => string | null; | ||
}; | ||
|
||
export type IntegrationsDispatch = { | ||
refreshIntegrations: () => Promise< void >; | ||
invalidateIntegrations: () => void; | ||
}; | ||
|
||
export type SelectIntegrations = ( store: typeof INTEGRATIONS_STORE ) => IntegrationsSelectors; |
4 changes: 4 additions & 0 deletions
4
projects/plugins/jetpack/changelog/add-forms-integrations-store
This file contains hidden or 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,4 @@ | ||
Significance: minor | ||
Type: enhancement | ||
|
||
Forms: add integrations store. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit of a semantic issue. I am not really sure what the correct way is so feel free to ignore it.
I just noticed that in gutenberg we sometime use isResolving see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-data/#isresolving
Should we use that instead of isLoading?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good thought, but I think this wouldn't quite work. My understanding is that isResolving only tracks when resolvers run for a specific selector (e.g., getIntegrations). It wouldn't cover the refreshIntegrations 'thunk' that we added in this PR so we can actively trigger refreshes, so we'd lose loading state during those refreshes I think. And the refreshes are pretty key in a lot of places.