import React, { Fragment } from 'react';
import { Trans } from 'react-i18next';
import { R_IPV4, R_MAC, R_HOST, R_IPV6, UNSAFE_PORTS } from '../helpers/constants';
export const renderField = ({
input,
id,
className,
placeholder,
type,
disabled,
autoComplete,
meta: { touched, error },
}) => (
{!disabled &&
touched &&
(error && {error})}
);
export const renderRadioField = ({
input, placeholder, disabled, meta: { touched, error },
}) => (
{!disabled &&
touched &&
(error && {error})}
);
export const renderSelectField = ({
input,
placeholder,
subtitle,
disabled,
modifier = 'checkbox--form',
meta: { touched, error },
}) => (
{!disabled &&
touched &&
(error && {error})}
);
export const renderServiceField = ({
input,
placeholder,
disabled,
modifier,
icon,
meta: { touched, error },
}) => (
{!disabled &&
touched &&
(error && {error})}
);
export const required = (value) => {
if (value || value === 0) {
return false;
}
return form_error_required;
};
export const ipv4 = (value) => {
if (value && !new RegExp(R_IPV4).test(value)) {
return form_error_ip_format;
}
return false;
};
export const mac = (value) => {
if (value && !new RegExp(R_MAC).test(value)) {
return form_error_mac_format;
}
return false;
};
export const isPositive = (value) => {
if ((value || value === 0) && value <= 0) {
return form_error_positive;
}
return false;
};
export const port = (value) => {
if ((value || value === 0) && (value < 80 || value > 65535)) {
return form_error_port_range;
}
return false;
};
export const portTLS = (value) => {
if (value === 0) {
return false;
} else if (value && (value < 80 || value > 65535)) {
return form_error_port_range;
}
return false;
};
export const isSafePort = (value) => {
if (UNSAFE_PORTS.includes(value)) {
return form_error_port_unsafe;
}
return false;
};
export const domain = (value) => {
if (value && !new RegExp(R_HOST).test(value)) {
return form_error_domain_format;
}
return false;
};
export const answer = (value) => {
if (
value &&
(!new RegExp(R_IPV4).test(value) &&
!new RegExp(R_IPV6).test(value) &&
!new RegExp(R_HOST).test(value))
) {
return form_error_answer_format;
}
return false;
};
export const toNumber = value => value && parseInt(value, 10);