@itcase/forms 1.0.77 → 1.0.78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -36,6 +36,7 @@ var Button = require('@itcase/ui/components/Button');
36
36
  var Group$1 = require('@itcase/ui/components/Group');
37
37
  var Notification = require('@itcase/ui/components/Notification');
38
38
  var createDecorator = require('final-form-focus');
39
+ var apisauce = require('apisauce');
39
40
 
40
41
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
41
42
 
@@ -3149,6 +3150,67 @@ const DEFAULT_MESSAGES_FIELDS = {
3149
3150
  }
3150
3151
  };
3151
3152
 
3153
+ const parseNumericField = value => {
3154
+ const numberValue = value.slice(0, 10).replace(/,/g, '.').replace(/[^\d.]/g, '');
3155
+ const parsedValue = parseFloat(numberValue);
3156
+ if (parsedValue || parsedValue === 0) {
3157
+ if (numberValue.endsWith('.')) {
3158
+ if ((numberValue.match(/\./g) || []).length > 1) {
3159
+ return numberValue.slice(0, -1);
3160
+ }
3161
+ return numberValue;
3162
+ }
3163
+ return numberValue;
3164
+ }
3165
+ return '';
3166
+ };
3167
+ const getErrorsForFinalForm = errorData => {
3168
+ /*
3169
+ * errorData - its an "axios" error
3170
+ */
3171
+
3172
+ const formErrors = {};
3173
+ const responseErrorMessage = errorData.toJSON ? errorData.toJSON().message : errorData.message;
3174
+
3175
+ // const status = (errorData.response && errorData.response.status) || null
3176
+ const problemError = apisauce.getProblemFromError(errorData);
3177
+ // const problemStatus = getProblemFromStatus(status)
3178
+
3179
+ if (problemError === apisauce.NETWORK_ERROR || problemError === apisauce.CONNECTION_ERROR) {
3180
+ // Say to "react-final-form" that we have general error
3181
+ formErrors[finalForm.FORM_ERROR] = 'Проблемы с подключением к сервису';
3182
+ } else if (errorData.response?.data) {
3183
+ // Collect errors for some fields, which in the response from server
3184
+ const serverErrors = errorData.response.data;
3185
+ if (typeof serverErrors === 'string') {
3186
+ if (errorData.response.status === 500) {
3187
+ formErrors[finalForm.FORM_ERROR] = 'Во время обработки запроса произошла ошибка, попробуйте повторить запрос';
3188
+ }
3189
+ // formErrors[FORM_ERROR] = responseErrorMessage
3190
+ } else {
3191
+ if (errorData.response.status === 500) {
3192
+ formErrors[finalForm.FORM_ERROR] = 'Во время обработки запроса произошла ошибка, попробуйте повторить запрос';
3193
+ }
3194
+ for (const key in serverErrors) {
3195
+ // TODO: what is forms has "detail" field? show as form error is well?
3196
+ const errorFieldKey = key === 'non_field_errors' || key === 'detail' ? finalForm.FORM_ERROR : key;
3197
+ // Say to "react-final-form" that we have some fields errors
3198
+ formErrors[errorFieldKey] = castArray__default.default(serverErrors[key])[0];
3199
+ }
3200
+ }
3201
+ } else if (typeof errorData === 'object' && Object.keys(errorData).length) {
3202
+ for (const key in errorData) {
3203
+ const errorFieldKey = key === 'non_field_errors' || key === 'detail' ? finalForm.FORM_ERROR : key;
3204
+ // Say to "react-final-form" that we have some fields errors
3205
+ formErrors[errorFieldKey] = castArray__default.default(errorData[key])[0];
3206
+ }
3207
+ } else {
3208
+ // Say to "react-final-form" that we have general error
3209
+ formErrors[finalForm.FORM_ERROR] = responseErrorMessage || 'Произошла ошибка';
3210
+ }
3211
+ return formErrors;
3212
+ };
3213
+
3152
3214
  Object.defineProperty(exports, "Field", {
3153
3215
  enumerable: true,
3154
3216
  get: function () { return reactFinalForm.Field; }
@@ -3186,6 +3248,8 @@ exports.focusOnError = focusOnError;
3186
3248
  exports.focusOnErrorDecorator = focusOnErrorDecorator;
3187
3249
  exports.formTypes = formTypes;
3188
3250
  exports.generateField = generateField;
3251
+ exports.getErrorsForFinalForm = getErrorsForFinalForm;
3252
+ exports.parseNumericField = parseNumericField;
3189
3253
  exports.phoneValidation = phoneValidation;
3190
3254
  exports.sendFormDataToServer = sendFormDataToServer;
3191
3255
  exports.setErrorsMutator = setErrorsMutator;
@@ -35,6 +35,7 @@ import { Button } from '@itcase/ui/components/Button';
35
35
  import { Group as Group$1 } from '@itcase/ui/components/Group';
36
36
  import { NotificationItem } from '@itcase/ui/components/Notification';
37
37
  import createDecorator from 'final-form-focus';
38
+ import { getProblemFromError, NETWORK_ERROR, CONNECTION_ERROR } from 'apisauce';
38
39
 
39
40
  const phoneValidation = (value, context) => {
40
41
  if (!value) {
@@ -3137,4 +3138,65 @@ const DEFAULT_MESSAGES_FIELDS = {
3137
3138
  }
3138
3139
  };
3139
3140
 
3140
- export { CheckboxField as Checkbox, ChipsField, ChoiceField, CodeField, CustomField, DEFAULT_MESSAGES_FIELDS, DatePickerField, FieldWrapper, FieldWrapperBase, FileInput, FinalForm, Group, InputField, MaskedInputField, RadioGroup, SegmentedField, SelectField, SwitchField as Switch, TextareaField as Textarea, addRequiredFieldsParamToSchema, emailValidation, focusOnError, focusOnErrorDecorator, formTypes, generateField, phoneValidation, sendFormDataToServer, setErrorsMutator, useYupValidationSchema };
3141
+ const parseNumericField = value => {
3142
+ const numberValue = value.slice(0, 10).replace(/,/g, '.').replace(/[^\d.]/g, '');
3143
+ const parsedValue = parseFloat(numberValue);
3144
+ if (parsedValue || parsedValue === 0) {
3145
+ if (numberValue.endsWith('.')) {
3146
+ if ((numberValue.match(/\./g) || []).length > 1) {
3147
+ return numberValue.slice(0, -1);
3148
+ }
3149
+ return numberValue;
3150
+ }
3151
+ return numberValue;
3152
+ }
3153
+ return '';
3154
+ };
3155
+ const getErrorsForFinalForm = errorData => {
3156
+ /*
3157
+ * errorData - its an "axios" error
3158
+ */
3159
+
3160
+ const formErrors = {};
3161
+ const responseErrorMessage = errorData.toJSON ? errorData.toJSON().message : errorData.message;
3162
+
3163
+ // const status = (errorData.response && errorData.response.status) || null
3164
+ const problemError = getProblemFromError(errorData);
3165
+ // const problemStatus = getProblemFromStatus(status)
3166
+
3167
+ if (problemError === NETWORK_ERROR || problemError === CONNECTION_ERROR) {
3168
+ // Say to "react-final-form" that we have general error
3169
+ formErrors[FORM_ERROR] = 'Проблемы с подключением к сервису';
3170
+ } else if (errorData.response?.data) {
3171
+ // Collect errors for some fields, which in the response from server
3172
+ const serverErrors = errorData.response.data;
3173
+ if (typeof serverErrors === 'string') {
3174
+ if (errorData.response.status === 500) {
3175
+ formErrors[FORM_ERROR] = 'Во время обработки запроса произошла ошибка, попробуйте повторить запрос';
3176
+ }
3177
+ // formErrors[FORM_ERROR] = responseErrorMessage
3178
+ } else {
3179
+ if (errorData.response.status === 500) {
3180
+ formErrors[FORM_ERROR] = 'Во время обработки запроса произошла ошибка, попробуйте повторить запрос';
3181
+ }
3182
+ for (const key in serverErrors) {
3183
+ // TODO: what is forms has "detail" field? show as form error is well?
3184
+ const errorFieldKey = key === 'non_field_errors' || key === 'detail' ? FORM_ERROR : key;
3185
+ // Say to "react-final-form" that we have some fields errors
3186
+ formErrors[errorFieldKey] = castArray(serverErrors[key])[0];
3187
+ }
3188
+ }
3189
+ } else if (typeof errorData === 'object' && Object.keys(errorData).length) {
3190
+ for (const key in errorData) {
3191
+ const errorFieldKey = key === 'non_field_errors' || key === 'detail' ? FORM_ERROR : key;
3192
+ // Say to "react-final-form" that we have some fields errors
3193
+ formErrors[errorFieldKey] = castArray(errorData[key])[0];
3194
+ }
3195
+ } else {
3196
+ // Say to "react-final-form" that we have general error
3197
+ formErrors[FORM_ERROR] = responseErrorMessage || 'Произошла ошибка';
3198
+ }
3199
+ return formErrors;
3200
+ };
3201
+
3202
+ export { CheckboxField as Checkbox, ChipsField, ChoiceField, CodeField, CustomField, DEFAULT_MESSAGES_FIELDS, DatePickerField, FieldWrapper, FieldWrapperBase, FileInput, FinalForm, Group, InputField, MaskedInputField, RadioGroup, SegmentedField, SelectField, SwitchField as Switch, TextareaField as Textarea, addRequiredFieldsParamToSchema, emailValidation, focusOnError, focusOnErrorDecorator, formTypes, generateField, getErrorsForFinalForm, parseNumericField, phoneValidation, sendFormDataToServer, setErrorsMutator, useYupValidationSchema };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@itcase/forms",
3
- "version": "1.0.77",
3
+ "version": "1.0.78",
4
4
  "description": "Forms fields, inputs, etc.",
5
5
  "keywords": [],
6
6
  "license": "MIT",
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "@itcase/common": "^1.2.17",
34
- "@itcase/ui": "^1.3.14",
34
+ "@itcase/ui": "^1.3.15",
35
35
  "axios": "^1.7.9",
36
36
  "clsx": "^2.1.1",
37
37
  "final-form": "^4.20.10",