2022-05-06 10:25:10 +02:00
|
|
|
import {
|
|
|
|
|
Accordion,
|
|
|
|
|
Button,
|
|
|
|
|
Col,
|
|
|
|
|
Grid,
|
|
|
|
|
NumberInput,
|
|
|
|
|
PasswordInput,
|
|
|
|
|
Select,
|
2022-10-10 22:14:23 +02:00
|
|
|
Stack,
|
2022-05-06 10:25:10 +02:00
|
|
|
Text,
|
|
|
|
|
TextInput,
|
|
|
|
|
} from "@mantine/core";
|
|
|
|
|
import { useForm, yupResolver } from "@mantine/form";
|
|
|
|
|
import { useModals } from "@mantine/modals";
|
|
|
|
|
import * as yup from "yup";
|
|
|
|
|
import shareService from "../../services/share.service";
|
2022-10-09 22:30:32 +02:00
|
|
|
import { ShareSecurity } from "../../types/share.type";
|
2022-05-06 10:25:10 +02:00
|
|
|
|
|
|
|
|
const CreateUploadModalBody = ({
|
|
|
|
|
uploadCallback,
|
|
|
|
|
}: {
|
|
|
|
|
uploadCallback: (
|
|
|
|
|
id: string,
|
2022-10-09 22:30:32 +02:00
|
|
|
expiration: string,
|
|
|
|
|
security: ShareSecurity
|
2022-05-06 10:25:10 +02:00
|
|
|
) => void;
|
|
|
|
|
}) => {
|
|
|
|
|
const modals = useModals();
|
|
|
|
|
const validationSchema = yup.object().shape({
|
2022-10-09 22:30:32 +02:00
|
|
|
link: yup
|
|
|
|
|
.string()
|
|
|
|
|
.required()
|
|
|
|
|
.min(3)
|
|
|
|
|
.max(100)
|
|
|
|
|
.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),
|
2022-05-06 10:25:10 +02:00
|
|
|
});
|
|
|
|
|
const form = useForm({
|
|
|
|
|
initialValues: {
|
|
|
|
|
link: "",
|
2022-10-09 22:30:32 +02:00
|
|
|
|
2022-05-06 10:25:10 +02:00
|
|
|
password: undefined,
|
2022-10-09 22:30:32 +02:00
|
|
|
maxViews: undefined,
|
|
|
|
|
expiration: "1-day",
|
2022-05-06 10:25:10 +02:00
|
|
|
},
|
2022-10-10 22:14:23 +02:00
|
|
|
validate: yupResolver(validationSchema),
|
2022-05-06 10:25:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<form
|
|
|
|
|
onSubmit={form.onSubmit(async (values) => {
|
2022-10-09 22:30:32 +02:00
|
|
|
if (!(await shareService.isShareIdAvailable(values.link))) {
|
|
|
|
|
form.setFieldError("link", "This link is already in use");
|
2022-05-06 10:25:10 +02:00
|
|
|
} else {
|
2022-10-09 22:30:32 +02:00
|
|
|
uploadCallback(values.link, values.expiration, {
|
|
|
|
|
password: values.password,
|
|
|
|
|
maxViews: values.maxViews,
|
|
|
|
|
});
|
2022-05-06 10:25:10 +02:00
|
|
|
modals.closeAll();
|
|
|
|
|
}
|
|
|
|
|
})}
|
|
|
|
|
>
|
2022-10-10 22:14:23 +02:00
|
|
|
<Stack align="stretch">
|
2022-05-06 10:25:10 +02:00
|
|
|
<Grid align={form.errors.link ? "center" : "flex-end"}>
|
|
|
|
|
<Col xs={9}>
|
|
|
|
|
<TextInput
|
|
|
|
|
variant="filled"
|
|
|
|
|
label="Link"
|
|
|
|
|
placeholder="myAwesomeShare"
|
|
|
|
|
{...form.getInputProps("link")}
|
|
|
|
|
/>
|
|
|
|
|
</Col>
|
|
|
|
|
<Col xs={3}>
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
onClick={() =>
|
|
|
|
|
form.setFieldValue(
|
|
|
|
|
"link",
|
|
|
|
|
Buffer.from(Math.random().toString(), "utf8")
|
|
|
|
|
.toString("base64")
|
|
|
|
|
.substr(10, 7)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
Generate
|
|
|
|
|
</Button>
|
|
|
|
|
</Col>
|
|
|
|
|
</Grid>
|
|
|
|
|
|
|
|
|
|
<Text
|
|
|
|
|
size="xs"
|
|
|
|
|
sx={(theme) => ({
|
|
|
|
|
color: theme.colors.gray[6],
|
|
|
|
|
})}
|
|
|
|
|
>
|
|
|
|
|
{window.location.origin}/share/
|
|
|
|
|
{form.values.link == "" ? "myAwesomeShare" : form.values.link}
|
|
|
|
|
</Text>
|
|
|
|
|
<Select
|
|
|
|
|
label="Expiration"
|
|
|
|
|
{...form.getInputProps("expiration")}
|
|
|
|
|
data={[
|
2022-10-09 22:30:32 +02:00
|
|
|
{
|
|
|
|
|
value: "10-minutes",
|
|
|
|
|
label: "10 Minutes",
|
|
|
|
|
},
|
|
|
|
|
{ value: "1-hour", label: "1 Hour" },
|
|
|
|
|
{ value: "1-day", label: "1 Day" },
|
|
|
|
|
{ value: "1-week".toString(), label: "1 Week" },
|
|
|
|
|
{ value: "1-month", label: "1 Month" },
|
2022-05-06 10:25:10 +02:00
|
|
|
]}
|
|
|
|
|
/>
|
|
|
|
|
<Accordion>
|
|
|
|
|
<Accordion.Item
|
2022-10-10 22:14:23 +02:00
|
|
|
value="Security options"
|
2022-05-06 10:25:10 +02:00
|
|
|
sx={{ borderBottom: "none" }}
|
|
|
|
|
>
|
2022-10-10 22:14:23 +02:00
|
|
|
<Stack align="stretch">
|
2022-10-09 22:30:32 +02:00
|
|
|
<PasswordInput
|
|
|
|
|
variant="filled"
|
|
|
|
|
placeholder="No password"
|
|
|
|
|
label="Password protection"
|
|
|
|
|
{...form.getInputProps("password")}
|
|
|
|
|
/>
|
|
|
|
|
|
2022-05-06 10:25:10 +02:00
|
|
|
<NumberInput
|
2022-10-09 22:30:32 +02:00
|
|
|
min={1}
|
2022-05-06 10:25:10 +02:00
|
|
|
type="number"
|
|
|
|
|
variant="filled"
|
|
|
|
|
placeholder="No limit"
|
|
|
|
|
label="Maximal views"
|
2022-10-09 22:30:32 +02:00
|
|
|
{...form.getInputProps("maxViews")}
|
2022-05-06 10:25:10 +02:00
|
|
|
/>
|
2022-10-10 22:14:23 +02:00
|
|
|
</Stack>
|
2022-05-06 10:25:10 +02:00
|
|
|
</Accordion.Item>
|
|
|
|
|
</Accordion>
|
|
|
|
|
<Button type="submit">Share</Button>
|
2022-10-10 22:14:23 +02:00
|
|
|
</Stack>
|
2022-05-06 10:25:10 +02:00
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default CreateUploadModalBody;
|