Skip to content

Commit ac4fe2a

Browse files
committed
fix: preserve trigger condition builder value from saved draft
Parse saved draft.triggerCondition string into ConditionRule[] and pass as initialRules to TriggerConditionBuilder
1 parent 1436aac commit ac4fe2a

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

frontend/src/app/create/create-page-client.test.tsx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,20 @@ vi.mock("@stellar/freighter-api", () => ({
1818
}));
1919

2020
vi.mock("@/components/trigger-condition-builder", () => ({
21-
TriggerConditionBuilder: ({ onChange }: { onChange: (value: string) => void }) => (
21+
TriggerConditionBuilder: ({
22+
initialRules,
23+
onChange,
24+
}: {
25+
initialRules?: { field: string; operator: string; value: string }[];
26+
onChange: (value: string) => void;
27+
}) => (
2228
<input
2329
aria-label="Trigger mock input"
30+
defaultValue={
31+
initialRules
32+
? initialRules.map((r) => `${r.field} ${r.operator} ${r.value}`).join(" AND ")
33+
: ""
34+
}
2435
onChange={(event) => onChange(event.target.value)}
2536
placeholder="Trigger mock input"
2637
type="text"
@@ -114,4 +125,28 @@ describe("CreatePolicyPageClient", () => {
114125

115126
expect(screen.getByText(/policy created successfully/i)).toBeInTheDocument();
116127
});
128+
129+
it("restores trigger condition builder rules from draft when going back to configure step", () => {
130+
localStorage.setItem(
131+
"stellarinsure-policy-draft",
132+
JSON.stringify({
133+
policyType: "weather",
134+
coverageAmount: "5000",
135+
premium: "120",
136+
triggerCondition: "temperature > 25 AND rainfall > 50",
137+
duration: "90",
138+
oracleProvider: "weatherlink-prime",
139+
}),
140+
);
141+
render(<CreatePolicyPageClient />);
142+
143+
expect(screen.getByRole("heading", { name: /review your policy/i })).toBeInTheDocument();
144+
145+
fireEvent.click(screen.getByRole("button", { name: /back/i }));
146+
147+
expect(screen.getByRole("heading", { name: /configure your policy/i })).toBeInTheDocument();
148+
149+
const triggerInput = screen.getByPlaceholderText("Trigger mock input") as HTMLInputElement;
150+
expect(triggerInput.value).toBe("temperature > 25 AND rainfall > 50");
151+
});
117152
});

frontend/src/app/create/create-page-client.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
import { useWallet } from "@/components/wallet-provider";
2020
import { useAutosave } from "@/hooks/use-autosave";
2121
import { useAppTranslation } from "@/i18n/provider";
22-
import { TriggerConditionBuilder } from "@/components/trigger-condition-builder";
22+
import { TriggerConditionBuilder, type ConditionRule, type Operator } from "@/components/trigger-condition-builder";
2323
import { PremiumEstimate, type PremiumBreakdown } from "@/components/premium-estimate";
2424
import { ValidationSummary, type ValidationError } from "@/components/validation-summary";
2525
import { signTransaction } from "@stellar/freighter-api";
@@ -58,6 +58,22 @@ const INITIAL_DRAFT: PolicyDraft = {
5858

5959

6060

61+
function parseConditionString(condition: string): ConditionRule[] | undefined {
62+
if (!condition || condition.trim() === "") return undefined;
63+
const parts = condition.split(" AND ").map((p) => p.trim()).filter(Boolean);
64+
if (parts.length === 0) return undefined;
65+
const rules: ConditionRule[] = [];
66+
const pattern = /^(\w+)\s*(>=|<=|>|<|=)\s*(.+)$/;
67+
for (const part of parts) {
68+
const match = part.match(pattern);
69+
if (!match) return undefined;
70+
const operator = match[2] as Operator;
71+
if (![">", "<", "=", ">=", "<="].includes(operator)) return undefined;
72+
rules.push({ field: match[1], operator, value: match[3].trim() });
73+
}
74+
return rules.length > 0 ? rules : undefined;
75+
}
76+
6177
const MAX_COVERAGE_AMOUNT = 1_000_000;
6278

6379
const ORACLE_PROVIDER_MAP: Record<PolicyType, OracleProvider[]> = {
@@ -523,6 +539,11 @@ export default function CreatePolicyPageClient() {
523539
oracleState === "ready" &&
524540
draft.oracleProvider.trim() !== "";
525541

542+
const triggerInitialRules = useMemo(
543+
() => parseConditionString(draft.triggerCondition),
544+
[draft.triggerCondition],
545+
);
546+
526547
const isWalletReady = isConnected && walletStatus !== "checking" && walletStatus !== "connecting";
527548

528549
return (
@@ -624,6 +645,7 @@ export default function CreatePolicyPageClient() {
624645
<div className="field field--full" id="trigger-input">
625646
<span className="field__label">{t("createPolicy.configSection.triggerLabel")}</span>
626647
<TriggerConditionBuilder
648+
initialRules={triggerInitialRules}
627649
onChange={(val) => updateDraft("triggerCondition", val)}
628650
/>
629651
<span className="field__hint">

0 commit comments

Comments
 (0)