8000 feat(builder): confirm before discarding all unpublished changes by Klehm · Pull Request #2 · Klehm/content-blocks-project · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export default class extends Controller {
/** Confirm prompt shown before applying a destructive replace. */
static REPLACE_PICKER_CONFIRM_FALLBACK =
'Are you sure you want to overwrite the current content with the selected one?';
/** Confirm prompt shown before discarding all unpublished draft changes. */
static DISCARD_CONFIRM_FALLBACK =
'Are you sure you want to discard all unpublished changes? This cannot be undone.';

static SIDEBAR_WIDTH_KEY = 'cb-builder.sidebarWidth';
static SIDEBAR_COLLAPSED_KEY = 'cb-builder.sidebarCollapsed';
Expand Down Expand Up @@ -272,6 +275,12 @@ export default class extends Controller {

async discard(event) {
if (event) event.preventDefault();
// Discard throws away every unpublished edit at once — far more
// destructive than a single delete (which has its own Undo snackbar)
// and irreversible. Gate it behind a native confirm, same as the
// "Insert content" replace flow.
const confirmText = this._t('cb.builder.discard_confirm', this.constructor.DISCARD_CONFIRM_FALLBACK);
if (!window.confirm(confirmText)) return;
const result = await this._jsonRequest('POST', `/_content-blocks/area/${this.areaIdValue}/discard`);
if (result === null) return;
// Discard already reverted every draft deletion (or removed
Expand Down Expand Up @@ -1278,15 +1287,19 @@ export default class extends Controller {
/**
* Tiny translation lookup. The host's translation strings are not
* available client-side; we read precomputed values from data-*
* attributes on any picker root that carries them, otherwise fall back
* to the English default. This keeps the bundle dependency-free while
* still letting hosts override the wording.
* attributes on any picker root that carries them, plus the shell root
* itself for topbar-level strings (e.g. the discard confirm) that live
* outside the optional pickers. Falls back to the English default. This
* keeps the bundle dependency-free while still letting hosts override the
* wording.
*/
_t(key, fallback) {
const attr = 'data-i18n-' + key.replace(/[._]/g, '-');
const sources = [];
if (this.hasReplacePickerTarget) sources.push(this.replacePickerTarget);
if (this.hasImportExportPickerTarget) sources.push(this.importExportPickerTarget);
// Shell root: always present, carries topbar strings the pickers don't.
sources.push(this.element);
for (const el of sources) {
const value = el.getAttribute(attr);
if (value && value.length > 0) return value;
Expand Down
24 changes: 23 additions & 1 deletion packages/content-blocks/assets/test/cb-builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,8 @@ describe('cb-builder: publish/discard', () => {
expect(reloadSpy).toHaveBeenCalled();
});

it('discard posts to area/{id}/discard, applies state, reloads', async () => {
it('discard posts to area/{id}/discard, applies state, reloads (when confirmed)', async () => {
vi.spyOn(window, 'confirm').mockReturnValue(true);
reqSpy.mockResolvedValue({ hasUnpublishedChanges: false });

await controller.discard({ preventDefault: () => {} });
Expand All @@ -933,6 +934,26 @@ describe('cb-builder: publish/discard', () => {
expect(reloadSpy).toHaveBeenCalled();
});

it('discard asks for confirmation and does nothing when declined', async () => {
const confirmSpy = vi.spyOn(window, 'confirm').mockReturnValue(false);

await controller.discard({ preventDefault: () => {} });

expect(confirmSpy).toHaveBeenCalled();
expect(reqSpy).not.toHaveBeenCalled();
expect(applySpy).not.toHaveBeenCalled();
expect(reloadSpy).not.toHaveBeenCalled();
});

it('discard confirm prompt uses the localized text from the shell root', async () => {
controller.element.setAttribute('data-i18n-cb-builder-discard-confirm', 'Tout annuler ?');
const confirmSpy = vi.spyOn(window, 'confirm').mockReturnValue(false);

await controller.discard({ preventDefault: () => {} });

expect(confirmSpy).toHaveBeenCalledWith('Tout annuler ?');
});

it('publish does not act when the request fails', async () => {
reqSpy.mockResolvedValue(null);

Expand Down Expand Up @@ -1352,6 +1373,7 @@ describe('cb-builder: undo delete snackbar', () => {

await controller._deleteBlock(8);
expect(undoBar.hidden).toBe(false);
vi.spyOn(window, 'confirm').mockReturnValue(true); // discard is now gated by a confirm
await controller.discard();
expect(undoBar.hidden).toBe(true);
expect(controller._pendingUndo).toBeNull();
Expand Down
21 changes: 20 additions & 1 deletion packages/content-blocks/assets/test/e2e/builder-shell.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,8 @@ test.describe('builder shell — publish / discard', () => {
await addFullSection(page, frame);
expect(await frame.locator('[data-cb-section-id]').count()).toBe(1);

// Discard is gated behind a native confirm — accept it.
page.once('dialog', (dialog) => dialog.accept());
await page.locator('.cb-shell__discard').click();

// Section was added but never published → discardDraft removes it.
Expand All @@ -753,6 +755,21 @@ test.describe('builder shell — publish / discard', () => {
await expect(page.locator('.cb-shell__publish')).toBeDisabled();
});

test('Discard asks for confirmation; dismissing it keeps the draft', async ({ page }) => {
const frame = await openBuilder(page);
await addFullSection(page, frame);
expect(await frame.locator('[data-cb-section-id]').count()).toBe(1);

// Dismiss the confirm → the discard is aborted and nothing changes.
let dialogShown = false;
page.once('dialog', (dialog) => { dialogShown = true; dialog.dismiss(); });
await page.locator('.cb-shell__discard').click();

expect(dialogShown).toBe(true);
await expect(page.locator('.cb-shell__discard')).toBeVisible();
expect(await frame.locator('[data-cb-section-id]').count()).toBe(1);
});

test('Discard restores a soft-deleted block from a published area', async ({ page }) => {
const frame = await openBuilder(page);
await addFullSection(page, frame);
Expand All @@ -768,7 +785,9 @@ test.describe('builder shell — publish / discard', () => {
await expect.poll(() => frame.locator('[data-cb-block-id]').count()).toBe(0);
await expect(page.locator('.cb-shell__discard')).toBeVisible();

// Discard the soft-delete → the published block comes back.
// Discard the soft-delete → the published block comes back. Accept
// the confirm dialog that now guards the discard action.
page.once('dialog', (dialog) => dialog.accept());
await page.locator('.cb-shell__discard').click();

await expect.poll(() => frame.locator('[data-cb-block-id]').count()).toBe(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ test.describe('replace-content picker — happy path', () => {
await expect.poll(() => targetFrame.locator('[data-cb-section-id]').count()).toBe(1);

// Now discard — the target had no published content, so discardDraft
// should remove the cloned never-published sections entirely.
// should remove the cloned never-published sections entirely. Accept
// the confirm dialog that now guards the discard action.
page.once('dialog', async (d) => { await d.accept(); });
await page.locator('.cb-shell__discard').click();
await expect.poll(() => targetFrame.locator('[data-cb-section-id]').count()).toBe(0);
await expect(page.locator('.cb-shell__discard')).toBeHidden();
Expand Down
3 changes: 2 additions & 1 deletion packages/content-blocks/templates/builder/shell.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
data-controller="cb-builder"
data-cb-builder-area-id-value="{{ area.id }}"
data-cb-builder-iframe-url-value="{{ iframeUrl }}"
data-cb-csrf-token="{{ csrf_token('content_blocks') }}">
data-cb-csrf-token="{{ csrf_token('content_blocks') }}"
data-i18n-cb-builder-discard-confirm="{{ 'cb.builder.discard_confirm'|trans({}, 'content_blocks') }}">

<div class="cb-shell__progress" data-cb-builder-target="progress" aria-hidden="true"></div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ cb:
preview: "Content preview"
publish: "Publish"
discard: "Discard changes"
discard_confirm: "Are you sure you want to discard all unpublished changes? This cannot be undone."
add_section: "Add a section:"
empty_cta: "Start by adding a section"
draft_pending: "Draft pending"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ cb:
preview: "Aperçu du contenu"
publish: "Publier"
discard: "Annuler les modifications"
discard_confirm: "Voulez-vous vraiment annuler toutes les modifications non publiées ? Cette action est irréversible."
add_section: "Ajouter une section :"
empty_cta: "Commencez par ajouter une section"
draft_pending: "Brouillon en attente"
Expand Down
0