Load the finished Rule itself on Edit, and ask before replacing a draft in progress.
This commit is contained in:
1 parent
942c7cc9de
commit
d576ed81d9
4 files changed
+171
-7
No files matched your search
@@ -64,6 +64,11 @@ import {
|
|||||||
} from "../../../lib/create/publishedDocumentToCreateFlowState";
|
} from "../../../lib/create/publishedDocumentToCreateFlowState";
|
||||||
import { METHOD_FACET_API_SECTION_IDS } from "../../../lib/create/customRuleFacets";
|
import { METHOD_FACET_API_SECTION_IDS } from "../../../lib/create/customRuleFacets";
|
||||||
import { readLastPublishedRule } from "../../../lib/create/lastPublishedRule";
|
import { readLastPublishedRule } from "../../../lib/create/lastPublishedRule";
|
||||||
|
import {
|
||||||
|
createFlowStateForEditingPublishedRule,
|
||||||
|
publishedRuleEditWouldReplaceDraft,
|
||||||
|
} from "../../../lib/create/beginEditingPublishedRule";
|
||||||
|
import { useAsyncConfirm } from "../../hooks/useAsyncConfirm";
|
||||||
import {
|
import {
|
||||||
CREATE_FLOW_COPIED_QUERY,
|
CREATE_FLOW_COPIED_QUERY,
|
||||||
CREATE_FLOW_COPIED_VALUE,
|
CREATE_FLOW_COPIED_VALUE,
|
||||||
@@ -225,6 +230,7 @@ function CreateFlowLayoutContent({
|
|||||||
} | null>(null);
|
} | null>(null);
|
||||||
const [shareModalOpen, setShareModalOpen] = useState(false);
|
const [shareModalOpen, setShareModalOpen] = useState(false);
|
||||||
const [leaveConfirmOpen, setLeaveConfirmOpen] = useState(false);
|
const [leaveConfirmOpen, setLeaveConfirmOpen] = useState(false);
|
||||||
|
const { requestConfirm, confirmDialog } = useAsyncConfirm();
|
||||||
const [completedHasPublishedRule, setCompletedHasPublishedRule] =
|
const [completedHasPublishedRule, setCompletedHasPublishedRule] =
|
||||||
useState(false);
|
useState(false);
|
||||||
const [completedCopiedReady, setCompletedCopiedReady] = useState(false);
|
const [completedCopiedReady, setCompletedCopiedReady] = useState(false);
|
||||||
@@ -889,6 +895,7 @@ function CreateFlowLayoutContent({
|
|||||||
backdropVariant="blurredYellow"
|
backdropVariant="blurredYellow"
|
||||||
ariaLabel={messages.create.topNav.leaveConfirmTitle}
|
ariaLabel={messages.create.topNav.leaveConfirmTitle}
|
||||||
/>
|
/>
|
||||||
|
{confirmDialog}
|
||||||
<TemplateCustomizeDialog
|
<TemplateCustomizeDialog
|
||||||
isOpen={customizeDialog !== null}
|
isOpen={customizeDialog !== null}
|
||||||
templateTitle={customizeDialog?.templateTitle ?? ""}
|
templateTitle={customizeDialog?.templateTitle ?? ""}
|
||||||
@@ -926,13 +933,24 @@ function CreateFlowLayoutContent({
|
|||||||
onEdit={
|
onEdit={
|
||||||
isCompletedStep
|
isCompletedStep
|
||||||
? () => {
|
? () => {
|
||||||
const last = readLastPublishedRule();
|
void (async () => {
|
||||||
if (!last) return;
|
const last = readLastPublishedRule();
|
||||||
updateState({
|
if (!last) return;
|
||||||
editingPublishedRuleId: last.id,
|
if (publishedRuleEditWouldReplaceDraft(state, last)) {
|
||||||
sections: [],
|
const agreed = await requestConfirm({
|
||||||
});
|
title: messages.create.topNav.editReplaceDraftTitle,
|
||||||
router.push(createFlowStepPath("final-review"));
|
description:
|
||||||
|
messages.create.topNav.editReplaceDraftDescription,
|
||||||
|
proceedText:
|
||||||
|
messages.create.topNav.editReplaceDraftProceed,
|
||||||
|
cancelText:
|
||||||
|
messages.create.topNav.editReplaceDraftCancel,
|
||||||
|
});
|
||||||
|
if (!agreed) return;
|
||||||
|
}
|
||||||
|
replaceState(createFlowStateForEditingPublishedRule(last));
|
||||||
|
router.push(createFlowStepPath("final-review"));
|
||||||
|
})();
|
||||||
}
|
}
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
import type { CreateFlowState } from "../../app/(app)/create/types";
|
||||||
|
import { createFlowStateHasKeys } from "./draftHydrationUtils";
|
||||||
|
import { PUBLISHED_CUSTOM_RULE_SELECTION_KEYS } from "./customRuleFacets";
|
||||||
|
import type { StoredLastPublishedRule } from "./lastPublishedRule";
|
||||||
|
import {
|
||||||
|
createFlowStateFromPublishedRule,
|
||||||
|
methodSectionsPinsFromPublishedHydratePatch,
|
||||||
|
} from "./publishedDocumentToCreateFlowState";
|
||||||
|
|
||||||
|
function sortedIds(value: unknown): string {
|
||||||
|
if (!Array.isArray(value)) return "";
|
||||||
|
return value
|
||||||
|
.filter((id): id is string => typeof id === "string")
|
||||||
|
.map((id) => id.trim())
|
||||||
|
.filter((id) => id.length > 0)
|
||||||
|
.sort()
|
||||||
|
.join(",");
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createFlowStateMatchesPublishedRule(
|
||||||
|
state: CreateFlowState,
|
||||||
|
last: StoredLastPublishedRule,
|
||||||
|
): boolean {
|
||||||
|
const patch = createFlowStateFromPublishedRule(last);
|
||||||
|
const title =
|
||||||
|
typeof state.title === "string" ? state.title.trim() : "";
|
||||||
|
const patchTitle =
|
||||||
|
typeof patch.title === "string" ? patch.title.trim() : "";
|
||||||
|
if (title !== patchTitle) return false;
|
||||||
|
for (const key of PUBLISHED_CUSTOM_RULE_SELECTION_KEYS) {
|
||||||
|
if (sortedIds(state[key]) !== sortedIds(patch[key])) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* True when Edit would drop a different in-progress draft to load this
|
||||||
|
* finished Rule. Empty state and a draft that already is this Rule do not
|
||||||
|
* need a confirm.
|
||||||
|
*/
|
||||||
|
export function publishedRuleEditWouldReplaceDraft(
|
||||||
|
state: CreateFlowState,
|
||||||
|
last: StoredLastPublishedRule,
|
||||||
|
): boolean {
|
||||||
|
if (!createFlowStateHasKeys(state)) return false;
|
||||||
|
const editingId =
|
||||||
|
typeof state.editingPublishedRuleId === "string"
|
||||||
|
? state.editingPublishedRuleId.trim()
|
||||||
|
: "";
|
||||||
|
if (editingId.length > 0 && editingId !== last.id) return true;
|
||||||
|
return !createFlowStateMatchesPublishedRule(state, last);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Full wizard state for editing a finished Rule — not a merge onto another draft. */
|
||||||
|
export function createFlowStateForEditingPublishedRule(
|
||||||
|
last: StoredLastPublishedRule,
|
||||||
|
): CreateFlowState {
|
||||||
|
const patch = createFlowStateFromPublishedRule(last);
|
||||||
|
return {
|
||||||
|
...patch,
|
||||||
|
methodSectionsPinCommitted:
|
||||||
|
methodSectionsPinsFromPublishedHydratePatch(patch),
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -19,6 +19,10 @@
|
|||||||
"leaveConfirmProceed": "Leave anyway",
|
"leaveConfirmProceed": "Leave anyway",
|
||||||
"leaveConfirmSaveProgress": "Save my progress",
|
"leaveConfirmSaveProgress": "Save my progress",
|
||||||
"leaveConfirmCancel": "Stay",
|
"leaveConfirmCancel": "Stay",
|
||||||
|
"editReplaceDraftTitle": "Replace the draft in progress?",
|
||||||
|
"editReplaceDraftDescription": "Edit opens this finished Rule. The draft you have been working on will be replaced in this browser.",
|
||||||
|
"editReplaceDraftProceed": "Edit this Rule",
|
||||||
|
"editReplaceDraftCancel": "Keep my draft",
|
||||||
"draftSaveBannerTitle": "Couldn't save draft",
|
"draftSaveBannerTitle": "Couldn't save draft",
|
||||||
"postLoginSaveFailedWithReason": "Could not save your draft to your account. Your progress is still stored on this device.\n\n{reason}",
|
"postLoginSaveFailedWithReason": "Could not save your draft to your account. Your progress is still stored on this device.\n\n{reason}",
|
||||||
"offlineTitle": "You're offline",
|
"offlineTitle": "You're offline",
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import {
|
||||||
|
createFlowStateForEditingPublishedRule,
|
||||||
|
createFlowStateMatchesPublishedRule,
|
||||||
|
publishedRuleEditWouldReplaceDraft,
|
||||||
|
} from "../../lib/create/beginEditingPublishedRule";
|
||||||
|
import type { StoredLastPublishedRule } from "../../lib/create/lastPublishedRule";
|
||||||
|
import type { CreateFlowState } from "../../app/(app)/create/types";
|
||||||
|
|
||||||
|
const published: StoredLastPublishedRule = {
|
||||||
|
id: "rule-qa",
|
||||||
|
title: "QA Tenants Union",
|
||||||
|
summary: "How we share the building.",
|
||||||
|
document: {
|
||||||
|
coreValues: [
|
||||||
|
{
|
||||||
|
chipId: "accountability",
|
||||||
|
label: "Accountability",
|
||||||
|
meaning: "We say what we will do.",
|
||||||
|
signals: "",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
methodSelections: {
|
||||||
|
communication: [
|
||||||
|
{
|
||||||
|
id: "signal",
|
||||||
|
label: "Signal",
|
||||||
|
sections: {
|
||||||
|
corePrinciple: "We write it down.",
|
||||||
|
logisticsAdmin: "A shared thread.",
|
||||||
|
codeOfConduct: "No pile-ons.",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
describe("publishedRuleEditWouldReplaceDraft", () => {
|
||||||
|
it("is false when there is no draft", () => {
|
||||||
|
expect(publishedRuleEditWouldReplaceDraft({}, published)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("is false when the draft is already this finished Rule", () => {
|
||||||
|
const state = createFlowStateForEditingPublishedRule(published);
|
||||||
|
expect(publishedRuleEditWouldReplaceDraft(state, published)).toBe(false);
|
||||||
|
expect(createFlowStateMatchesPublishedRule(state, published)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("is true when the draft is a different community", () => {
|
||||||
|
const state: CreateFlowState = {
|
||||||
|
title: "The CommunityRule Project",
|
||||||
|
selectedCoreValueIds: ["solidarity"],
|
||||||
|
selectedCommunicationMethodIds: ["loomio"],
|
||||||
|
};
|
||||||
|
expect(publishedRuleEditWouldReplaceDraft(state, published)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("is true when the draft is already marked as an edit of another Rule", () => {
|
||||||
|
const state: CreateFlowState = {
|
||||||
|
...createFlowStateForEditingPublishedRule(published),
|
||||||
|
editingPublishedRuleId: "some-other-rule",
|
||||||
|
};
|
||||||
|
expect(publishedRuleEditWouldReplaceDraft(state, published)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("createFlowStateForEditingPublishedRule", () => {
|
||||||
|
it("loads the finished Rule's own name and cards, not leftover draft fields", () => {
|
||||||
|
const state = createFlowStateForEditingPublishedRule(published);
|
||||||
|
expect(state.title).toBe("QA Tenants Union");
|
||||||
|
expect(state.editingPublishedRuleId).toBe("rule-qa");
|
||||||
|
expect(state.selectedCommunicationMethodIds).toEqual(["signal"]);
|
||||||
|
expect(state.selectedOrganizationTypeIds).toBeUndefined();
|
||||||
|
expect(state.communityAvatarUrl).toBeUndefined();
|
||||||
|
expect(state.sections).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user