Skip to content

Commit

Permalink
Update page link before submitting page form
Browse files Browse the repository at this point in the history
  • Loading branch information
charludo committed Feb 21, 2024
1 parent 9916ac8 commit 5082abf
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
12 changes: 9 additions & 3 deletions integreat_cms/cms/templates/events/event_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,24 @@ <h1 class="heading overflow-hidden text-ellipsis">
<div class="flex flex-wrap gap-4 ml-auto mr-0 items-center">
{% include "generic_auto_save_note.html" with form_instance=event_form.instance %}
{% if perms.cms.publish_event %}
<button name="status" value="{{ DRAFT }}" class="btn btn-outline">
<button name="status"
value="{{ DRAFT }}"
class="btn btn-outline no-premature-submission">
{% translate "Save as draft" %}
</button>
<button name="status" value="{{ PUBLIC }}" class="btn">
<button name="status"
value="{{ PUBLIC }}"
class="btn no-premature-submission">
{% if event_translation_form.instance.status == PUBLIC %}
{% translate "Update" %}
{% else %}
{% translate "Publish" %}
{% endif %}
</button>
{% else %}
<button name="status" value="{{ REVIEW }}" class="btn">
<button name="status"
value="{{ REVIEW }}"
class="btn no-premature-submission">
{% translate "Submit for approval" %}
</button>
{% endif %}
Expand Down
14 changes: 10 additions & 4 deletions integreat_cms/cms/templates/pages/page_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,29 @@ <h1 class="heading">
{% if not page_form.instance.id or not page_form.instance.archived %}
<div class="flex flex-wrap grow justify-end gap-2 items-center">
{% include "generic_auto_save_note.html" with form_instance=page_translation_form.instance %}
<button name="preview_page" type="button" data-preview-page data-edit-page-mode data-right-to-left={{ right_to_left }} class="btn btn-ghost">
<button name="preview_page" type="button" data-preview-page data-edit-page-mode data-right-to-left={{ right_to_left }} class="btn btn-ghost no-premature-submission">
{% translate "Preview" %}
</button>
{% has_perm 'cms.publish_page_object' request.user page as can_publish_page %}
{% if can_publish_page %}
<button name="status" value="{{ DRAFT }}" class="btn btn-outline">
<button name="status"
value="{{ DRAFT }}"
class="btn btn-outline no-premature-submission">
{% translate "Save as draft" %}
</button>
<button name="status" value="{{ PUBLIC }}" class="btn whitespace-nowrap">
<button name="status"
value="{{ PUBLIC }}"
class="btn whitespace-nowrap no-premature-submission">
{% if page_translation_form.instance.status == PUBLIC %}
{% translate "Update" %}
{% else %}
{% translate "Publish" %}
{% endif %}
</button>
{% elif can_edit_page %}
<button name="status" value="{{ REVIEW }}" class="btn">
<button name="status"
value="{{ REVIEW }}"
class="btn no-premature-submission">
{% translate "Submit for approval" %}
</button>
{% endif %}
Expand Down
6 changes: 4 additions & 2 deletions integreat_cms/cms/templates/pois/poi_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ <h1 class="heading overflow-hidden text-ellipsis">
{% if not poi_form.instance.archived and perms.cms.change_poi %}
<div class="flex flex-wrap gap-4 ml-auto mr-0 items-center">
{% include "generic_auto_save_note.html" with form_instance=poi_translation_form.instance %}
<button name="status" value="{{ DRAFT }}" class="btn btn-outline">
<button name="status"
value="{{ DRAFT }}"
class="btn btn-outline no-premature-submission">
{% translate "Save as draft" %}
</button>
<button name="status"
value="{{ PUBLIC }}"
class="btn"
class="btn no-premature-submission"
{% if language != request.region.default_language and not poi_form.instance.default_public_translation %} title="{% translate "The default translation is not yet published" %}" disabled {% endif %}>
{% if poi_translation_form.instance.status == PUBLIC %}
{% translate "Update" %}
Expand Down
26 changes: 26 additions & 0 deletions integreat_cms/static/src/js/forms/prevent-premature-submission.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default class SubmissionPrevention {
watchedElements: HTMLElement[] = [];
mostRecentlyClicked: HTMLElement = null;

preventSubmission = (e: Event) => {
e.preventDefault();
this.mostRecentlyClicked = e.target as HTMLElement;
};

constructor(identifier: string) {
const elements = document.querySelectorAll<HTMLElement>(identifier);
elements.forEach((element) => {
element.addEventListener("click", this.preventSubmission);
});
this.watchedElements = Array.from(elements);
}

release() {
this.watchedElements.forEach((element) => {
element.removeEventListener("click", this.preventSubmission);
});
if (this.mostRecentlyClicked !== null) {
this.mostRecentlyClicked.click();
}
}
}
3 changes: 3 additions & 0 deletions integreat_cms/static/src/js/forms/update-permalink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ or when the user clicks the permalinks edit button */

import { getCsrfToken } from "../utils/csrf-token";
import { copyToClipboard } from "../copy-clipboard";
import SubmissionPrevention from "./prevent-premature-submission";

const slugify = async (url: string, data: any) => {
const response = await fetch(url, {
Expand Down Expand Up @@ -44,12 +45,14 @@ window.addEventListener("load", () => {
(document.querySelector('[for="id_title"]') as HTMLElement)?.dataset?.slugifyUrl
) {
document.getElementById("id_title").addEventListener("focusout", ({ target }) => {
const submissionLock = new SubmissionPrevention(".no-premature-submission");
const currentTitle = (target as HTMLInputElement).value;
const url = (document.querySelector('[for="id_title"]') as HTMLElement).dataset.slugifyUrl;
slugify(url, { title: currentTitle }).then((response) => {
/* on success write response to both slug field and permalink */
slugField.value = response.unique_slug;
updatePermalink(response.unique_slug);
submissionLock.release();
});
});
}
Expand Down

0 comments on commit 5082abf

Please sign in to comment.