UNPKG

@widergy/web-utils

Version:
200 lines (199 loc) 8.96 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.uploadFiles = exports.deleteValue = exports.removeAuxiliaryFields = exports.evaluateCaptcha = exports.retrieveCreditCardValues = exports.shouldShowErrors = exports.scrollToInvalid = exports.submitListener = void 0; const object_1 = require("./object"); const fieldTypes_1 = require("./constants/fieldTypes"); const array_1 = require("./array"); const submitListener = (e, submitCallback) => { if (e.key === 'Enter' && e.shiftKey === false) { e.preventDefault(); if (submitCallback) submitCallback(); } }; exports.submitListener = submitListener; const scrollToInvalid = (errors) => { if (errors) { const invalidInput = (0, object_1.findKey)(errors, (key) => key !== undefined); if (document.getElementsByName(invalidInput)[0]) { window.scrollTo(0, window.pageYOffset + (document.getElementsByName(invalidInput)[0].getBoundingClientRect().top - 100)); } } }; exports.scrollToInvalid = scrollToInvalid; const shouldShowErrors = (meta) => (!meta.active || (meta.active && meta.submitFailed)) && meta.invalid && meta.submitFailed; exports.shouldShowErrors = shouldShowErrors; const retrieveCreditCardValues = (fieldValues, fields, fieldName) => { const fieldNames = Object.keys(fieldValues); if (fieldName) { let newValues = Object.assign({}, fieldValues); delete newValues[fieldName]; newValues = Object.assign(Object.assign({}, newValues), fieldValues[fieldName]); return newValues; } const findElement = (element, index) => element.id.toString() === (fieldNames[index] && fieldNames[index].substring(2)); let index; for (let i = 0; i < fieldNames.length; i += 1) { const field = fields.find(findElement); if (field && field.type === fieldTypes_1.FIELD_ANIMATED_CREDIT_CARD) { index = i; break; } } if (index) { let newValues = Object.assign({}, fieldValues); delete newValues[fieldNames[index]]; newValues = Object.assign(Object.assign({}, newValues), fieldValues[fieldNames[index]]); return newValues; } return fieldValues; }; exports.retrieveCreditCardValues = retrieveCreditCardValues; const evaluateCaptcha = async (fieldValues, fields, validateCaptchaService) => { const fieldNames = Object.keys(fieldValues); const findElement = (element, index) => element.id.toString() === (fieldNames[index] && fieldNames[index].substring(2)); let index; for (let i = 0; i < fieldNames.length; i += 1) { const field = fields.find(findElement); if (field && field.type === fieldTypes_1.FIELD_CAPTCHA) { index = i; break; } } if (index) { const response = await validateCaptchaService(fieldValues[fieldNames[index]]); return response.data.success; } return true; }; exports.evaluateCaptcha = evaluateCaptcha; const removeAuxiliaryFields = (fields) => fields.filter((field) => !field.auxiliary); exports.removeAuxiliaryFields = removeAuxiliaryFields; const deleteValue = (state, action) => { var _a; if (action.type === '@@redux-form/REGISTER_FIELD') { if (!(0, object_1.objectIsEmpty)(state)) { const initialValue = (_a = state === null || state === void 0 ? void 0 : state.initial) === null || _a === void 0 ? void 0 : _a[action.payload.name]; if (initialValue !== undefined && initialValue !== null) { const values = Object.assign({ [action.payload.name]: initialValue }, state.values); return Object.assign(Object.assign({}, state), { values }); } } } if (action.type === '@@redux-form/UNREGISTER_FIELD') { if (!(0, object_1.objectIsEmpty)(state) && state.values) { const _b = state, _c = _b.values, _d = action.payload.name, unregistered = _c[_d], values = __rest(_c, [typeof _d === "symbol" ? _d : _d + ""]); return Object.assign(Object.assign({}, state), { values }); } } if (action.type === '@@redux-form/INITIALIZE') { if (!(0, object_1.objectIsEmpty)(state)) { return Object.assign(Object.assign({}, state), { values: {} }); } } if (action.type === '@@redux-form/RESET') { if (!(0, object_1.objectIsEmpty)(state) && !(0, object_1.objectIsEmpty)(state.values) && !(0, object_1.objectIsEmpty)(state.registeredFields)) { const newValuesKeys = Object.keys(state.values).filter((valueKey) => !!state.registeredFields[valueKey]); const newValues = {}; if (!(0, array_1.isEmpty)(newValuesKeys)) newValuesKeys.forEach((newValueKey) => { newValues[newValueKey] = state.values[newValueKey]; }); return Object.assign(Object.assign({}, state), { values: newValues }); } } if (action.type === '@@redux-form/CHANGE') { const payloadIsBoolean = typeof action.payload === 'boolean'; if ((action.payload === '' || (0, object_1.objectIsEmpty)(action.payload) || (0, array_1.isEmpty)(action.payload)) && (state === null || state === void 0 ? void 0 : state.values) && !payloadIsBoolean) { const newValues = Object.assign({}, state.values); delete newValues[action.meta.field]; return Object.assign(Object.assign({}, state), { values: newValues }); } } return state; }; exports.deleteValue = deleteValue; const uploadFile = async (file, getUrlService, onFailure) => { let url = null; const urlRes = await getUrlService(); if (urlRes.ok && urlRes.data) { try { const response = await fetch(urlRes.data.url, { method: 'PUT', body: file, headers: { 'Content-Type': file.type }, }); if (response.ok) { url = urlRes.data.get_url ? urlRes.data.get_url : urlRes.data.url; } } catch (error) { if (onFailure) onFailure(error); } } else { if (onFailure) onFailure(urlRes.error); } return url; }; const uploadFiles = async (values, fields, getUrlService, onFailure) => { const fieldsValues = Object.entries(values); const newValues = Object.assign({}, values); const fileFields = fieldsValues.filter((field) => field[1] && (field[1].file || field[1].files)); let field; for (field of fileFields) { const fieldKey = field[0].slice(2); const originalField = fields.find((_field) => _field.id.toString() === fieldKey); const isMultiple = Array.isArray(newValues[field[0]].files); const files = isMultiple ? newValues[field[0]].files : [newValues[field[0]].file]; const uploadedFiles = await Promise.all(files.map(async (fileToProcess) => { const file = (fileToProcess === null || fileToProcess === void 0 ? void 0 : fileToProcess.file) || fileToProcess; const url = await uploadFile(file, getUrlService, onFailure); if (url) { const childKeys = originalField.configuration.child_keys; if (childKeys && childKeys.url) { const fieldType = file.type; return Object.assign(Object.assign({ [childKeys.url]: url }, (childKeys.type && { [childKeys.type]: fieldType.substring(fieldType.indexOf('/') + 1), })), (childKeys.thumbnail_url && { [childKeys.thumbnail_url]: url })); } return url; } return null; })); if (uploadedFiles.some((file) => file === null)) { return null; } newValues[field[0]] = isMultiple ? uploadedFiles : uploadedFiles[0]; } return newValues; }; exports.uploadFiles = uploadFiles; const FORM_UTILS = { submitListener: exports.submitListener, scrollToInvalid: exports.scrollToInvalid, shouldShowErrors: exports.shouldShowErrors, retrieveCreditCardValues: exports.retrieveCreditCardValues, evaluateCaptcha: exports.evaluateCaptcha, removeAuxiliaryFields: exports.removeAuxiliaryFields, deleteValue: exports.deleteValue, uploadFiles: exports.uploadFiles, }; exports.default = FORM_UTILS;