2022-12-27 00:57:59 +00:00
|
|
|
import {
|
|
|
|
|
Form,
|
|
|
|
|
FormRow,
|
2023-01-06 06:56:36 +00:00
|
|
|
FormButtons,
|
|
|
|
|
Flexbox,
|
2022-12-27 00:57:59 +00:00
|
|
|
TextField,
|
2023-01-06 06:56:36 +00:00
|
|
|
Button,
|
2022-12-27 00:57:59 +00:00
|
|
|
Toggle,
|
2024-01-29 11:15:22 +00:00
|
|
|
LoadingButton,
|
2022-12-27 00:57:59 +00:00
|
|
|
} from 'react-basics';
|
2024-02-05 03:53:06 +00:00
|
|
|
import { useContext, useState } from 'react';
|
2023-01-06 06:56:36 +00:00
|
|
|
import { getRandomChars } from 'next-basics';
|
2024-02-08 07:48:51 +00:00
|
|
|
import { useApi, useMessages, useModified } from 'components/hooks';
|
2024-02-05 03:53:06 +00:00
|
|
|
import { WebsiteContext } from 'app/(main)/websites/[websiteId]/WebsiteProvider';
|
2020-08-08 03:36:57 +00:00
|
|
|
|
2023-01-06 06:56:36 +00:00
|
|
|
const generateId = () => getRandomChars(16);
|
|
|
|
|
|
2024-05-12 04:52:40 +00:00
|
|
|
export function ShareUrl({ onSave }: { websiteId: string; onSave?: () => void }) {
|
2024-02-05 03:53:06 +00:00
|
|
|
const website = useContext(WebsiteContext);
|
2024-01-29 11:15:22 +00:00
|
|
|
const { domain, shareId } = website;
|
2023-03-22 21:05:55 +00:00
|
|
|
const { formatMessage, labels, messages } = useMessages();
|
2022-12-27 00:57:59 +00:00
|
|
|
const [id, setId] = useState(shareId);
|
2023-01-06 06:56:36 +00:00
|
|
|
const { post, useMutation } = useApi();
|
2024-01-29 11:15:22 +00:00
|
|
|
const { mutate, error, isPending } = useMutation({
|
|
|
|
|
mutationFn: (data: any) => post(`/websites/${website.id}`, data),
|
2023-12-03 11:07:03 +00:00
|
|
|
});
|
2024-02-08 07:48:51 +00:00
|
|
|
const { touch } = useModified();
|
2022-12-27 00:57:59 +00:00
|
|
|
|
2024-05-12 04:52:40 +00:00
|
|
|
const url = `${process.env.shareUrlHost || window?.location.origin || ''}${
|
|
|
|
|
process.env.basePath || ''
|
2024-03-27 09:17:55 +00:00
|
|
|
}/share/${id}/${domain}`;
|
2022-12-27 00:57:59 +00:00
|
|
|
|
|
|
|
|
const handleGenerate = () => {
|
2024-01-29 11:15:22 +00:00
|
|
|
setId(generateId());
|
2022-12-27 00:57:59 +00:00
|
|
|
};
|
|
|
|
|
|
2023-12-03 11:07:03 +00:00
|
|
|
const handleCheck = (checked: boolean) => {
|
2022-12-27 00:57:59 +00:00
|
|
|
const data = { shareId: checked ? generateId() : null };
|
|
|
|
|
mutate(data, {
|
|
|
|
|
onSuccess: async () => {
|
2024-02-15 07:21:35 +00:00
|
|
|
touch(`website:${website.id}`);
|
|
|
|
|
onSave?.();
|
2022-12-27 00:57:59 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
setId(data.shareId);
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-29 11:15:22 +00:00
|
|
|
const handleSave = () => {
|
|
|
|
|
mutate(
|
|
|
|
|
{ shareId: id },
|
|
|
|
|
{
|
|
|
|
|
onSuccess: async () => {
|
2024-02-15 07:21:35 +00:00
|
|
|
touch(`website:${website.id}`);
|
|
|
|
|
onSave?.();
|
2024-01-29 11:15:22 +00:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
};
|
2020-08-08 03:36:57 +00:00
|
|
|
|
|
|
|
|
return (
|
2023-01-25 15:42:46 +00:00
|
|
|
<>
|
|
|
|
|
<Toggle checked={Boolean(id)} onChecked={handleCheck} style={{ marginBottom: 30 }}>
|
|
|
|
|
{formatMessage(labels.enableShareUrl)}
|
|
|
|
|
</Toggle>
|
2022-12-27 00:57:59 +00:00
|
|
|
{id && (
|
2024-01-29 11:15:22 +00:00
|
|
|
<Form error={error}>
|
2022-12-27 00:57:59 +00:00
|
|
|
<FormRow>
|
2023-01-25 15:42:46 +00:00
|
|
|
<p>{formatMessage(messages.shareUrl)}</p>
|
2023-01-06 06:56:36 +00:00
|
|
|
<Flexbox gap={10}>
|
|
|
|
|
<TextField value={url} readOnly allowCopy />
|
2023-01-25 15:42:46 +00:00
|
|
|
<Button onClick={handleGenerate}>{formatMessage(labels.regenerate)}</Button>
|
2023-01-06 06:56:36 +00:00
|
|
|
</Flexbox>
|
2022-12-27 00:57:59 +00:00
|
|
|
</FormRow>
|
|
|
|
|
<FormButtons>
|
2024-01-29 11:15:22 +00:00
|
|
|
<LoadingButton
|
|
|
|
|
variant="primary"
|
|
|
|
|
disabled={id === shareId}
|
|
|
|
|
isLoading={isPending}
|
|
|
|
|
onClick={handleSave}
|
|
|
|
|
>
|
|
|
|
|
{formatMessage(labels.save)}
|
|
|
|
|
</LoadingButton>
|
2022-12-27 00:57:59 +00:00
|
|
|
</FormButtons>
|
2023-01-25 15:42:46 +00:00
|
|
|
</Form>
|
2022-12-27 00:57:59 +00:00
|
|
|
)}
|
2023-01-25 15:42:46 +00:00
|
|
|
</>
|
2020-08-08 03:36:57 +00:00
|
|
|
);
|
|
|
|
|
}
|
2023-04-21 15:00:42 +00:00
|
|
|
|
|
|
|
|
export default ShareUrl;
|