Skip to content

Commit

Permalink
Propose four hooks that also expose status
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Jan 27, 2022
1 parent c723804 commit 5948110
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 45 deletions.
4 changes: 4 additions & 0 deletions packages/core-data/src/hooks/constants.js
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';
56 changes: 56 additions & 0 deletions packages/core-data/src/hooks/use-entity-record-builder.js
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 packages/core-data/src/hooks/use-entity-record-mutation.js
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,
};
}
53 changes: 53 additions & 0 deletions packages/core-data/src/hooks/use-entity-record.js
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,
};
}
15 changes: 15 additions & 0 deletions packages/core-data/src/hooks/use-entity-records.js
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 )
);
}
45 changes: 0 additions & 45 deletions packages/core-data/src/use-entity-mutation.js

This file was deleted.

0 comments on commit 5948110

Please sign in to comment.