Skip to content

Commit

Permalink
[5.x] Prevent published toast when save failed (#10263)
Browse files Browse the repository at this point in the history
Co-authored-by: Duncan McClean <duncan@duncanmcclean.com>
  • Loading branch information
simonerd and duncanmcclean authored Aug 12, 2024
1 parent b681fd9 commit 302c69d
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 2 deletions.
12 changes: 12 additions & 0 deletions resources/js/components/entries/PublishActions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ export default {
this.$axios.post(this.actions.publish, payload)
.then(response => {
this.saving = false;
if (! response.data.saved) {
this.$emit('failed');
return this.$toast.error(__(`Couldn't publish entry`));
}
this.$toast.success(__('Published'));
this.runAfterPublishHook(response);
}).catch(error => this.handleAxiosError(error));
Expand Down Expand Up @@ -187,6 +192,13 @@ export default {
const payload = { message: this.revisionMessage };
this.$axios.post(this.actions.unpublish, { data: payload }).then(response => {
this.saving = false;
if (! response.data.saved) {
this.$emit('failed');
return this.$toast.error(__(`Couldn't unpublish entry`));
}
this.$toast.success(__('Unpublished'));
this.revisionMessage = null;
this.$emit('saved', { published: false, isWorkingCopy: false, response });
Expand Down
6 changes: 6 additions & 0 deletions resources/js/components/entries/PublishForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@
@closed="confirmingPublish = false"
@saving="saving = true"
@saved="publishActionCompleted"
@failed="publishActionFailed"
/>

<confirmation-modal
Expand Down Expand Up @@ -810,6 +811,11 @@ export default {
}
},
publishActionFailed() {
this.confirmPublish = false;
this.saving = false;
},
setFieldValue(handle, value) {
if (this.hasOrigin) this.desyncField(handle);
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,10 @@
"Copy Password Reset Link": "Passwort Zurücksetzen Link kopieren",
"Copy URL": "URL kopieren",
"Core": "Core",
"Couldn't publish entry": "Eintrag konnte nicht veröffentlicht werden",
"Couldn't save entry": "Eintrag konnte nicht gespeichert werden",
"Couldn't save term": "Begriff konnte nicht gespeichert werden",
"Couldn't unpublish entry": "Veröffentlichung konnte nicht aufgehoben werden",
"CP Nav Preferences": "Voreinstellungen CP Navigation",
"Create": "Erstellen",
"Create & Link Item": "Eintrag erstellen & verknüpfen",
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/de_CH.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,10 @@
"Copy Password Reset Link": "Passwort Zurücksetzen Link kopieren",
"Copy URL": "URL kopieren",
"Core": "Core",
"Couldn't publish entry": "Eintrag konnte nicht veröffentlicht werden",
"Couldn't save entry": "Eintrag konnte nicht gespeichert werden",
"Couldn't save term": "Begriff konnte nicht gespeichert werden",
"Couldn't unpublish entry": "Veröffentlichung konnte nicht aufgehoben werden",
"CP Nav Preferences": "Voreinstellungen CP Navigation",
"Create": "Erstellen",
"Create & Link Item": "Eintrag erstellen & verknüpfen",
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,10 @@
"Copy Password Reset Link": "Copier le lien de réinitialisation du mot de passe",
"Copy URL": "Copier l'URL",
"Core": "Core",
"Couldn't publish entry": "Impossible de publier l'entrée",
"Couldn't save entry": "Impossible d'enregistrer l'entrée",
"Couldn't save term": "Impossible d'enregistrer le terme",
"Couldn't unpublish entry": "Impossible de dépublier l'entrée",
"CP Nav Preferences": "Préférences de navigation du CP",
"Create": "Créer",
"Create & Link Item": "Créer et lier l'élément",
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,10 @@
"Copy Password Reset Link": "Kopieer wachtwoordresetlink",
"Copy URL": "Kopieer URL",
"Core": "Core",
"Couldn't publish entry": "Kon de entry niet publiceren",
"Couldn't save entry": "Kon de entry niet opslaan",
"Couldn't save term": "Kon de term niet opslaan",
"Couldn't unpublish entry": "Kon de entry niet depubliceren",
"CP Nav Preferences": "CP Nav Instellingen",
"Create": "Aanmaken",
"Create & Link Item": "Item aanmaken & linken",
Expand Down
10 changes: 10 additions & 0 deletions src/Http/Controllers/CP/Collections/PublishedEntriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public function store(Request $request, $collection, $entry)
'user' => User::fromUser($request->user()),
]);

if ($entry === false) {
return ['saved' => false];
}

$blueprint = $entry->blueprint();

[$values] = $this->extractFromFields($entry, $blueprint);
Expand All @@ -28,6 +32,7 @@ public function store(Request $request, $collection, $entry)
'data' => array_merge((new EntryResource($entry->fresh()))->resolve()['data'], [
'values' => $values,
]),
'saved' => true,
];
}

Expand All @@ -40,6 +45,10 @@ public function destroy(Request $request, $collection, $entry)
'user' => User::fromUser($request->user()),
]);

if ($entry === false) {
return ['saved' => false];
}

$blueprint = $entry->blueprint();

[$values] = $this->extractFromFields($entry, $blueprint);
Expand All @@ -48,6 +57,7 @@ public function destroy(Request $request, $collection, $entry)
'data' => array_merge((new EntryResource($entry->fresh()))->resolve()['data'], [
'values' => $values,
]),
'saved' => true,
];
}
}
12 changes: 10 additions & 2 deletions src/Revisions/Revisable.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,15 @@ public function publishWorkingCopy($options = [])
$item->remove('parent');
}

$item
$saved = $item
->published(true)
->updateLastModified($user = $options['user'] ?? false)
->save();

if (! $saved) {
return false;
}

if ($item instanceof Entry && $item->collection()->hasStructure() && $parent) {
$tree = $item->collection()->structure()->in($item->locale());

Expand Down Expand Up @@ -111,11 +115,15 @@ public function unpublishWorkingCopy($options = [])
{
$item = $this->fromWorkingCopy();

$item
$saved = $item
->published(false)
->updateLastModified($user = $options['user'] ?? false)
->save();

if (! $saved) {
return false;
}

$item
->makeRevision()
->user($user)
Expand Down

0 comments on commit 302c69d

Please sign in to comment.