Skip to content

Commit

Permalink
Allowed spaces for phone number in reservation form (#345)
Browse files Browse the repository at this point in the history
Spaces for phone number are now allowed. The number is later formatted to not include spaces when it's sent to Respa.
  • Loading branch information
SanttuA authored Sep 4, 2024
1 parent c0d0e8d commit 9e7d413
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
4 changes: 3 additions & 1 deletion app/actions/reservationActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
getRequestTypeDescriptor,
getSuccessTypeDescriptor,
} from 'utils/apiUtils';
import { getMissingValues, isStaffEvent } from 'utils/reservationUtils';
import { getMissingValues, isStaffEvent, formatPhone } from 'utils/reservationUtils';

function commentReservation(reservation, resource, comments) {
const missingValues = getMissingValues(reservation);
Expand Down Expand Up @@ -98,6 +98,8 @@ function parseReservationData(reservation) {
return value;
});

trimmedValues.reserverPhoneNumber = formatPhone(trimmedValues.reserverPhoneNumber);

const parsed = pickBy(trimmedValues, value => value || value === 0 || typeof (value) === 'boolean');
return JSON.stringify(decamelizeKeys(parsed));
}
Expand Down
7 changes: 7 additions & 0 deletions app/utils/__tests__/phoneValidationUtil.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ describe('Utils: phoneValidationUtil', () => {
expect(isValidPhoneNumber('+123-123123')).toBe(false);
});
});
describe('returns true', () => {
test('when valid number has spaces', () => {
expect(isValidPhoneNumber('55 555555')).toBe(true);
expect(isValidPhoneNumber('32 123123')).toBe(true);
expect(isValidPhoneNumber('32 123 123')).toBe(true);
});
});
});

describe('when number does not contains non numbers excluding first + char', () => {
Expand Down
8 changes: 8 additions & 0 deletions app/utils/__tests__/reservationUtils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
isManuallyConfirmedWithOrderAllowed,
normalizeUniversalFieldOptions,
mapReservationErrors,
formatPhone
} from 'utils/reservationUtils';
import { buildAPIUrl, getHeadersCreator } from '../apiUtils';
import Product from '../fixtures/Product';
Expand Down Expand Up @@ -797,4 +798,11 @@ describe('Utils: reservationUtils', () => {
});
});
});

describe('formatPhone', () => {
test('removes all whitespace characters from phone number', () => {
const phone = ' 040 1234567 ';
expect(formatPhone(phone)).toBe('0401234567');
});
});
});
4 changes: 2 additions & 2 deletions app/utils/phoneValidationUtil.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { PhoneNumberUtil } from 'google-libphonenumber';

export function isValidPhoneNumber(number) {
// only allow numbers and + if its the first char
const regex = /^([+]\d*|\d*)$/;
// only allow numbers, spaces and + if it's the first char
const regex = /^(\+?\d[\d\s]*)$/;
if (regex.test(number) === false) {
return false;
}
Expand Down
11 changes: 11 additions & 0 deletions app/utils/reservationUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,16 @@ function mapReservationErrors(errors, universalFields) {
return mappedErrors.sort((a, b) => a.order - b.order);
}

/**
* Formats given phone number by removing all whitespace characters
* @param {string} phoneNum
* @returns {string} formatted phone number
*/
function formatPhone(phoneNum) {
if (!phoneNum) return phoneNum;
return phoneNum.replace(/\s/g, '');
}

export {
combine,
isStaffEvent,
Expand All @@ -443,4 +453,5 @@ export {
isManuallyConfirmedWithOrderAllowed,
normalizeUniversalFieldOptions,
mapReservationErrors,
formatPhone,
};

0 comments on commit 9e7d413

Please sign in to comment.