2023-02-18 12:40:37 +03:00
|
|
|
import type { FC } from 'react';
|
|
|
|
import { useMemo } from 'react';
|
2023-07-23 19:30:59 +03:00
|
|
|
import type { ShortUrlCreationSettings } from '../utils/settings';
|
|
|
|
import { useSetting } from '../utils/settings';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { ShortUrlData } from './data';
|
|
|
|
import type { CreateShortUrlResultProps } from './helpers/CreateShortUrlResult';
|
2023-02-18 13:11:01 +03:00
|
|
|
import type { ShortUrlCreation } from './reducers/shortUrlCreation';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { ShortUrlFormProps } from './ShortUrlForm';
|
2020-08-30 20:45:17 +03:00
|
|
|
|
2020-12-07 22:37:03 +03:00
|
|
|
export interface CreateShortUrlProps {
|
|
|
|
basicMode?: boolean;
|
|
|
|
}
|
2020-08-30 20:45:17 +03:00
|
|
|
|
2020-12-07 22:37:03 +03:00
|
|
|
interface CreateShortUrlConnectProps extends CreateShortUrlProps {
|
2022-11-07 20:24:26 +03:00
|
|
|
shortUrlCreation: ShortUrlCreation;
|
2020-12-07 22:37:03 +03:00
|
|
|
createShortUrl: (data: ShortUrlData) => Promise<void>;
|
2020-08-30 20:45:17 +03:00
|
|
|
resetCreateShortUrl: () => void;
|
|
|
|
}
|
|
|
|
|
2021-02-14 15:23:42 +03:00
|
|
|
const getInitialState = (settings?: ShortUrlCreationSettings): ShortUrlData => ({
|
2020-08-30 20:45:17 +03:00
|
|
|
longUrl: '',
|
|
|
|
tags: [],
|
|
|
|
customSlug: '',
|
2021-03-19 21:11:27 +03:00
|
|
|
title: undefined,
|
2020-08-30 20:45:17 +03:00
|
|
|
shortCodeLength: undefined,
|
|
|
|
domain: '',
|
|
|
|
validSince: undefined,
|
|
|
|
validUntil: undefined,
|
|
|
|
maxVisits: undefined,
|
|
|
|
findIfExists: false,
|
2021-02-14 15:23:42 +03:00
|
|
|
validateUrl: settings?.validateUrls ?? false,
|
2021-10-14 00:10:22 +03:00
|
|
|
forwardQuery: settings?.forwardQuery ?? true,
|
2021-02-14 15:23:42 +03:00
|
|
|
});
|
2020-08-30 20:45:17 +03:00
|
|
|
|
2022-05-28 12:16:59 +03:00
|
|
|
export const CreateShortUrl = (
|
|
|
|
ShortUrlForm: FC<ShortUrlFormProps>,
|
|
|
|
CreateShortUrlResult: FC<CreateShortUrlResultProps>,
|
|
|
|
) => ({
|
2020-12-07 22:37:03 +03:00
|
|
|
createShortUrl,
|
2022-11-07 20:24:26 +03:00
|
|
|
shortUrlCreation,
|
2020-12-07 22:37:03 +03:00
|
|
|
resetCreateShortUrl,
|
|
|
|
basicMode = false,
|
|
|
|
}: CreateShortUrlConnectProps) => {
|
2023-07-23 19:30:59 +03:00
|
|
|
const shortUrlCreationSettings = useSetting('shortUrlCreation');
|
2022-03-26 14:17:42 +03:00
|
|
|
const initialState = useMemo(() => getInitialState(shortUrlCreationSettings), [shortUrlCreationSettings]);
|
2020-12-08 21:21:31 +03:00
|
|
|
|
|
|
|
return (
|
2021-03-27 12:41:13 +03:00
|
|
|
<>
|
|
|
|
<ShortUrlForm
|
|
|
|
initialState={initialState}
|
2022-11-07 20:24:26 +03:00
|
|
|
saving={shortUrlCreation.saving}
|
2021-03-27 12:41:13 +03:00
|
|
|
mode={basicMode ? 'create-basic' : 'create'}
|
2021-05-08 11:56:20 +03:00
|
|
|
onSave={async (data: ShortUrlData) => {
|
|
|
|
resetCreateShortUrl();
|
|
|
|
return createShortUrl(data);
|
|
|
|
}}
|
2021-03-27 12:41:13 +03:00
|
|
|
/>
|
2020-12-08 12:57:27 +03:00
|
|
|
<CreateShortUrlResult
|
2022-11-07 20:27:41 +03:00
|
|
|
creation={shortUrlCreation}
|
2020-12-08 12:57:27 +03:00
|
|
|
resetCreateShortUrl={resetCreateShortUrl}
|
|
|
|
canBeClosed={basicMode}
|
|
|
|
/>
|
2021-03-27 12:41:13 +03:00
|
|
|
</>
|
2020-08-30 20:45:17 +03:00
|
|
|
);
|
|
|
|
};
|