Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Aug 4, 2022
1 parent f1de0e8 commit b019462
Showing 1 changed file with 79 additions and 80 deletions.
159 changes: 79 additions & 80 deletions packages/core-data/src/hooks/use-entity-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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 );
}

0 comments on commit b019462

Please sign in to comment.