Skip to content

Commit 00ccee4

Browse files
committed
fix(browser-sdk): reject unsuccessful feedback form submissions
1 parent 1adfced commit 00ccee4

4 files changed

Lines changed: 78 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@reflag/browser-sdk": patch
3+
---
4+
5+
Treat unsuccessful feedback form submissions as errors instead of showing success and invoking `onAfterSubmit` with unsaved feedback. Applies to manual and automated forms, and allows retrying Send.

‎packages/browser-sdk/src/client.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,11 +1199,13 @@ export class ReflagClient {
11991199
return { feedbackId: undefined };
12001200
},
12011201
onSubmit: async (data) => {
1202-
// Default onSubmit handler
1203-
await this.feedback({
1202+
const res = await this.feedback({
12041203
...feedbackData,
12051204
...data,
12061205
});
1206+
if (!res?.ok) {
1207+
throw new Error("Couldn't submit feedback. Please try again.");
1208+
}
12071209

12081210
options.onAfterSubmit?.(data);
12091211
},

‎packages/browser-sdk/src/feedback/feedback.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,10 @@ export class AutoFeedback {
435435
return { feedbackId: res.feedbackId };
436436
},
437437
onSubmit: async (data) => {
438-
await replyCallback(data);
438+
const res = await replyCallback(data);
439+
if (!res?.feedbackId) {
440+
throw new Error("Couldn't submit feedback. Please try again.");
441+
}
439442
options.onAfterSubmit?.(data);
440443
},
441444
onDismiss: () => replyCallback(null),
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { afterEach, expect, test, vi } from "vitest";
2+
3+
import { ReflagClient } from "../src/client";
4+
import { AutoFeedback } from "../src/feedback/feedback";
5+
import * as feedbackUi from "../src/feedback/ui";
6+
import { HttpClient } from "../src/httpClient";
7+
import { testLogger } from "./testLogger";
8+
9+
const data = { question: "How was it?", score: 4, comment: "Useful!" };
10+
11+
afterEach(() => vi.restoreAllMocks());
12+
13+
test.each(["manual", "automated"] as const)(
14+
"%s feedback rejects a failed Send without notifying, then allows retry",
15+
async (source) => {
16+
const open = vi
17+
.spyOn(feedbackUi, "openFeedbackForm")
18+
.mockImplementation(() => undefined);
19+
const onAfterSubmit = vi.fn();
20+
let status = 200;
21+
vi.spyOn(HttpClient.prototype, "post").mockImplementation(async () =>
22+
Response.json(
23+
status === 200 ? { feedbackId: "123" } : { message: "Unavailable" },
24+
{ status },
25+
),
26+
);
27+
28+
if (source === "manual") {
29+
const client = new ReflagClient({
30+
publishableKey: "test-key",
31+
user: { id: "user" },
32+
logger: testLogger,
33+
});
34+
client.requestFeedback({ flagKey: "flag", onAfterSubmit });
35+
await vi.waitFor(() => expect(open).toHaveBeenCalledOnce());
36+
} else {
37+
const auto = new AutoFeedback(
38+
"https://front.reflag.com",
39+
testLogger,
40+
new HttpClient("test-key"),
41+
(_prompt, handlers) => handlers.openFeedbackForm({ onAfterSubmit }),
42+
"user",
43+
);
44+
await auto.triggerFeedbackPrompt(
45+
"user",
46+
{
47+
question: data.question,
48+
promptId: "prompt",
49+
featureId: "feature",
50+
showAfter: new Date(),
51+
showBefore: new Date(Date.now() + 60000),
52+
},
53+
vi.fn(),
54+
);
55+
}
56+
57+
const { onSubmit } = open.mock.calls[0]![0];
58+
status = 500;
59+
await expect(onSubmit(data)).rejects.toThrow("Couldn't submit feedback");
60+
expect(onAfterSubmit).not.toHaveBeenCalled();
61+
62+
status = 200;
63+
await expect(onSubmit(data)).resolves.toBeUndefined();
64+
},
65+
);

0 commit comments

Comments
 (0)