Migrate workflow and workflow tasks enable action to a Stimulus controller (#9844)

Co-authored-by: Thibaud Colas <thibaudcolas@gmail.com>
pull/9884/head
Lovelyfin00 2023-01-12 22:55:40 +01:00 zatwierdzone przez GitHub
rodzic 1e9f580b8c
commit 004faac53c
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
10 zmienionych plików z 110 dodań i 29 usunięć

Wyświetl plik

@ -113,6 +113,7 @@ Changelog
* Maintenance: Refactor userbar styles to use the same stylesheet as other components (Thibaud Colas)
* Maintenance: Add deprecation warnings for `wagtail.core` and other imports deprecated in Wagtail 3.0 (Matt Westcott)
* Maintenance: Migrate admin upgrade notification message implementation to a Stimulus controller (Loveth Omokaro)
* Maintenance: Migrate workflow and workflow tasks enable action to a Stimulus controller (Loveth Omokaro)
4.1.2 (xx.xx.xxxx) - IN DEVELOPMENT

Wyświetl plik

@ -21,6 +21,11 @@ exports[`TooltipEntity #openTooltip 1`] = `
<body
data-draftail-editor-wrapper="true"
>
<script
id="wagtail-config"
>
{"CSRF_TOKEN":"potato"}
</script>
<div
data-draftail-trigger="true"
/>
@ -35,6 +40,11 @@ exports[`TooltipEntity #openTooltip 1`] = `
"container": <body
data-draftail-editor-wrapper="true"
>
<script
id="wagtail-config"
>
{"CSRF_TOKEN":"potato"}
</script>
<div
data-draftail-trigger="true"
/>

Wyświetl plik

@ -0,0 +1,32 @@
import { Application } from '@hotwired/stimulus';
import { ActionController } from './ActionController';
describe('ActionController', () => {
beforeEach(() => {
document.body.innerHTML = `
<button
class="button no"
data-controller="w-action"
data-action="w-action#post"
data-w-action-url-value="https://www.github.com"
>
Enable
</button>
`;
Application.start().register('w-action', ActionController);
});
it('it should enable the workflow on click', () => {
const btn = document.querySelector('[data-controller="w-action"]');
const submitMock = jest.fn();
window.HTMLFormElement.prototype.submit = submitMock;
btn.click();
const form = document.querySelector('form');
expect(submitMock).toHaveBeenCalled();
expect(form.action).toBe('https://www.github.com/');
expect(new FormData(form).get('csrfmiddlewaretoken')).toBe('potato');
});
});

Wyświetl plik

@ -0,0 +1,46 @@
import { Controller } from '@hotwired/stimulus';
import { WAGTAIL_CONFIG } from '../config/wagtailConfig';
/**
* <button type="submit" class="button no"
* data-controller="w-action"
* data-action="click->w-action#post"
* data-w-action-redirect-value="true"
* data-w-action-url-value = '{{ view.get_enable_url }}'>Enable</button>
*/
export class ActionController extends Controller {
static values = {
redirect: String,
url: String,
};
urlValue: string;
redirectValue: any;
post(event: Event) {
event.preventDefault();
event.stopPropagation();
const formElement = document.createElement('form');
formElement.action = this.urlValue;
formElement.method = 'POST';
const csrftokenElement = document.createElement('input');
csrftokenElement.type = 'hidden';
csrftokenElement.name = 'csrfmiddlewaretoken';
csrftokenElement.value = WAGTAIL_CONFIG.CSRF_TOKEN;
formElement.appendChild(csrftokenElement);
if (this.redirectValue) {
const nextElement = document.createElement('input');
nextElement.type = 'hidden';
nextElement.name = 'next';
nextElement.value = window.location.href;
formElement.appendChild(nextElement);
}
document.body.appendChild(formElement);
formElement.submit();
}
}

Wyświetl plik

@ -1,5 +1,7 @@
import type { Definition } from '@hotwired/stimulus';
// Order controller imports alphabetically.
import { ActionController } from './ActionController';
import { AutoFieldController } from './AutoFieldController';
import { SkipLinkController } from './SkipLinkController';
import { UpgradeController } from './UpgradeController';
@ -8,6 +10,8 @@ import { UpgradeController } from './UpgradeController';
* Important: Only add default core controllers that should load with the base admin JS bundle.
*/
export const coreControllerDefinitions: Definition[] = [
// Keep this list alphabetized.
{ controllerConstructor: ActionController, identifier: 'w-action' },
{ controllerConstructor: AutoFieldController, identifier: 'w-auto-field' },
{ controllerConstructor: SkipLinkController, identifier: 'w-skip-link' },
{ controllerConstructor: UpgradeController, identifier: 'w-upgrade' },

Wyświetl plik

@ -32,6 +32,10 @@ global.wagtailConfig = {
ACTIVE_LOCALE: 'en',
};
document.body.innerHTML = `<script id="wagtail-config">${JSON.stringify({
CSRF_TOKEN: 'potato',
})}</script>`;
global.wagtailVersion = '1.6a1';
global.wagtail = {};

Wyświetl plik

@ -144,6 +144,7 @@ This feature was developed by Jake Howard.
* Refactor userbar styles to use the same stylesheet as other components (Thibaud Colas)
* Add deprecation warnings for `wagtail.core` and other imports deprecated in Wagtail 3.0 (Matt Westcott)
* Migrate admin upgrade notification message implementation to a Stimulus controller (Loveth Omokaro)
* Migrate workflow and workflow tasks enable action to a Stimulus controller (Loveth Omokaro)
## Upgrade considerations

Wyświetl plik

@ -12,7 +12,6 @@
{{ block.super }}
{% include "wagtailadmin/pages/_editor_js.html" %}
{{ media.js }}
{% include "wagtailadmin/workflows/includes/_edit_js.html" %}
{% endblock %}
{% block content %}
@ -59,7 +58,12 @@
<ul>
{% if can_enable %}
<li>
<button type="submit" class="button no" data-enable-action>{{ view.enable_item_label }}</button>
<button class="button no"
data-action="w-action#post"
data-controller="w-action"
data-w-action-url-value="{{ view.get_enable_url }}"
type="submit">{{ view.enable_item_label }}
</button>
</li>
{% elif can_disable %}
<li>

Wyświetl plik

@ -6,7 +6,6 @@
{% block extra_js %}
{{ block.super }}
{% include "wagtailadmin/pages/_editor_js.html" %}
{% include "wagtailadmin/workflows/includes/_edit_js.html" %}
{% endblock %}
{% block content %}
@ -50,7 +49,12 @@
<ul>
{% if can_enable %}
<li>
<button type="submit" class="button no" data-enable-action>{{ view.enable_item_label }}</button>
<button class="button no"
data-action="w-action#post"
data-controller="w-action"
data-w-action-url-value="{{ view.get_enable_url }}"
type="submit">{{ view.enable_item_label }}
</button>
</li>
{% elif can_disable %}
<li>

Wyświetl plik

@ -1,25 +0,0 @@
<script>
document.addEventListener('DOMContentLoaded', function() {
document.querySelectorAll('[data-enable-action]').forEach(function (buttonElement) {
buttonElement.addEventListener('click', function (e) {
// Stop the button from submitting the form
e.preventDefault();
e.stopPropagation();
var formElement = document.createElement('form');
formElement.action = '{{ view.get_enable_url }}';
formElement.method = 'POST';
var csrftokenElement = document.createElement('input');
csrftokenElement.type = 'hidden';
csrftokenElement.name = 'csrfmiddlewaretoken';
csrftokenElement.value = '{{ csrf_token|escapejs }}';
formElement.appendChild(csrftokenElement);
document.body.appendChild(formElement);
formElement.submit();
}, {capture: true});
})
});
</script>