2023-02-18 12:40:37 +03:00
|
|
|
import type { FC } from 'react';
|
|
|
|
import { useMemo } from 'react';
|
|
|
|
import type { SelectedServer } from '../servers/data';
|
|
|
|
import type { Settings, ShortUrlCreationSettings } from '../settings/reducers/settings';
|
|
|
|
import type { ShortUrlData } from './data';
|
|
|
|
import type { ShortUrlCreation } from './reducers/shortUrlCreation';
|
|
|
|
import type { CreateShortUrlResultProps } from './helpers/CreateShortUrlResult';
|
|
|
|
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 {
|
2021-02-14 15:23:42 +03:00
|
|
|
settings: Settings;
|
2022-11-07 20:24:26 +03:00
|
|
|
shortUrlCreation: ShortUrlCreation;
|
2020-08-30 20:45:17 +03:00
|
|
|
selectedServer: SelectedServer;
|
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,
|
|
|
|
selectedServer,
|
|
|
|
basicMode = false,
|
2021-02-14 15:23:42 +03:00
|
|
|
settings: { shortUrlCreation: shortUrlCreationSettings },
|
2020-12-07 22:37:03 +03:00
|
|
|
}: CreateShortUrlConnectProps) => {
|
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
|
|
|
selectedServer={selectedServer}
|
|
|
|
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
|
|
|
);
|
|
|
|
};
|