import { FormikValues } from 'formik' import _forEach from 'lodash/forEach' import _get from 'lodash/get' import _identity from 'lodash/identity' import _isEmpty from 'lodash/isEmpty' import _map from 'lodash/map' import _split from 'lodash/split' import { combineValidators, passwordValidator, requiredValidator, } from '../../validators' import { IShareButton } from '../confirmationContainer' export const getValidateFunctions = ({ element, values, }: { element: IFormField; values: FormikValues; }) => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const validationFunctions: any[] = [] if (element.required) { validationFunctions.push(requiredValidator) } if (element.onValidate) { validationFunctions.push(element.onValidate) } if (element.name === 'password') { validationFunctions.push(passwordValidator) } if (element.name === 'confirmEmail') { const isSameEmail = (confirmEmail?: string) => values.email !== confirmEmail ? 'Please confirm your email address correctly' : null validationFunctions.push(isSameEmail) } if (element.name === 'confirmPassword') { const isSame = (confirmPassword?: string) => values.password !== confirmPassword ? 'Password confirmation does not match' : null validationFunctions.push(isSame) } return combineValidators(...validationFunctions) } export const getFormInitialValues = (fieldsSections: IFormFieldsSection[]) => { const initialValues: { [key: string]: string | number | boolean } = {} const isWindowDefined = typeof window !== 'undefined' const userData = JSON.parse( isWindowDefined ? window.localStorage.getItem('user_data') || '{}' : '{}' ) as IProfileData _forEach(fieldsSections, item => { _forEach(item.fields, fieldItem => { switch (fieldItem.name) { case 'country': case 'numTickets': initialValues[fieldItem.name] = 1 break case 'brandOptIn': initialValues[fieldItem.name] = true break case 'confirmEmail': initialValues[fieldItem.name] = _get(userData, fieldItem.name) || _get(userData, 'email') || '' break default: initialValues[fieldItem.name] = _get(userData, fieldItem.name) || '' break } }) }) return initialValues } export const updateFormFieldsAttributes = ( formFields: IFormFieldsSection[], attributes?: IFieldAttribute ) => { if (attributes && !_isEmpty(attributes)) { const updatedFormFields = _map(formFields, fieldSection => { const fieldSectionAttributes = attributes[fieldSection.name] || {} const updatedFields = _map(fieldSection.fields, fieldItem => { const fieldItemAttributes = attributes[fieldItem.name] || {} return { ...fieldItem, ...fieldItemAttributes } }) return { ...fieldSection, ...fieldSectionAttributes, fields: updatedFields, } }) return updatedFormFields } return formFields } export const shareOptionsAdapter = ( data: Array, shareUrl: string ) => { const adaptedData: Array = [] _forEach(data, option => { if (option.action !== 'shared_url') { const [platform, method] = _split(option.action, '_') const mainLabel = method ? method[0].toUpperCase() + method.substring(1) : 'Share' adaptedData.push({ mainLabel: option.action === 'twitter' ? 'Tweet' : mainLabel + ' on', subLabel: option.action === 'twitter' ? ' to your followers' : platform[0].toUpperCase() + platform.substring(1), platform, shareData: { quote: 'as', url: option.socialUrl || shareUrl, }, points: option.point, eventActionId: option.id, oneTimeAction: option.oneTimeAction, alreadyApplied: option.alreadyApplied, }) } }) return adaptedData } export const getShuffleMethodByName = ( tokenShuffleMethod: string ): ((value: string) => string) => { const reverseString = (value: string) => value.split('').reverse().join('') const symmetricShuffleString = (value: string) => { const lengthOfStr = value.length const part1 = value.substring(0, lengthOfStr / 2) return value.substring(lengthOfStr / 2) + part1 } const shuffleEveryTwoSymbolsInString = (value: string): string => { const words = value.split('') let shuffledStr = '' for (let i = 0; i < words.length; i += 2) { shuffledStr = shuffledStr.concat(words[i + 1], words[i]) } return shuffledStr } if (tokenShuffleMethod === 'reverse_string') { return reverseString } else if (tokenShuffleMethod === 'symmetric_shuffle_string') { return symmetricShuffleString } else if (tokenShuffleMethod === 'shuffle_every_two_symbols_in_string') return shuffleEveryTwoSymbolsInString return _identity }