Skip to content

Commit c65f85e

Browse files
committed
Merge branch 'master' into develop
2 parents aefb5f2 + 41fdc76 commit c65f85e

File tree

2 files changed

+80
-77
lines changed

2 files changed

+80
-77
lines changed

components/Checkout/Billing.component.jsx

Lines changed: 51 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
11
import { useForm } from 'react-hook-form';
22
import { InputField } from '../Input/InputField.component';
3+
import { getCustomNumberValidation } from '../../utils/functions/functions';
4+
5+
const inputField = [
6+
{ label: 'Fornavn', name: 'firstName' },
7+
{ label: 'Etternavn', name: 'lastName' },
8+
{ label: 'Adresse', name: 'address1' },
9+
{
10+
label: 'Postnummer',
11+
name: 'postcode',
12+
customValidation: getCustomNumberValidation(
13+
{
14+
minLength: 'Postnummer må være minimum 4 tall',
15+
maxLength: 'Postnummer må være maksimalt 4 tall',
16+
pattern: 'Postnummer må bare være tall',
17+
},
18+
4
19+
),
20+
},
21+
{ label: 'Sted', name: 'city' },
22+
{
23+
label: 'Epost',
24+
name: 'email',
25+
customValidation: getCustomNumberValidation(
26+
{ pattern: 'Du må oppgi en gyldig epost' },
27+
undefined,
28+
/^[a-z0-9_!#$%&'*+\/=?`{|}~^.-]+@[a-z0-9.-]+$/gim
29+
),
30+
},
31+
{
32+
label: 'Telefon',
33+
name: 'phone',
34+
customValidation: getCustomNumberValidation(
35+
{
36+
minLength: 'Minimum 8 tall i telefonnummeret',
37+
maxLength: 'Maksimalt 8 tall i telefonnummeret',
38+
pattern: 'Ikke gyldig telefonnummer',
39+
},
40+
8
41+
),
42+
},
43+
];
344

445
const Billing = ({ onSubmit }) => {
546
const {
@@ -12,82 +53,16 @@ const Billing = ({ onSubmit }) => {
1253
<section className="text-gray-700 container p-4 py-2 mx-auto">
1354
<form onSubmit={handleSubmit(onSubmit)}>
1455
<div className="mx-auto lg:w-1/2 flex flex-wrap">
15-
<InputField
16-
label="Fornavn"
17-
name="firstName"
18-
errors={errors}
19-
register={register}
20-
/>
21-
<InputField
22-
label="Etternavn"
23-
name="lastName"
24-
errors={errors}
25-
register={register}
26-
/>
27-
<InputField
28-
label="Adresse"
29-
name="address1"
30-
errors={errors}
31-
register={register}
32-
/>
33-
<InputField
34-
label="Postnummer"
35-
name="postcode"
36-
errors={errors}
37-
register={register}
38-
customValidation={{
39-
minLength: {
40-
value: 4,
41-
message: 'Postnummer må være minimum 4 tall',
42-
},
43-
maxLength: {
44-
value: 4,
45-
message: 'Postnummer må være maksimalt 4 tall',
46-
},
47-
pattern: {
48-
value: /^\d+$/i,
49-
message: 'Postnummer må bare være tall',
50-
},
51-
}}
52-
/>
53-
<InputField
54-
label="Sted"
55-
name="city"
56-
errors={errors}
57-
register={register}
58-
/>
59-
<InputField
60-
label="Epost"
61-
name="email"
62-
errors={errors}
63-
register={register}
64-
customValidation={{
65-
pattern: {
66-
value: /[^@]+@[^@]+\.[^@]+/i,
67-
message: 'Du må oppgi en gyldig epost',
68-
},
69-
}}
70-
/>
71-
<InputField
72-
label="Telefon"
73-
name="phone"
74-
errors={errors}
75-
register={register}
76-
customValidation={{
77-
minLength: {
78-
value: 8,
79-
message: 'Minimum 8 tall i telefonnummeret',
80-
},
81-
maxLength: {
82-
value: 8,
83-
message: 'Maksimalt 8 tall i telefonnummeret',
84-
},
85-
pattern: {
86-
value: /^\d+$/i,
87-
message: 'Ikke gyldig telefonnummer',
88-
},
89-
}}
90-
/>
56+
{inputField.map(({ label, name, customValidation }, key) => (
57+
<InputField
58+
key={key}
59+
label={label}
60+
name={name}
61+
customValidation={customValidation}
62+
errors={errors}
63+
register={register}
64+
/>
65+
))}
9166
<OrderButton register={register} />
9267
</div>
9368
</form>

utils/functions/functions.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { v4 as uuidv4 } from 'uuid';
55
* @param {String} price The price string that we input
66
* @param {String} symbol Currency symbol to add empty character/padding after
77
*/
8-
98
export const paddedPrice = (price, symbol) =>
109
price.split(symbol).join(`${symbol} `);
1110

@@ -22,6 +21,35 @@ export const trimmedStringToLength = (string, length) => {
2221
return string;
2322
};
2423

24+
/**
25+
* Creates a validation object with passed custom error messages.
26+
* If `value` is not passed then returned object will contain only pattern message.
27+
* @param {Object} messages Custom error messages
28+
* @param {String} messages.minLength Message for min length attribute validation
29+
* @param {String} messages.maxLength Message for max length attribute vlidation
30+
* @param {String} messages.pattern Message for custom pattern vlidation
31+
* @param {Integer=} value The number value used as limit for min/max attribute
32+
* @param {RegExp} patternValue Regular expression pattern for validation
33+
*/
34+
export const getCustomNumberValidation = (
35+
{ minLength, maxLength, pattern },
36+
value,
37+
patternValue = /^\d+$/i
38+
) => {
39+
const validationObj = {
40+
minLength: { value, message: minLength },
41+
maxLength: { value, message: maxLength },
42+
pattern: { value: patternValue, message: pattern },
43+
};
44+
45+
return Object.keys(validationObj).reduce((acc, key) => {
46+
if (validationObj[key].value) {
47+
acc[key] = validationObj[key];
48+
}
49+
return acc;
50+
}, {});
51+
};
52+
2553
/**
2654
* Filter variant price. Changes "kr198.00 - kr299.00" to kr299.00 or kr198 depending on the side variable
2755
* @param {String} side Which side of the string to return (which side of the "-" symbol)

0 commit comments

Comments
 (0)