Skip to content

Commit

Permalink
feat(utils): update isValidPhone to don't be equal to call center pho…
Browse files Browse the repository at this point in the history
…ne number
  • Loading branch information
Hashs7 committed Nov 6, 2024
1 parent 09015a7 commit 5ea5ab0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
13 changes: 8 additions & 5 deletions src/app/solicitar-ayuda/_components/Form/FormContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
'use client';

import React, { useState, useEffect, useCallback, useMemo } from 'react';
import React, { useCallback, useEffect, useMemo, useState } from 'react';

import { FormRenderer } from './FormRenderer';
import { FormData, Status } from '../types';
// @ts-expect-error
import { isValidPhone } from '@/helpers/utils';
// @ts-expect-error
import { formatPhoneNumber } from '@/helpers/utils';
import { formatPhoneNumber, isValidPhone } from '@/helpers/utils';
import { helpRequestService } from '@/lib/service';
import { Database } from '@/types/database';
import { Enums } from '@/types/common';
import { useRouter } from 'next/navigation';

import { TIPOS_DE_AYUDA_MAP, TIPOS_DE_AYUDA } from '../constants';
import { TIPOS_DE_AYUDA, TIPOS_DE_AYUDA_MAP } from '../constants';
import { useSession } from '@/context/SessionProvider';

const mapHelpToEnum = (helpTypeMap: FormData['tiposDeAyuda']): Enums['help_type_enum'][] =>
Expand Down Expand Up @@ -84,6 +82,11 @@ export function FormContainer() {
return;
}

if (!isValidPhone(formData.contacto)) {
alert('El teléfono de contacto no es válido.');
return;
}

setStatus({ isSubmitting: true, error: null, success: false });

try {
Expand Down
16 changes: 6 additions & 10 deletions src/components/CallCenterLink.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { callCenterPhone } from '@/constants/phoneNumber';
import { callCenterPhone, callCenterPhoneTrimmed } from '@/constants/phoneNumber';

export const CallCenterLink = () => {
const formattedPhone = callCenterPhone.replace(/\s/g, '');

return (
<a className="font-bold text-blue-600 hover:text-blue-800" href={`tel:+34${formattedPhone}`}>
{callCenterPhone}
</a>
);
};
export const CallCenterLink = () => (
<a className="font-bold text-blue-600 hover:text-blue-800" href={`tel:+34${callCenterPhoneTrimmed}`}>
{callCenterPhone}
</a>
);
2 changes: 2 additions & 0 deletions src/constants/phoneNumber.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const callCenterPhone = '626 675 591';

export const callCenterPhoneTrimmed = callCenterPhone.replace(/\s/g, '');
13 changes: 12 additions & 1 deletion src/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { callCenterPhoneTrimmed } from '@/constants/phoneNumber';

const isPhoneEqualToCallCenter = (phoneNumber) => {
return phoneNumber === callCenterPhoneTrimmed;
};

export const isValidPhone = (phoneNumber) => {
const phoneNumberWithoutSpaces = phoneNumber.replace(/\D/g, '');
return phoneNumberWithoutSpaces.length === 9 && /^\d[ \d]*$/.test(phoneNumberWithoutSpaces);

return (
phoneNumberWithoutSpaces.length === 9 &&
/^\d[ \d]*$/.test(phoneNumberWithoutSpaces) &&
!isPhoneEqualToCallCenter(phoneNumberWithoutSpaces)
);
};

export const formatPhoneNumber = (value) => {
Expand Down

0 comments on commit 5ea5ab0

Please sign in to comment.