diff --git a/packages/core-data/src/hooks/use-entity-record.ts b/packages/core-data/src/hooks/use-entity-record.ts index 482b280e9ef49..730e42d23e0e4 100644 --- a/packages/core-data/src/hooks/use-entity-record.ts +++ b/packages/core-data/src/hooks/use-entity-record.ts @@ -13,44 +13,44 @@ import { store as coreStore } from '../'; import type { Status } from './constants'; export interface EntityRecordResolution< RecordType > { - /** The requested entity record */ - record: RecordType | null; + /** The requested entity record */ + record: RecordType | null; - /** The edited entity record */ - editedRecord: Partial< RecordType >; + /** The edited entity record */ + editedRecord: Partial< RecordType >; - /** Apply edits to the edited entity record */ - edit: ( diff: Partial< RecordType > ) => void; + /** Apply edits to the edited entity record */ + edit: ( diff: Partial< RecordType > ) => void; - /** Persist the edits to the server */ - save: () => Promise< void >; + /** Persist the edits to the server */ + save: () => Promise< void >; - /** - * Is the record still being resolved? - */ - isResolving: boolean; + /** + * Is the record still being resolved? + */ + isResolving: boolean; - /** - * Does the record have any edits? - */ - hasEdits: boolean; + /** + * Does the record have any edits? + */ + hasEdits: boolean; - /** - * Is the record resolved by now? - */ - hasResolved: boolean; + /** + * Is the record resolved by now? + */ + hasResolved: boolean; - /** Resolution status */ - status: Status; + /** Resolution status */ + status: Status; } export interface Options { - /** - * Whether to run the query or short-circuit and return null. - * - * @default true - */ - enabled: boolean; + /** + * Whether to run the query or short-circuit and return null. + * + * @default true + */ + enabled: boolean; } /** @@ -138,61 +138,60 @@ export interface Options { * @template RecordType */ export default function useEntityRecord< RecordType >( - kind: string, - name: string, - recordId: string | number, - options: Options = { enabled: true } + kind: string, + name: string, + recordId: string | number, + options: Options = { enabled: true } ): EntityRecordResolution< RecordType > { - const { editEntityRecord, saveEditedEntityRecord } = useDispatch( - coreStore - ); - - const mutations = useMemo( - () => ( { - edit: ( record ) => - editEntityRecord( kind, name, recordId, record ), - save: ( saveOptions: any ) => - saveEditedEntityRecord( kind, name, recordId, saveOptions ), - } ), - [ recordId ] - ); - - const { editedRecord, hasEdits } = useSelect( - ( select ) => ( { - editedRecord: select( coreStore ).getEditedEntityRecord(), - hasEdits: select( coreStore ).hasEditsForEntityRecord(), - } ), - [ kind, name, recordId ] - ); - - const { data: record, ...querySelectRest } = useQuerySelect( - ( query ) => { - if ( ! options.enabled ) { - return null; - } - return query( coreStore ).getEntityRecord( kind, name, recordId ); - }, - [ kind, name, recordId, options.enabled ] - ); - - return { - record, - editedRecord, - hasEdits, - ...querySelectRest, - ...mutations, - }; + const { editEntityRecord, saveEditedEntityRecord } = + useDispatch( coreStore ); + + const mutations = useMemo( + () => ( { + edit: ( record ) => + editEntityRecord( kind, name, recordId, record ), + save: ( saveOptions: any ) => + saveEditedEntityRecord( kind, name, recordId, saveOptions ), + } ), + [ recordId ] + ); + + const { editedRecord, hasEdits } = useSelect( + ( select ) => ( { + editedRecord: select( coreStore ).getEditedEntityRecord(), + hasEdits: select( coreStore ).hasEditsForEntityRecord(), + } ), + [ kind, name, recordId ] + ); + + const { data: record, ...querySelectRest } = useQuerySelect( + ( query ) => { + if ( ! options.enabled ) { + return null; + } + return query( coreStore ).getEntityRecord( kind, name, recordId ); + }, + [ kind, name, recordId, options.enabled ] + ); + + return { + record, + editedRecord, + hasEdits, + ...querySelectRest, + ...mutations, + }; } export function __experimentalUseEntityRecord( - kind: string, - name: string, - recordId: any, - options: any + kind: string, + name: string, + recordId: any, + options: any ) { - deprecated( `wp.data.__experimentalUseEntityRecord`, { - alternative: 'wp.data.useEntityRecord', - since: '6.1', - } ); - return useEntityRecord( kind, name, recordId, options ); + deprecated( `wp.data.__experimentalUseEntityRecord`, { + alternative: 'wp.data.useEntityRecord', + since: '6.1', + } ); + return useEntityRecord( kind, name, recordId, options ); }