Skip to content

Commit 3a4e2cb

Browse files
committed
✨ (phone) handle different phone number format according to current lang
1 parent 6314c65 commit 3a4e2cb

3 files changed

Lines changed: 53 additions & 12 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ Bienvenue sur mon CV qui sert vaguement de portfolio. Je mets un peu tout et n'i
77
Code
88

99
- Double check l'accessibilité
10-
- Watermark "Générée via ..." à l'impression
10+
- Tester les utils et regex
11+
- Test de composants ?
12+
- Watermark "Générée via ..." à l'impression ?
1113

1214
## Installation et utilisation
1315

app/components/ContactSection/ContactSection.component.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { useEffect, useMemo, useState, type FC } from 'react'
1+
import { useContext, useEffect, useMemo, useState, type FC } from 'react'
22
import classNames from 'classnames/bind'
33
import styles from './ContactSection.module.scss'
44
import { Section } from '../Section'
55
import { SocialLink } from '../SocialLink'
66
import { LocalStorageKeys } from '~/model/localStorage'
7+
import { formatPhoneNumber } from '~/utils/phone.utils'
8+
import { LangContext } from '~/context/Lang.context'
79

810
const cx = classNames.bind(styles)
911

@@ -19,6 +21,8 @@ const cx = classNames.bind(styles)
1921
*/
2022
export const ContactSection: FC = () => {
2123
const [isClientSide, setisClientSide] = useState(false)
24+
const { langKey } = useContext(LangContext)
25+
2226
useEffect(() => {
2327
setisClientSide(true)
2428
}, [])
@@ -35,14 +39,8 @@ export const ContactSection: FC = () => {
3539
const storedPhone = window.localStorage.getItem(LocalStorageKeys.phone)
3640
if (!storedPhone) return null
3741

38-
const display = [...storedPhone]
39-
.map((c, i) => (i > 0 && i % 2 === 0 ? ` ${c}` : c))
40-
.join('')
41-
42-
const withIndicator = storedPhone.replace(/^0/g, '+33')
43-
44-
return { display, withIndicator }
45-
}, [isClientSide])
42+
return formatPhoneNumber(storedPhone, langKey === 'fr' ? 'fr' : 'intl')
43+
}, [isClientSide, langKey])
4644

4745
return (
4846
<Section title="Contact">
@@ -57,8 +55,8 @@ export const ContactSection: FC = () => {
5755
{phone && (
5856
<SocialLink
5957
icon="phone"
60-
label={phone.display}
61-
link={`tel:${phone.withIndicator}`}
58+
label={phone.formated}
59+
link={`tel:${phone.normalized}`}
6260
/>
6361
)}
6462
<SocialLink

app/utils/phone.utils.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const phoneRegex = /^((?:\+\d{2})|(?:0))(\d{9})$/
2+
const phoneIndicatorFrance = '+33'
3+
4+
/* Todo : Commentaire étoffé, mentionner que le retour est sans indicateur */
5+
export const getIntlPhoneNumber = (input: string) => {
6+
const match = input.replaceAll(' ', '').match(phoneRegex)
7+
8+
console.log(match)
9+
10+
if (!match) {
11+
return null
12+
}
13+
14+
const phoneCore = match.at(2)
15+
16+
return phoneCore
17+
}
18+
19+
export const formatPhoneNumber = (input: string, format: 'fr' | 'intl') => {
20+
const coreNumber = getIntlPhoneNumber(input)
21+
if (!coreNumber) {
22+
return null
23+
}
24+
const normalized = `${phoneIndicatorFrance}${coreNumber}`
25+
const splitedNumber = coreNumber?.split('')
26+
const formatedIntlNumber = splitedNumber
27+
.map((digit, index) => (index % 2 === 0 ? digit : ` ${digit}`))
28+
.join('')
29+
30+
switch (format) {
31+
case 'fr': {
32+
return { normalized, formated: `0${formatedIntlNumber}` }
33+
}
34+
default: {
35+
return {
36+
normalized,
37+
formated: `${phoneIndicatorFrance} ${formatedIntlNumber}`,
38+
}
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)