Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(site launch form): trim site launch form input #698

Merged
merged 4 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/routes/formsgSiteLaunch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,17 @@ export class FormsgSiteLaunchRouter {

({
submissionId,
requesterEmail: getField(responses, REQUESTER_EMAIL_FIELD)?.trim(),
repoName: element[2].trim(),
primaryDomain: this.getPrimaryDomain(element[0]).trim(),
requesterEmail: getField(responses, REQUESTER_EMAIL_FIELD),
repoName: element[2],
primaryDomain: this.getPrimaryDomain(element[0]),
redirectionDomain:
element[1] === "WWW"
? `www.${this.getPrimaryDomain(element[0]).trim()}`
? `www.${this.getPrimaryDomain(element[0])}`
: "",
// if agency email not needed, use email from requester instead
agencyEmail: element[3]
? element[3].trim()
: getField(responses, REQUESTER_EMAIL_FIELD)?.trim(),
? element[3]
: getField(responses, REQUESTER_EMAIL_FIELD),
})
) || []

Expand Down
24 changes: 23 additions & 1 deletion src/utils/formsg-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,35 @@ export function getField(
): string | undefined {
const response = responses.find(({ question }) => question === name)

return response?.answer
return response?.answer?.trim()
}

function trimAllStrings(
responseArray: string[] | string[][]
): string[] | string[][] {
if (Array.isArray(responseArray)) {
responseArray.map((item) => {
if (Array.isArray(item)) {
return trimAllStrings(item)
}
if (typeof item === "string") {
return item.trim()
}
return item
})
return responseArray
}
return responseArray
Copy link
Contributor

@alexanderleegs alexanderleegs Apr 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on reducing the checks here, given that responseArray is string[] | string[][]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TS complains if we dont have this because it is not able to infer that
if (Array.isArray(responseArray)), then responseArray is of type []. I have added in a comment for clarity here:
08433bb

}

export function getFieldsFromTable(
responses: FormField[],
name: string
): string[] | string[][] | undefined {
const response = responses.find(({ question }) => question === name)
let answers = response?.answerArray
if (answers) {
answers = trimAllStrings(answers)
}
return response?.answerArray
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking - does this modify response? I think it might be better to rewrite it so it's clearly regardless

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, this is a bug, fixed here 08433bb

}