Skip to content

Commit

Permalink
Fix double reloading the file editor after publish or unpublish
Browse files Browse the repository at this point in the history
Fix unpublish (by submit) was loading the old form state
Enhancement added a pristine and dirty state for save and publish buttons
  • Loading branch information
Christopher Joe authored and Damian Mooyman committed Sep 5, 2017
1 parent 3bf1769 commit 809528b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions client/src/containers/AssetAdmin/AssetAdmin.js
Expand Up @@ -356,9 +356,6 @@ class AssetAdmin extends SilverStripeComponent {
// open the containing folder, since folder edit mode isn't desired
if (action === 'action_createfolder' && this.props.type !== 'admin') {
this.handleOpenFolder(this.getFolderId());
} else if (action === 'action_publish') {
this.handleCloseFile();
this.handleOpenFile(response.record.id);
}
return response;
});
Expand Down Expand Up @@ -447,6 +444,7 @@ class AssetAdmin extends SilverStripeComponent {
// see https://dev-blog.apollodata.com/apollo-clients-new-imperative-store-api-6cb69318a1e3
this.props.actions.files.readFiles()
.then(() => {
// @todo form action cannot use graphql due to this forced editor reloading
this.handleCloseFile();
this.handleOpenFile(response.data.unpublishFile.id);
});
Expand Down
8 changes: 5 additions & 3 deletions client/src/containers/Editor/Editor.js
Expand Up @@ -40,11 +40,13 @@ class Editor extends Component {
}

if (name === 'action_unpublish') {
const message = i18n._t('AssetAdmin.CONFIRMUNPUBLISH', 'Are you sure you want to unpublish this record?');
// eslint-disable-next-line no-alert
if (confirm(i18n._t('AssetAdmin.CONFIRMUNPUBLISH', 'Are you sure you want to unpublish this record?'))) {
this.props.onUnpublish(data.ID);
if (!confirm(message)) {
// @todo go back to using graphql when form schema state consistency can be achieved with graphql
// this.props.onUnpublish(data.ID);
event.preventDefault();
}
event.preventDefault();
return;
}

Expand Down
3 changes: 3 additions & 0 deletions code/Controller/AssetAdmin.php
Expand Up @@ -1096,6 +1096,9 @@ public function unpublish($data, $form)
}

$record->doUnpublish();

// regenerate form, so that it constants/literals on the form are updated
$form = $this->getFileEditForm($record->ID);
return $this->getRecordUpdatedResponse($record, $form);
}

Expand Down
17 changes: 15 additions & 2 deletions code/Forms/AssetFormFactory.php
Expand Up @@ -149,8 +149,21 @@ protected function getFormFieldTabs($record, $context = [])
protected function getSaveAction($record)
{
if ($record && $record->isInDB() && $record->canEdit()) {
return FormAction::create('save', _t('SilverStripe\\CMS\\Controllers\\CMSMain.SAVE', 'Save'))
->setIcon('save');
/** @var FormAction $action */
$action = FormAction::create('save', _t(__CLASS__.'.SAVE', 'Save'))
->setIcon('save')
->setSchemaState([
'data' => [
'pristineTitle' => _t(__CLASS__.'SAVED', 'Saved'),
'pristineIcon' => 'tick',
'dirtyTitle' => _t(__CLASS__.'SAVE', 'Save'),
'dirtyIcon' => 'save',
'pristineClass' => 'btn-outline-primary',
'dirtyClass' => '',
],
]);

return $action;
}
return null;
}
Expand Down
11 changes: 11 additions & 0 deletions code/Forms/FileFormFactory.php
Expand Up @@ -196,6 +196,17 @@ protected function getPublishAction($record)
/** @var FormAction $action */
$action = FormAction::create('publish', $publishText)
->setIcon('rocket')
->setSchemaState([
'data' => [
'isPublished' => $record->isPublished(),
'pristineTitle' => _t(__CLASS__.'PUBLISHED', 'Published'),
'pristineIcon' => 'tick',
'dirtyTitle' => _t(__CLASS__.'PUBLISH', 'Publish'),
'dirtyIcon' => 'rocket',
'pristineClass' => 'btn-outline-primary',
'dirtyClass' => '',
],
])
->setSchemaData(['data' => ['buttonStyle' => 'primary']]);

return $action;
Expand Down
4 changes: 4 additions & 0 deletions lang/en.yml
Expand Up @@ -51,6 +51,8 @@ en:
SilverStripe\AssetAdmin\GraphQL\FileFilterInputTypeCreator:
FileNotFound: 'File or Folder could not be found'
SilverStripe\AssetAdmin\Forms\AssetFormFactory:
SAVE: Save
SAVED: Saved
ACCESSHEADER: 'Who can view this file?'
ANYONE: Anyone
EDITHEADER: 'Who can edit this file?'
Expand All @@ -63,6 +65,8 @@ en:
ROOTNAME: '(Top level)'
VIEWERGROUPS: 'Viewer Groups'
SilverStripe\AssetAdmin\Forms\FileFormFactory:
PUBLISH: Publish
PUBLISHED: Published
INSERT_FILE: 'Insert file'
INSERT_LINK: 'Link to file'
LINKDESCR: 'Link description'
Expand Down

0 comments on commit 809528b

Please sign in to comment.