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'; import { useLocation, useParams } from 'react-router-dom'; import { SelectedServer } from '../servers/data'; import { Settings } from '../settings/reducers/settings'; import { ShortUrlIdentifier } from './data'; import { parseQuery } from '../utils/helpers/query'; import { Message } from '../utils/Message'; import { Result } from '../utils/Result'; import { ShlinkApiError } from '../api/ShlinkApiError'; import { useGoBack } from '../utils/helpers/hooks'; import { ShortUrlFormProps } from './ShortUrlForm'; import { ShortUrlDetail } from './reducers/shortUrlDetail'; import { EditShortUrl as EditShortUrlInfo, ShortUrlEdition } from './reducers/shortUrlEdition'; import { shortUrlDataFromShortUrl, urlDecodeShortCode } from './helpers'; interface EditShortUrlConnectProps { settings: Settings; selectedServer: SelectedServer; shortUrlDetail: ShortUrlDetail; shortUrlEdition: ShortUrlEdition; getShortUrlDetail: (shortUrl: ShortUrlIdentifier) => void; editShortUrl: (editShortUrl: EditShortUrlInfo) => void; } export const EditShortUrl = (ShortUrlForm: FC) => ({ settings: { shortUrlCreation: shortUrlCreationSettings }, selectedServer, shortUrlDetail, getShortUrlDetail, shortUrlEdition, editShortUrl, }: EditShortUrlConnectProps) => { const { search } = useLocation(); const params = useParams<{ shortCode: string }>(); const goBack = useGoBack(); const { loading, error, errorData, shortUrl } = shortUrlDetail; const { saving, saved, error: savingError, errorData: savingErrorData } = shortUrlEdition; const { domain } = parseQuery<{ domain?: string }>(search); const initialState = useMemo( () => shortUrlDataFromShortUrl(shortUrl, shortUrlCreationSettings), [shortUrl, shortUrlCreationSettings], ); useEffect(() => { params.shortCode && getShortUrlDetail({ shortCode: urlDecodeShortCode(params.shortCode), domain }); }, []); if (loading) { return ; } if (error) { return ( ); } return ( <>

Edit

{ if (!shortUrl) { return; } editShortUrl({ ...shortUrl, data: shortUrlData }); }} /> {saved && savingError && ( )} {saved && !savingError && Short URL properly edited.} ); };