shlink-web-client/shlink-web-component/short-urls/CreateShortUrl.tsx

66 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-02-18 12:40:37 +03:00
import type { FC } from 'react';
import { useMemo } from 'react';
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';
export interface CreateShortUrlProps {
basicMode?: boolean;
}
interface CreateShortUrlConnectProps extends CreateShortUrlProps {
shortUrlCreation: ShortUrlCreation;
createShortUrl: (data: ShortUrlData) => Promise<void>;
resetCreateShortUrl: () => void;
}
const getInitialState = (settings?: ShortUrlCreationSettings): ShortUrlData => ({
longUrl: '',
tags: [],
customSlug: '',
title: undefined,
shortCodeLength: undefined,
domain: '',
validSince: undefined,
validUntil: undefined,
maxVisits: undefined,
findIfExists: false,
validateUrl: settings?.validateUrls ?? false,
forwardQuery: settings?.forwardQuery ?? true,
});
export const CreateShortUrl = (
ShortUrlForm: FC<ShortUrlFormProps>,
CreateShortUrlResult: FC<CreateShortUrlResultProps>,
) => ({
createShortUrl,
shortUrlCreation,
resetCreateShortUrl,
basicMode = false,
}: CreateShortUrlConnectProps) => {
const shortUrlCreationSettings = useSetting('shortUrlCreation');
2022-03-26 14:17:42 +03:00
const initialState = useMemo(() => getInitialState(shortUrlCreationSettings), [shortUrlCreationSettings]);
return (
2021-03-27 12:41:13 +03:00
<>
<ShortUrlForm
initialState={initialState}
saving={shortUrlCreation.saving}
2021-03-27 12:41:13 +03:00
mode={basicMode ? 'create-basic' : 'create'}
onSave={async (data: ShortUrlData) => {
resetCreateShortUrl();
return createShortUrl(data);
}}
2021-03-27 12:41:13 +03:00
/>
<CreateShortUrlResult
creation={shortUrlCreation}
resetCreateShortUrl={resetCreateShortUrl}
canBeClosed={basicMode}
/>
2021-03-27 12:41:13 +03:00
</>
);
};