Skip to content

Commit

Permalink
Rename invalidFields to validatedFields
Browse files Browse the repository at this point in the history
  • Loading branch information
dgcohen committed Jan 2, 2025
1 parent cc28efe commit fdb4c81
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
7 changes: 5 additions & 2 deletions pages/api/hold/request/[id]/edd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
fetchPatronEligibility,
} from "../../../../../src/server/api/hold"
import {
initialEDDInvalidFields,
defaultValidatedEDDFields,
validateEDDForm,
eddFormIsInvalid,
} from "../../../../../src/utils/holdPageUtils"
Expand All @@ -28,7 +28,10 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
const holdId = req.query.id as string

// Server-side form validation
const validatedFields = validateEDDForm(formState, initialEDDInvalidFields)
const validatedFields = validateEDDForm(
formState,
defaultValidatedEDDFields
)

const [, itemId] = holdId.split("-")

Expand Down
4 changes: 2 additions & 2 deletions src/components/HoldPages/EDDRequestForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
validateEDDForm,
isInvalidField,
holdButtonDisabledStatuses,
initialEDDInvalidFields,
defaultValidatedEDDFields,
getFirstInvalidEDDField,
} from "../../utils/holdPageUtils"
import type {
Expand Down Expand Up @@ -61,7 +61,7 @@ const EDDRequestForm = ({

// Set the invalid fields as an array in state to keep track of the first invalid field for focus on submit
const [invalidFields, setInvalidFields] = useState<EDDFormValidatedField[]>(
serverValidatedFields || initialEDDInvalidFields
serverValidatedFields || defaultValidatedEDDFields
)

// Create refs for fields that require validation to focus on the first invalid field on submit
Expand Down
2 changes: 1 addition & 1 deletion src/utils/holdPageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const initialEDDFormState: EDDRequestParams = {
}

// Initial state for invalid fields in the EDD form to keep track of the first invalid field for focus on submit
export const initialEDDInvalidFields: EDDFormValidatedField[] = [
export const defaultValidatedEDDFields: EDDFormValidatedField[] = [
{ key: "emailAddress", isInvalid: false },
{ key: "startPage", isInvalid: false },
{ key: "endPage", isInvalid: false },
Expand Down
18 changes: 11 additions & 7 deletions src/utils/utilsTests/holdPageUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import {
initialEDDInvalidFields,
defaultValidatedEDDFields,
getUpdatedInvalidFields,
} from "../holdPageUtils"

describe("holdPageUtils", () => {
describe("getUpdatedInvalidFields", () => {
it("returns correctly updated field validation statuses for different inputs", () => {
expect(
getUpdatedInvalidFields("emailAddress", "test", initialEDDInvalidFields)
getUpdatedInvalidFields(
"emailAddress",
"test",
defaultValidatedEDDFields
)
).toStrictEqual([
{ isInvalid: true, key: "emailAddress" },
{ isInvalid: false, key: "startPage" },
Expand All @@ -18,7 +22,7 @@ describe("holdPageUtils", () => {
getUpdatedInvalidFields(
"emailAddress",
"[email protected]",
initialEDDInvalidFields
defaultValidatedEDDFields
)
).toStrictEqual([
{ isInvalid: false, key: "emailAddress" },
Expand All @@ -27,31 +31,31 @@ describe("holdPageUtils", () => {
{ isInvalid: false, key: "chapterTitle" },
])
expect(
getUpdatedInvalidFields("emailAddress", "", initialEDDInvalidFields)
getUpdatedInvalidFields("emailAddress", "", defaultValidatedEDDFields)
).toStrictEqual([
{ isInvalid: true, key: "emailAddress" },
{ isInvalid: false, key: "startPage" },
{ isInvalid: false, key: "endPage" },
{ isInvalid: false, key: "chapterTitle" },
])
expect(
getUpdatedInvalidFields("startPage", "", initialEDDInvalidFields)
getUpdatedInvalidFields("startPage", "", defaultValidatedEDDFields)
).toStrictEqual([
{ isInvalid: false, key: "emailAddress" },
{ isInvalid: true, key: "startPage" },
{ isInvalid: false, key: "endPage" },
{ isInvalid: false, key: "chapterTitle" },
])
expect(
getUpdatedInvalidFields("endPage", "", initialEDDInvalidFields)
getUpdatedInvalidFields("endPage", "", defaultValidatedEDDFields)
).toStrictEqual([
{ isInvalid: false, key: "emailAddress" },
{ isInvalid: false, key: "startPage" },
{ isInvalid: true, key: "endPage" },
{ isInvalid: false, key: "chapterTitle" },
])
expect(
getUpdatedInvalidFields("chapterTitle", "", initialEDDInvalidFields)
getUpdatedInvalidFields("chapterTitle", "", defaultValidatedEDDFields)
).toStrictEqual([
{ isInvalid: false, key: "emailAddress" },
{ isInvalid: false, key: "startPage" },
Expand Down

0 comments on commit fdb4c81

Please sign in to comment.