-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propose four hooks that also expose status
- Loading branch information
Showing
6 changed files
with
199 additions
and
45 deletions.
There are no files selected for viewing
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,4 @@ | ||
export const IDLE = 'IDLE'; | ||
export const RESOLVING = 'RESOLVING'; | ||
export const ERROR = 'ERROR'; | ||
export const READY = 'READY'; |
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,56 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo, useState } from '@wordpress/element'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { IDLE, ERROR, RESOLVING } from './constants'; | ||
import { store as coreStore } from '../'; | ||
|
||
export function useEntityRecordBuilder( kind, type, id ) { | ||
const [ record, setRecord ] = useState( {} ); | ||
const { saveEntityRecord } = useDispatch( coreStore ); | ||
|
||
const mutations = useMemo( | ||
() => ( { | ||
edit: ( diff ) => setRecord( { ...record, ...diff } ), | ||
save: () => saveEntityRecord( kind, type, id, record ), | ||
reset: () => setRecord( {} ), | ||
} ), | ||
[ record ] | ||
); | ||
|
||
const state = useSelect( | ||
( select ) => { | ||
const args = [ kind, type ]; | ||
const { getLastEntitySaveError, isSavingEntityRecord } = select( | ||
coreStore | ||
); | ||
const error = getLastEntitySaveError( ...args ); | ||
const isSaving = isSavingEntityRecord( ...args ); | ||
|
||
let status; | ||
if ( isSaving ) { | ||
status = RESOLVING; | ||
} else if ( error ) { | ||
status = ERROR; | ||
} else { | ||
status = IDLE; | ||
} | ||
return { | ||
status, | ||
error, | ||
isSaving, | ||
}; | ||
}, | ||
[ kind, type ] | ||
); | ||
|
||
return { | ||
...mutations, | ||
...state, | ||
}; | ||
} |
71 changes: 71 additions & 0 deletions
71
packages/core-data/src/hooks/use-entity-record-mutation.js
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,71 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo } from '@wordpress/element'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as coreStore } from '../'; | ||
import { IDLE, ERROR, RESOLVING } from './constants'; | ||
|
||
export function useEntityRecordMutations( kind, type, id ) { | ||
const { | ||
editEntityRecord, | ||
saveEntityRecord, | ||
saveEditedEntityRecord, | ||
deleteEntityRecord, | ||
} = useDispatch( coreStore ); | ||
|
||
const mutations = useMemo( | ||
() => ( { | ||
edit: ( record ) => editEntityRecord( kind, type, id, record ), | ||
create: ( record ) => saveEntityRecord( kind, type, id, record ), | ||
save: () => saveEditedEntityRecord( kind, type, id ), | ||
delete: () => deleteEntityRecord( kind, type, id ), | ||
} ), | ||
[ id ] | ||
); | ||
|
||
const state = useSelect( | ||
( select ) => { | ||
const args = [ kind, type, id ]; | ||
const { | ||
getLastEntitySaveError, | ||
getLastEntityDeleteError, | ||
isSavingEntityRecord, | ||
isDeletingEntityRecord, | ||
} = select( coreStore ); | ||
|
||
const saveError = getLastEntitySaveError( ...args ); | ||
const deleteError = getLastEntityDeleteError( ...args ); | ||
const error = saveError || deleteError; | ||
|
||
const isSaving = isSavingEntityRecord( ...args ); | ||
const isDeleting = isDeletingEntityRecord( ...args ); | ||
|
||
let status; | ||
if ( isSaving || isDeleting ) { | ||
status = RESOLVING; | ||
} else if ( error ) { | ||
status = ERROR; | ||
} else { | ||
status = IDLE; | ||
} | ||
|
||
return { | ||
status, | ||
error, | ||
isSaving, | ||
isDeleting, | ||
}; | ||
}, | ||
[ kind, type, id ] | ||
); | ||
|
||
return { | ||
...mutations, | ||
...state, | ||
}; | ||
} |
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,53 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect, useQuerySelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as coreStore } from '../'; | ||
import { IDLE, READY, RESOLVING } from './constants'; | ||
|
||
export function useEntityRecord( kind, type, id ) { | ||
const { | ||
data, | ||
isResolving, | ||
hasFinished: isResolved, | ||
} = useQuerySelect( ( resolve ) => | ||
resolve( coreStore ).getEntityRecord( kind, type, id ) | ||
); | ||
|
||
let status; | ||
if ( isResolving ) { | ||
status = RESOLVING; | ||
} else if ( isResolved || data ) { | ||
status = READY; | ||
} else { | ||
status = IDLE; | ||
} | ||
|
||
const { editedRecord, hasEdits } = useSelect( | ||
( select ) => { | ||
const { getEditedEntityRecord, hasEditsForEntityRecord } = select( | ||
coreStore | ||
); | ||
|
||
const args = [ kind, type, id ]; | ||
return { | ||
editedRecord: getEditedEntityRecord( ...args ), | ||
hasEdits: hasEditsForEntityRecord( ...args ), | ||
}; | ||
}, | ||
[ kind, type, id ] | ||
); | ||
|
||
return { | ||
status, | ||
record: data, | ||
editedRecord, | ||
hasEdits, | ||
isResolving, | ||
isResolved, | ||
}; | ||
} |
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,15 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useQuerySelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as coreStore } from '../'; | ||
|
||
export function useEntityRecords( kind, type, id, query ) { | ||
return useQuerySelect( ( resolve ) => | ||
resolve( coreStore ).getEntityRecords( kind, type, id, query ) | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.