shlink-web-client/src/short-urls/EditShortUrl.tsx

101 lines
3.6 KiB
TypeScript
Raw Normal View History

import { FC, useEffect, useMemo } from 'react';
import { Button, Card } from 'reactstrap';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faArrowLeft } from '@fortawesome/free-solid-svg-icons';
import { ExternalLink } from 'react-external-link';
2022-02-06 22:07:18 +03:00
import { useLocation, useParams } from 'react-router-dom';
2021-03-20 18:32:12 +03:00
import { SelectedServer } from '../servers/data';
import { Settings } from '../settings/reducers/settings';
2021-03-20 18:32:12 +03:00
import { OptionalString } from '../utils/utils';
import { parseQuery } from '../utils/helpers/query';
import { Message } from '../utils/Message';
2021-03-20 18:32:12 +03:00
import { Result } from '../utils/Result';
import { ShlinkApiError } from '../api/ShlinkApiError';
2022-11-06 14:32:55 +03:00
import { useGoBack } from '../utils/helpers/hooks';
2021-03-20 18:32:12 +03:00
import { ShortUrlFormProps } from './ShortUrlForm';
import { ShortUrlDetail } from './reducers/shortUrlDetail';
import { EditShortUrl as EditShortUrlInfo, ShortUrlEdition } from './reducers/shortUrlEdition';
import { shortUrlDataFromShortUrl, urlDecodeShortCode } from './helpers';
2021-03-20 18:32:12 +03:00
2022-02-06 22:07:18 +03:00
interface EditShortUrlConnectProps {
2021-03-20 18:32:12 +03:00
settings: Settings;
selectedServer: SelectedServer;
shortUrlDetail: ShortUrlDetail;
shortUrlEdition: ShortUrlEdition;
2021-03-20 18:32:12 +03:00
getShortUrlDetail: (shortCode: string, domain: OptionalString) => void;
2022-11-06 14:32:55 +03:00
editShortUrl: (editShortUrl: EditShortUrlInfo) => void;
2021-03-20 18:32:12 +03:00
}
export const EditShortUrl = (ShortUrlForm: FC<ShortUrlFormProps>) => ({
settings: { shortUrlCreation: shortUrlCreationSettings },
selectedServer,
shortUrlDetail,
getShortUrlDetail,
shortUrlEdition,
editShortUrl,
2021-03-20 18:32:12 +03:00
}: EditShortUrlConnectProps) => {
2022-02-06 22:07:18 +03:00
const { search } = useLocation();
const params = useParams<{ shortCode: string }>();
const goBack = useGoBack();
2021-03-20 18:32:12 +03:00
const { loading, error, errorData, shortUrl } = shortUrlDetail;
2022-11-06 14:32:55 +03:00
const { saving, saved, error: savingError, errorData: savingErrorData } = shortUrlEdition;
2021-03-20 18:32:12 +03:00
const { domain } = parseQuery<{ domain?: string }>(search);
const initialState = useMemo(
() => shortUrlDataFromShortUrl(shortUrl, shortUrlCreationSettings),
2022-03-26 14:17:42 +03:00
[shortUrl, shortUrlCreationSettings],
);
2021-03-20 18:32:12 +03:00
useEffect(() => {
params.shortCode && getShortUrlDetail(urlDecodeShortCode(params.shortCode), domain);
2021-03-20 18:32:12 +03:00
}, []);
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
<>
<header className="mb-3">
<Card body>
<h2 className="d-sm-flex justify-content-between align-items-center mb-0">
<Button color="link" size="lg" className="p-0 me-3" onClick={goBack}>
<FontAwesomeIcon icon={faArrowLeft} />
</Button>
<span className="text-center">
<small>Edit <ExternalLink href={shortUrl?.shortUrl ?? ''} /></small>
</span>
<span />
</h2>
</Card>
</header>
2021-03-27 12:41:13 +03:00
<ShortUrlForm
initialState={initialState}
2021-03-27 12:41:13 +03:00
saving={saving}
selectedServer={selectedServer}
mode="edit"
onSave={async (shortUrlData) => {
if (!shortUrl) {
return;
}
2022-11-06 14:32:55 +03:00
editShortUrl({ ...shortUrl, data: shortUrlData });
}}
2021-03-27 12:41:13 +03:00
/>
2022-11-06 14:32:55 +03:00
{saved && savingError && (
<Result type="error" className="mt-3">
<ShlinkApiError errorData={savingErrorData} fallbackMessage="An error occurred while updating short URL :(" />
</Result>
)}
2022-11-06 14:32:55 +03:00
{saved && !savingError && <Result type="success" className="mt-3">Short URL properly edited.</Result>}
2021-03-27 12:41:13 +03:00
</>
2021-03-20 18:32:12 +03:00
);
};