Skip to content

Commit

Permalink
fix(UserDetailEditor): unite first and last name field
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianAndersen committed Jul 5, 2024
1 parent 13d9255 commit 07e8efd
Showing 1 changed file with 13 additions and 59 deletions.
72 changes: 13 additions & 59 deletions src/components/user/UserDetailEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ import { assertBlApiError } from "@/utils/types";
type UserEditorFields = {
email: string;
password: string;
firstName: string;
lastName: string;
name: string;
phoneNumber: string;
address: string;
postalCode: string;
Expand All @@ -57,36 +56,6 @@ type UserEditorFields = {
agreeToTermsAndConditions: boolean;
};

export const extractFirstName = (fullName: string): string => {
if (!fullName || fullName.length <= 0) {
return "";
}

const fullNameSplit = fullName.split(" ");

if (fullNameSplit.length === 1) {
return fullNameSplit[0] as string;
}

return fullName
.split(" ")
.slice(0, fullNameSplit.length - 1)
.join(" ");
};

export const extractLastName = (fullName: string): string => {
if (!fullName || fullName.length <= 0) {
return "";
}

const fullNameSplit = fullName.split(" ");

if (fullNameSplit.length > 1) {
return fullNameSplit.slice(-1).join(" ");
}
return "";
};

const isUnder18 = (birthday: moment.Moment | null): boolean => {
return birthday !== null && moment().diff(birthday, "years") < 18;
};
Expand All @@ -111,8 +80,7 @@ const UserDetailEditor = ({

const defaultValues = {
email: userDetails.email,
firstName: extractFirstName(userDetails.name),
lastName: extractLastName(userDetails.name),
name: userDetails.name,
phoneNumber: userDetails.phone,
address: userDetails.address,
postalCode: userDetails.postCode,
Expand Down Expand Up @@ -163,7 +131,7 @@ const UserDetailEditor = ({
}
try {
await updateUserDetails(getAccessTokenBody().details, {
name: `${data.firstName} ${data.lastName}`,
name: data.name,
email: data.email,
phone: data.phoneNumber,
address: data.address,
Expand Down Expand Up @@ -364,31 +332,17 @@ const UserDetailEditor = ({
<Typography variant="body1">Din informasjon</Typography>
<Divider />
</Grid>
<Grid item xs={12} sm={6}>
<TextField
data-testid="first-name-field"
required
autoComplete="given-name"
fullWidth
id="firstName"
label="Fornavn"
error={!!errors.firstName}
{...register("firstName", {
required: "Du må fylle inn fornavn",
})}
/>
</Grid>
<Grid item xs={12} sm={6}>
<Grid item xs={12}>
<TextField
data-testid="last-name-field"
data-testid="name-field"
required
autoComplete="name"
fullWidth
id="lastName"
label="Etternavn"
autoComplete="family-name"
error={!!errors.lastName}
{...register("lastName", {
required: "Du må fylle inn etternavn",
id="name"
label="Navn"
error={!!errors.name}
{...register("name", {
required: "Du må fylle inn navn",
})}
/>
</Grid>
Expand Down Expand Up @@ -466,7 +420,7 @@ const UserDetailEditor = ({
},
]
>(
`${BL_CONFIG.collection.delivery}/${BL_CONFIG.delivery.postalCodeLookup}`,
`${BL_CONFIG.collection.delivery}/${BL_CONFIG.delivery.postalCodeLookup.operation}`,
{ postalCode: event.target.value },
);
setWaitingForPostalCity(false);
Expand All @@ -493,7 +447,7 @@ const UserDetailEditor = ({
},
]
>(
`${BL_CONFIG.collection.delivery}/${BL_CONFIG.delivery.postalCodeLookup}`,
`${BL_CONFIG.collection.delivery}/${BL_CONFIG.delivery.postalCodeLookup.operation}`,
{ postalCode: v },
);

Expand Down

0 comments on commit 07e8efd

Please sign in to comment.