Skip to content

Commit

Permalink
refactor: 💡 globale email validation helper
Browse files Browse the repository at this point in the history
  • Loading branch information
koepferd committed Apr 22, 2021
1 parent 3eef9ec commit 51ff942
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
24 changes: 24 additions & 0 deletions src/api/apiPutEmail.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { InputFieldLabelState } from '../components/inputField/InputField';
import { isStringValidEmail } from '../components/registration/registrationHelpers';
import { config } from '../resources/scripts/config';
import { translate } from '../resources/scripts/i18n/translate';
import { fetchData, FETCH_METHODS, FETCH_ERRORS } from './fetchData';

export const apiPutEmail = async (email: string): Promise<any> => {
Expand All @@ -11,3 +14,24 @@ export const apiPutEmail = async (email: string): Promise<any> => {
responseHandling: [FETCH_ERRORS.CONFLICT_WITH_RESPONSE]
});
};

export const validateEmail = (
email
): { valid: InputFieldLabelState; label: string } => {
if (email.length > 0 && isStringValidEmail(email)) {
return {
valid: 'valid',
label: translate('furtherSteps.email.overlay.input.valid')
};
} else if (email.length > 0) {
return {
valid: 'invalid',
label: translate('furtherSteps.email.overlay.input.invalid')
};
} else {
return {
valid: null,
label: translate('furtherSteps.email.overlay.input.label')
};
}
};
28 changes: 2 additions & 26 deletions src/components/message/FurtherSteps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@ import {
InputFieldItem,
InputFieldLabelState
} from '../inputField/InputField';
import {
isStringValidEmail,
ResortData
} from '../registration/registrationHelpers';
import { ResortData } from '../registration/registrationHelpers';
import { useContext, useEffect, useState } from 'react';
import {
Overlay,
OverlayItem,
OverlayWrapper,
OVERLAY_FUNCTIONS
} from '../overlay/Overlay';
import { apiPutEmail, FETCH_ERRORS } from '../../api';
import { apiPutEmail, FETCH_ERRORS, validateEmail } from '../../api';
import {
ActiveSessionGroupIdContext,
getActiveSession,
Expand Down Expand Up @@ -88,27 +85,6 @@ export const FurtherSteps = (props: FurtherStepsProps) => {
labelState: emailLabelState
};

const validateEmail = (
email
): { valid: InputFieldLabelState; label: string } => {
if (email.length > 0 && isStringValidEmail(email)) {
return {
valid: 'valid',
label: translate('furtherSteps.email.overlay.input.valid')
};
} else if (email.length > 0) {
return {
valid: 'invalid',
label: translate('furtherSteps.email.overlay.input.invalid')
};
} else {
return {
valid: null,
label: translate('furtherSteps.email.overlay.input.label')
};
}
};

const handleEmailChange = (event) => {
const validity = validateEmail(event.target.value);
setEmailLabelState(validity.valid);
Expand Down

0 comments on commit 51ff942

Please sign in to comment.