import { Accordion, Button, Checkbox, Col, Grid, NumberInput, PasswordInput, Select, Stack, Text, TextInput, } from "@mantine/core"; import { useForm, yupResolver } from "@mantine/form"; import { useModals } from "@mantine/modals"; import moment from "moment"; import * as yup from "yup"; import shareService from "../../services/share.service"; import { ShareSecurity } from "../../types/share.type"; const PreviewExpiration = ({ form }: { form: any }) => { const value = form.values.never_expires ? "never" : form.values.expiration_num + form.values.expiration_unit; if (value === "never") return "This share will never expire."; const expirationDate = moment() .add( value.split("-")[0], value.split("-")[1] as moment.unitOfTime.DurationConstructor ) .toDate(); return `This share will expire on ${moment(expirationDate).format("LLL")}`; }; const CreateUploadModalBody = ({ uploadCallback, }: { uploadCallback: ( id: string, expiration: string, security: ShareSecurity ) => void; }) => { const modals = useModals(); const validationSchema = yup.object().shape({ link: yup .string() .required() .min(3) .max(50) .matches(new RegExp("^[a-zA-Z0-9_-]*$"), { message: "Can only contain letters, numbers, underscores and hyphens", }), password: yup.string().min(3).max(30), maxViews: yup.number().min(1), }); const form = useForm({ initialValues: { link: "", password: undefined, maxViews: undefined, expiration_num: 1, expiration_unit: "-days", never_expires: false, }, validate: yupResolver(validationSchema), }); return (
{ if (!(await shareService.isShareIdAvailable(values.link))) { form.setFieldError("link", "This link is already in use"); } else { const expiration = form.values.never_expires ? "never" : form.values.expiration_num + form.values.expiration_unit; uploadCallback(values.link, expiration, { password: values.password, maxViews: values.maxViews, }); modals.closeAll(); } })} > ({ color: theme.colors.gray[6], })} > {window.location.origin}/share/ {form.values.link == "" ? "myAwesomeShare" : form.values.link}