Skip to content

Commit

Permalink
fix: make sure customer type is included in the payload.
Browse files Browse the repository at this point in the history
Because it previously set it via a reducer and submitted the form in
the same step, the customer type wouldn't be set correctly before the
form was submitted, causing it to show up as "undefined".

We're doing double the work now, but I think that's an acceptable
trade-off for now.
  • Loading branch information
thomasheartman committed Mar 10, 2022
1 parent 027eac4 commit 18c16b3
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions website/src/components/UserFeedback/index.tsx
Expand Up @@ -7,6 +7,12 @@ const join = (...cs: string[]) => cs.join(' ');

type CustomerType = 'open source' | 'paying';

type FormData = {
score: number;
comment: undefined | string;
customerType: undefined | CustomerType;
};

type InitialData = {
currentStep: number;
data: {
Expand Down Expand Up @@ -173,13 +179,13 @@ export const FeedbackWrapper: React.FC<Props> = ({ seedData, open }) => {
const setCustomerType = (customerType: CustomerType) =>
dispatch({ kind: 'set customer type', data: customerType });

const submitFeedback = () => {
const submitFeedback = (data: FormData) => {
if (feedbackTargetUrl) {
fetch(feedbackTargetUrl, {
method: 'post',
body: JSON.stringify({
data: {
...state.data,
...data,
openedManually: manuallyOpened,
currentPage: location.pathname,
},
Expand Down Expand Up @@ -380,7 +386,16 @@ export const FeedbackWrapper: React.FC<Props> = ({ seedData, open }) => {
onSubmit={(e) => {
e.preventDefault();
setCustomerType(value);
submitFeedback();

// To ensure that we get the correct customer type included.
// We can't rely on the reducer to set it because it won't
// happen until the component re-renders, causing customer
// type to have an old or empty value.
const finalState = stateReducer(state, {
kind: 'set customer type',
data: value,
});
submitFeedback(finalState.data);
}}
>
<fieldset disabled={hidden}>
Expand Down

0 comments on commit 18c16b3

Please sign in to comment.