fix(pages): bring the unpublished-changes banner into view after save
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
1 parent
168529585a
commit
a0d10fc169
2 files changed
+13
-1
No files matched your search
@@ -56,8 +56,13 @@ describe("PageEditor drafts", () => {
|
||||
expect(screen.queryByText(/Visitors still see the published version/)).not.toBeInTheDocument();
|
||||
await user.clear(screen.getByLabelText("Title"));
|
||||
await user.type(screen.getByLabelText("Title"), "Hotel week");
|
||||
const scroll = vi.fn();
|
||||
Element.prototype.scrollIntoView = scroll;
|
||||
await user.click(screen.getByRole("button", { name: "Save" }));
|
||||
expect(await screen.findByText(/Visitors still see the published version/)).toBeInTheDocument();
|
||||
// Save sits at the bottom of a long form: the banner is brought into view.
|
||||
await waitFor(() => expect(scroll).toHaveBeenCalled());
|
||||
expect(scroll.mock.contexts[0]).toHaveTextContent(/Visitors still see the published version/);
|
||||
expect(screen.getByLabelText("Title")).toHaveValue("Hotel week");
|
||||
|
||||
await user.click(screen.getByRole("button", { name: "Publish changes" }));
|
||||
|
||||
@@ -70,6 +70,12 @@ export function PageEditor({ pageId, fromId, initialBlocks }: { pageId?: number;
|
||||
const [confirmDiscard, setConfirmDiscard] = useState(false);
|
||||
// A published page's edits are saved as a draft; visitors see the live version until Publish changes.
|
||||
const [hasDraft, setHasDraft] = useState(false);
|
||||
// Bumped when a save leaves a draft: Save sits below a long form, so the banner is scrolled into view.
|
||||
const [revealDraft, setRevealDraft] = useState(0);
|
||||
const draftBanner = useRef<HTMLDivElement>(null);
|
||||
useEffect(() => {
|
||||
if (revealDraft) draftBanner.current?.scrollIntoView?.({ behavior: "smooth", block: "nearest" });
|
||||
}, [revealDraft]);
|
||||
const [events, setEvents] = useState<EventOption[]>([]);
|
||||
const nextKey = useRef(0);
|
||||
const toRows = (blocks: CmsBlock[]) => blocks.map((block) => ({ key: ++nextKey.current, block }));
|
||||
@@ -185,6 +191,7 @@ export function PageEditor({ pageId, fromId, initialBlocks }: { pageId?: number;
|
||||
setDraft((d) => ({ ...d, id: page.id, companyId: page.companyId }));
|
||||
setSaved({ slug: page.slug, status: page.status });
|
||||
setHasDraft(!!page.draft);
|
||||
if (page.draft) setRevealDraft((n) => n + 1);
|
||||
setDirty(false);
|
||||
setNotice("PagesSaved");
|
||||
if (isNew) router.replace(`/pages/${page.id}?saved=1`);
|
||||
@@ -277,7 +284,7 @@ export function PageEditor({ pageId, fromId, initialBlocks }: { pageId?: number;
|
||||
{summary && <p role="alert" className="text-sm text-destructive">{summary}</p>}
|
||||
{notice && <p role="status" className="text-sm text-positive">{t(notice)}</p>}
|
||||
{hasDraft && draft.status === "PUBLISHED" ? (
|
||||
<div className="flex flex-wrap items-center gap-2 rounded-md border border-warning/30 bg-warning/10 px-3 py-2 text-sm">
|
||||
<div ref={draftBanner} className="flex scroll-mt-20 flex-wrap items-center gap-2 rounded-md border border-warning/30 bg-warning/10 px-3 py-2 text-sm">
|
||||
<p className="mr-auto">{t("PagesDraftBanner")}</p>
|
||||
<Button type="button" size="sm" variant="outline" disabled={saving} onClick={() => setConfirmDiscard(true)}>{t("PagesDiscardChanges")}</Button>
|
||||
<Button type="button" size="sm" disabled={saving} onClick={() => void publishChanges()}>{t("PagesPublishChanges")}</Button>
|
||||
|
||||
Reference in new issue
Block a user