Skip to content

Commit

Permalink
Edit Site: Add multiple entity saving functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
epiqueras committed Dec 17, 2019
1 parent 5c84474 commit 2e171f9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/edit-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@wordpress/components": "file:../components",
"@wordpress/core-data": "file:../core-data",
"@wordpress/data": "file:../data",
"@wordpress/editor": "file:../editor",
"@wordpress/element": "file:../element",
"@wordpress/hooks": "file:../hooks",
"@wordpress/i18n": "file:../i18n",
Expand Down
8 changes: 8 additions & 0 deletions packages/edit-site/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import SaveButton from '../save-button';

export default function Header() {
return (
<div
Expand All @@ -14,6 +19,9 @@ export default function Header() {
<h1 className="edit-site-header__title">
{ __( 'Site Editor' ) } { __( '(beta)' ) }
</h1>
<div className="edit-site-header__actions">
<SaveButton />
</div>
</div>
);
}
4 changes: 4 additions & 0 deletions packages/edit-site/src/components/header/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@
font-size: 16px;
padding: 0 20px;
}

.edit-site-header__actions {
padding: 0 20px;
}
56 changes: 56 additions & 0 deletions packages/edit-site/src/components/save-button/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* WordPress dependencies
*/
import { useEntityProp } from '@wordpress/core-data';
import { useEffect, useState, useCallback } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { EntitiesSavedStates } from '@wordpress/editor';

export default function SaveButton() {
const [ , setStatus ] = useEntityProp( 'postType', 'wp_template', 'status' );
// Publish template if not done yet.
useEffect( () => setStatus( 'publish' ), [] );

const { isDirty, isSaving } = useSelect( ( select ) => {
const { getEntityRecordChangesByRecord, isSavingEntityRecord } = select(
'core'
);
const entityRecordChangesByRecord = getEntityRecordChangesByRecord();
const changedKinds = Object.keys( entityRecordChangesByRecord );
return {
isDirty: changedKinds.length > 0,
isSaving: changedKinds.some( ( changedKind ) =>
Object.keys(
entityRecordChangesByRecord[ changedKind ]
).some( ( changedName ) =>
Object.keys(
entityRecordChangesByRecord[ changedKind ][ changedName ]
).some( ( changedKey ) =>
isSavingEntityRecord( changedKind, changedName, changedKey )
)
)
),
};
} );
const disabled = ! isDirty || isSaving;

const [ isOpen, setIsOpen ] = useState( false );
const open = useCallback( setIsOpen.bind( null, true ), [] );
const close = useCallback( setIsOpen.bind( null, false ), [] );
return (
<>
<Button
isPrimary
aria-disabled={ disabled }
disabled={ disabled }
isBusy={ isSaving }
onClick={ disabled ? undefined : open }
>
{ __( 'Update' ) }
</Button>
<EntitiesSavedStates isOpen={ isOpen } onRequestClose={ close } />
</>
);
}

0 comments on commit 2e171f9

Please sign in to comment.