Skip to content
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

Core Data: Return updated record in saveEntityRecord. #17030

Merged
merged 1 commit into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export function* saveEntityRecord(
const recordId = record[ entityIdKey ];

yield { type: 'SAVE_ENTITY_RECORD_START', kind, name, recordId, isAutosave };
let updatedRecord;
let error;
try {
const path = `${ entity.baseURL }${ recordId ? '/' + recordId : '' }`;
Expand Down Expand Up @@ -260,14 +261,14 @@ export function* saveEntityRecord(
}
return acc;
}, {} );
const autosave = yield apiFetch( {
updatedRecord = yield apiFetch( {
path: `${ path }/autosaves`,
method: 'POST',
data,
} );
yield receiveAutosaves( persistedRecord.id, autosave );
yield receiveAutosaves( persistedRecord.id, updatedRecord );
} else {
const updatedRecord = yield apiFetch( {
updatedRecord = yield apiFetch( {
path,
method: recordId ? 'PUT' : 'POST',
data: record,
Expand All @@ -285,6 +286,8 @@ export function* saveEntityRecord(
error,
isAutosave,
};

return updatedRecord;
}

/**
Expand Down
14 changes: 12 additions & 2 deletions packages/core-data/src/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@ describe( 'saveEntityRecord', () => {
data: post,
} );
// Provide response and trigger action
const { value: received } = fulfillment.next( { ...post, id: 10 } );
expect( received ).toEqual( receiveEntityRecords( 'postType', 'post', { ...post, id: 10 }, undefined, true ) );
const updatedRecord = { ...post, id: 10 };
const { value: received } = fulfillment.next( updatedRecord );
expect( received ).toEqual(
receiveEntityRecords(
'postType',
'post',
updatedRecord,
undefined,
true
)
);
expect( fulfillment.next().value.type ).toBe( 'SAVE_ENTITY_RECORD_FINISH' );
expect( fulfillment.next().value ).toBe( updatedRecord );
} );

it( 'triggers a PUT request for an existing record', async () => {
Expand Down