2021-03-19 21:11:27 +03:00
|
|
|
import { FC, useMemo } from 'react';
|
2021-03-06 11:58:29 +03:00
|
|
|
import { SelectedServer } from '../servers/data';
|
2021-02-14 15:23:42 +03:00
|
|
|
import { Settings, ShortUrlCreationSettings } from '../settings/reducers/settings';
|
2020-08-30 20:45:17 +03:00
|
|
|
import { ShortUrlData } from './data';
|
|
|
|
import { ShortUrlCreation } from './reducers/shortUrlCreation';
|
|
|
|
import { CreateShortUrlResultProps } from './helpers/CreateShortUrlResult';
|
2021-03-19 21:11:27 +03:00
|
|
|
import { 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;
|
2020-08-30 20:45:17 +03:00
|
|
|
shortUrlCreationResult: ShortUrlCreation;
|
|
|
|
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,
|
|
|
|
});
|
2020-08-30 20:45:17 +03:00
|
|
|
|
2021-03-19 21:11:27 +03:00
|
|
|
const CreateShortUrl = (ShortUrlForm: FC<ShortUrlFormProps>, CreateShortUrlResult: FC<CreateShortUrlResultProps>) => ({
|
2020-12-07 22:37:03 +03:00
|
|
|
createShortUrl,
|
|
|
|
shortUrlCreationResult,
|
|
|
|
resetCreateShortUrl,
|
|
|
|
selectedServer,
|
|
|
|
basicMode = false,
|
2021-02-14 15:23:42 +03:00
|
|
|
settings: { shortUrlCreation: shortUrlCreationSettings },
|
2020-12-07 22:37:03 +03:00
|
|
|
}: CreateShortUrlConnectProps) => {
|
2021-02-14 15:23: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}
|
|
|
|
saving={shortUrlCreationResult.saving}
|
|
|
|
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
|
|
|
|
{...shortUrlCreationResult}
|
|
|
|
resetCreateShortUrl={resetCreateShortUrl}
|
|
|
|
canBeClosed={basicMode}
|
|
|
|
/>
|
2021-03-27 12:41:13 +03:00
|
|
|
</>
|
2020-08-30 20:45:17 +03:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CreateShortUrl;
|