customer-dashboard/src/Components/MyFormInputs/index.js

333 lines
8.6 KiB
JavaScript

import { Form, Input, InputNumber, Skeleton } from "antd";
import { Constants, myFetch } from "../../utils";
import { useRef } from "react";
import { useTranslation } from "react-i18next";
export function MyUsernameFormInput({
propsFormItem,
propsInput,
showSkeleton,
thirdPerson,
}) {
const { t } = useTranslation();
return (
<MyFormInput
propsFormItem={propsFormItem}
propsInput={propsInput}
showSkeleton={showSkeleton}
formItemName="username"
minLength={Constants.GLOBALS.MIN_USERNAME_LENGTH}
maxLength={Constants.GLOBALS.MAX_USERNAME_LENGTH}
label={t("common.username")}
ruleMessageValueRequired={t("common.inputRules.usernameRequired")}
ruleMessageValueMinLengthRequired={t(
"common.inputRules.usernameMinLength",
{
minLength: Constants.GLOBALS.MIN_USERNAME_LENGTH,
}
)}
inputPlaceholder={
thirdPerson
? t("common.usernamePlaceholderThirdPerson")
: t("common.usernamePlaceholder")
}
/>
);
}
export function MyAccountNameFormInput({
propsFormItem,
propsInput,
disableAccountNameCheck,
hasFeedback,
showSkeleton,
thirdPerson,
}) {
const { t } = useTranslation();
return (
<MyAvailableCheckFormInput
propsFormItem={propsFormItem}
propsInput={propsInput}
hasFeedback={hasFeedback}
showSkeleton={showSkeleton}
formItemName="accountName"
minLength={Constants.GLOBALS.MIN_ACCOUNT_NAME_LENGTH}
maxLength={Constants.GLOBALS.MAX_ACCOUNT_NAME_LENGTH}
label={t("common.accountName")}
ruleMessageValueRequired={t("common.inputRules.accountNameRequired")}
ruleMessageValueMinLengthRequired={t(
"common.inputRules.accountNameMinLength",
{
minLength: Constants.GLOBALS.MIN_ACCOUNT_NAME_LENGTH,
}
)}
ruleMessageValueNotAvailable={t("common.inputRules.accountNameTaken")}
inputPlaceholder={
thirdPerson
? t("common.accountNamePlaceholderThirdPerson")
: t("common.accountNamePlaceholder")
}
inputType="text"
disableAvailableCheck={disableAccountNameCheck}
fetchUrl="/user/auth/check/accountname"
fetchParameter="accountName"
fetchDelay={Constants.DELAY_ACCOUNT_NAME_CHECK}
/>
);
}
export function MyPasswordFormInput({
propsFormItem,
propsInput,
formItemName,
label,
inputPlaceholder,
formItemRules,
}) {
const { t } = useTranslation();
return (
<MyFormInput
propsFormItem={propsFormItem}
propsInput={propsInput}
formItemName={formItemName || "password"}
minLength={Constants.GLOBALS.MIN_PASSWORD_LENGTH}
maxLength={Constants.GLOBALS.MAX_PASSWORD_LENGTH}
label={label || t("common.password")}
ruleMessageValueRequired={t("common.inputRules.passwordRequired")}
ruleMessageValueMinLengthRequired={t(
"common.inputRules.passwordMinLength",
{
minLength: Constants.GLOBALS.MIN_PASSWORD_LENGTH,
}
)}
inputPlaceholder={inputPlaceholder || t("common.passwordPlaceholder")}
inputType="password"
formItemRules={formItemRules}
/>
);
}
export function MyCalendarMaxFutureBookingDaysFormInput() {
const { t } = useTranslation();
return (
<MyFormInput
formItemName="calendarMaxFutureBookingDays"
minLength={Constants.GLOBALS.MIN_CALENDAR_FUTURE_BOOKING_DAYS}
maxLength={Constants.GLOBALS.MAX_CALENDAR_FUTURE_BOOKING_DAYS}
label={t("common.calendarMaxFutureBookingDays")}
ruleMessageValueRequired={t(
"common.inputRules.calendarMaxFutureBookingDaysRequired"
)}
inputPlaceholder={14}
inputType="number"
propsInput={{
addonAfter: t("common.unit.days"),
}}
/>
);
}
export function MyCalendarMinEarliestBookingTimeFormInput() {
const { t } = useTranslation();
return (
<MyFormInput
formItemName="calendarMinEarliestBookingTime"
minLength={Constants.GLOBALS.MIN_CALENDAR_EARLIEST_BOOKING_TIME}
maxLength={Constants.GLOBALS.MAX_CALENDAR_EARLIEST_BOOKING_TIME}
label={t("common.calendarMinEarliestBookingTime")}
ruleMessageValueRequired={t(
"common.inputRules.calendarMinEarliestBookingTimeRequired"
)}
inputPlaceholder={15}
inputType="number"
propsInput={{
addonAfter: t("common.unit.minutes"),
}}
/>
);
}
export function MyCompanyNameFormInput({ showSkeleton }) {
const { t } = useTranslation();
return (
<MyFormInput
formItemName="companyName"
label={t("common.companyName")}
inputPlaceholder={t("common.companyNamePlaceholder")}
showSkeleton={showSkeleton}
ruleMessageValueRequired={t("common.inputRules.companyNameRequired")}
ruleMessageValueMinLengthRequired={t(
"common.inputRules.companyNameMinLength",
{
minLength: Constants.GLOBALS.MIN_COMPANY_NAME_LENGTH,
}
)}
minLength={Constants.GLOBALS.MIN_COMPANY_NAME_LENGTH}
maxLength={Constants.GLOBALS.MAX_COMPANY_NAME_LENGTH}
/>
);
}
export function MyAvailableCheckFormInput({
propsFormItem,
propsInput,
formItemName,
minLength,
maxLength,
label,
ruleMessageValueRequired,
ruleMessageValueMinLengthRequired,
ruleMessageValueNotAvailable,
inputPlaceholder,
inputType,
disableAvailableCheck = false,
fetchUrl,
fetchParameter,
fetchDelay,
hasFeedback,
showSkeleton,
}) {
const delayTimeout = useRef();
const isValid = (value) => {
return value.length >= minLength && value.length <= maxLength;
};
return (
<MyFormInput
propsFormItem={{
...propsFormItem,
hasFeedback: hasFeedback,
}}
propsInput={propsInput}
formItemName={formItemName}
minLength={minLength}
maxLength={maxLength}
label={label}
ruleMessageValueRequired={ruleMessageValueRequired}
ruleMessageValueMinLengthRequired={ruleMessageValueMinLengthRequired}
inputPlaceholder={inputPlaceholder}
inputType={inputType}
showSkeleton={showSkeleton}
formItemRules={[
() => ({
validator(_, value) {
if (!value || !isValid(value)) {
return Promise.reject("");
}
if (disableAvailableCheck) {
return Promise.resolve();
}
if (delayTimeout.current) {
clearTimeout(delayTimeout.current);
}
return new Promise((resolve, reject) => {
delayTimeout.current = setTimeout(() => {
let body = {};
body[fetchParameter] = value; // like accountName: value
myFetch({
url: fetchUrl,
method: "POST",
body: body,
})
.then(() => resolve())
.catch(() => reject(ruleMessageValueNotAvailable));
}, fetchDelay);
});
},
}),
]}
/>
);
}
export function MyFormInput({
propsFormItem,
propsInput,
formItemName,
formItemRules,
minLength,
maxLength,
label,
ruleMessageValueRequired,
ruleMessageValueMinLengthRequired,
inputPlaceholder,
inputType,
inputNotRequired,
showSkeleton,
}) {
const commonProps = {
...propsInput,
placeholder: inputPlaceholder,
};
const myFormItemRules = [];
if (!inputNotRequired) {
myFormItemRules.push({
required: true,
message: ruleMessageValueRequired,
});
}
if (formItemRules) {
// check if formItemRules is an array
if (Array.isArray(formItemRules)) {
myFormItemRules.push(...formItemRules);
} else {
myFormItemRules.push(formItemRules);
}
}
if (inputType === "number") {
commonProps.min = minLength;
commonProps.max = maxLength;
} else {
commonProps.minLength = minLength;
commonProps.maxLength = maxLength;
myFormItemRules.push({
min: minLength,
message: ruleMessageValueMinLengthRequired,
});
}
const inputComponents = {
textarea: showSkeleton ? (
<Skeleton.Input size="large" active block />
) : (
<Input.TextArea {...commonProps} autoSize={{ minRows: 2, maxRows: 6 }} />
),
number: <InputNumber {...commonProps} />,
password: <Input.Password {...commonProps} />,
default: showSkeleton ? (
<Skeleton.Input active block />
) : (
<Input {...commonProps} />
),
};
return (
<Form.Item
{...propsFormItem}
label={label}
name={formItemName}
required
rules={myFormItemRules}
>
{inputComponents[inputType] || inputComponents.default}
</Form.Item>
);
}