Skip to content

Commit 091de19

Browse files
committed
Refactored code a bit and moved getCustomNumberValidation to function.js
1 parent a06aed3 commit 091de19

File tree

2 files changed

+57
-45
lines changed

2 files changed

+57
-45
lines changed

components/Checkout/Billing.component.jsx

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { useForm } from 'react-hook-form';
22
import { InputField } from '../Input/InputField.component';
3+
import { getCustomNumberValidation } from '../../functions/functions';
34

4-
const getCustomNumberValidation = (
5-
value,
6-
{ minLength, maxLength, pattern }
7-
) => ({
8-
minLength: { value, message: minLength },
9-
maxLength: { value, message: maxLength },
10-
pattern: { value: /^\d+$/i, message: pattern },
11-
});
5+
const customPostNumberValidation = getCustomNumberValidation(
6+
{
7+
minLength: 'Postnummer må være minimum 4 tall',
8+
maxLength: 'Postnummer må være maksimalt 4 tall',
9+
pattern: 'Postnummer må bare være tall',
10+
},
11+
4
12+
);
1213

1314
const inputs = [
1415
{ label: 'Fornavn', name: 'firstName' },
@@ -17,31 +18,34 @@ const inputs = [
1718
{
1819
label: 'Postnummer',
1920
name: 'postcode',
20-
customValidation: getCustomNumberValidation(4, {
21-
minLength: 'Postnummer må være minimum 4 tall',
22-
maxLength: 'Postnummer må være maksimalt 4 tall',
23-
pattern: 'Postnummer må bare være tall',
24-
}),
21+
customValidation: customPostNumberValidation,
2522
},
2623
{ label: 'Sted', name: 'city' },
2724
{
2825
label: 'Epost',
2926
name: 'email',
30-
customValidation: {
31-
pattern: {
32-
value: /[^@]+@[^@]+\.[^@]+/i,
33-
message: 'Du må oppgi en gyldig epost',
34-
},
35-
},
27+
customValidation: getCustomNumberValidation(
28+
{ pattern: 'Du må oppgi en gyldig epost' },
29+
undefined,
30+
/[^@]+@[^@]+\.[^@]+/i
31+
),
3632
},
3733
{
3834
label: 'Telefon',
3935
name: 'phone',
40-
customValidation: getCustomNumberValidation(8, {
41-
minLength: 'Minimum 8 tall i telefonnummeret',
42-
maxLength: 'Maksimalt 8 tall i telefonnummeret',
43-
pattern: 'Ikke gyldig telefonnummer',
44-
}),
36+
customValidation: getCustomNumberValidation(
37+
{
38+
minLength: 'Minimum 8 tall i telefonnummeret',
39+
maxLength: 'Maksimalt 8 tall i telefonnummeret',
40+
pattern: 'Ikke gyldig telefonnummer',
41+
},
42+
8
43+
),
44+
},
45+
{
46+
label: 'Postnummer',
47+
name: 'postcode',
48+
customValidation: customPostNumberValidation,
4549
},
4650
];
4751

@@ -66,26 +70,6 @@ const Billing = ({ onSubmit }) => {
6670
register={register}
6771
/>
6872
))}
69-
<InputField
70-
label="Postnummer"
71-
name="postcode"
72-
errors={errors}
73-
register={register}
74-
customValidation={{
75-
minLength: {
76-
value: 4,
77-
message: 'Postnummer må være minimum 4 tall',
78-
},
79-
maxLength: {
80-
value: 4,
81-
message: 'Postnummer må være maksimalt 4 tall',
82-
},
83-
pattern: {
84-
value: /^\d+$/i,
85-
message: 'Postnummer må bare være tall',
86-
},
87-
}}
88-
/>
8973
<OrderButton register={register} />
9074
</div>
9175
</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 than 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)