2021-03-20 18:32:12 +03:00
|
|
|
import { FC, useEffect } from 'react';
|
|
|
|
import { RouteComponentProps } from 'react-router';
|
|
|
|
import { SelectedServer } from '../servers/data';
|
|
|
|
import { Settings, ShortUrlCreationSettings } from '../settings/reducers/settings';
|
|
|
|
import { OptionalString } from '../utils/utils';
|
|
|
|
import { parseQuery } from '../utils/helpers/query';
|
|
|
|
import Message from '../utils/Message';
|
|
|
|
import { Result } from '../utils/Result';
|
|
|
|
import { ShlinkApiError } from '../api/ShlinkApiError';
|
|
|
|
import { ShortUrlFormProps } from './ShortUrlForm';
|
|
|
|
import { ShortUrlDetail } from './reducers/shortUrlDetail';
|
2021-03-27 11:49:47 +03:00
|
|
|
import { EditShortUrlData, ShortUrl, ShortUrlData } from './data';
|
2021-03-27 12:27:46 +03:00
|
|
|
import { ShortUrlEdition } from './reducers/shortUrlEdition';
|
2021-03-20 18:32:12 +03:00
|
|
|
|
|
|
|
interface EditShortUrlConnectProps extends RouteComponentProps<{ shortCode: string }> {
|
|
|
|
settings: Settings;
|
|
|
|
selectedServer: SelectedServer;
|
|
|
|
shortUrlDetail: ShortUrlDetail;
|
2021-03-27 12:27:46 +03:00
|
|
|
shortUrlEdition: ShortUrlEdition;
|
2021-03-20 18:32:12 +03:00
|
|
|
getShortUrlDetail: (shortCode: string, domain: OptionalString) => void;
|
2021-03-27 11:49:47 +03:00
|
|
|
editShortUrl: (shortUrl: string, domain: OptionalString, data: EditShortUrlData) => Promise<void>;
|
2021-03-20 18:32:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const getInitialState = (shortUrl?: ShortUrl, settings?: ShortUrlCreationSettings): ShortUrlData => {
|
|
|
|
const validateUrl = settings?.validateUrls ?? false;
|
|
|
|
|
|
|
|
if (!shortUrl) {
|
|
|
|
return { longUrl: '', validateUrl };
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
longUrl: shortUrl.longUrl,
|
|
|
|
tags: shortUrl.tags,
|
|
|
|
title: shortUrl.title ?? undefined,
|
|
|
|
domain: shortUrl.domain ?? undefined,
|
|
|
|
validSince: shortUrl.meta.validSince ?? undefined,
|
|
|
|
validUntil: shortUrl.meta.validUntil ?? undefined,
|
|
|
|
maxVisits: shortUrl.meta.maxVisits ?? undefined,
|
|
|
|
validateUrl,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const EditShortUrl = (ShortUrlForm: FC<ShortUrlFormProps>) => ({
|
|
|
|
match: { params },
|
|
|
|
location: { search },
|
|
|
|
settings: { shortUrlCreation: shortUrlCreationSettings },
|
|
|
|
selectedServer,
|
|
|
|
shortUrlDetail,
|
|
|
|
getShortUrlDetail,
|
2021-03-27 12:27:46 +03:00
|
|
|
shortUrlEdition,
|
2021-03-27 11:49:47 +03:00
|
|
|
editShortUrl,
|
2021-03-20 18:32:12 +03:00
|
|
|
}: EditShortUrlConnectProps) => {
|
|
|
|
const { loading, error, errorData, shortUrl } = shortUrlDetail;
|
2021-03-27 12:27:46 +03:00
|
|
|
const { saving, error: savingError, errorData: savingErrorData } = shortUrlEdition;
|
2021-03-20 18:32:12 +03:00
|
|
|
const { domain } = parseQuery<{ domain?: string }>(search);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
getShortUrlDetail(params.shortCode, domain);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
return <Message loading />;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return (
|
|
|
|
<Result type="error">
|
|
|
|
<ShlinkApiError errorData={errorData} fallbackMessage="An error occurred while loading short URL detail :(" />
|
|
|
|
</Result>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2021-03-27 12:41:13 +03:00
|
|
|
<>
|
|
|
|
<ShortUrlForm
|
|
|
|
initialState={getInitialState(shortUrl, shortUrlCreationSettings)}
|
|
|
|
saving={saving}
|
|
|
|
selectedServer={selectedServer}
|
|
|
|
mode="edit"
|
|
|
|
onSave={async (shortUrlData) => shortUrl && editShortUrl(shortUrl.shortCode, shortUrl.domain, shortUrlData)}
|
|
|
|
/>
|
2021-03-27 12:27:46 +03:00
|
|
|
{savingError && (
|
|
|
|
<Result type="error" className="mt-3">
|
|
|
|
<ShlinkApiError errorData={savingErrorData} fallbackMessage="An error occurred while updating short URL :(" />
|
|
|
|
</Result>
|
|
|
|
)}
|
2021-03-27 12:41:13 +03:00
|
|
|
</>
|
2021-03-20 18:32:12 +03:00
|
|
|
);
|
|
|
|
};
|