Skip to content

Commit 9218492

Browse files
authored
Merge pull request #849 from acelaya-forks/long-url-query
Allow long URL to be preset via query param in short URL creation
2 parents 30a52ed + b471d11 commit 9218492

File tree

3 files changed

+48
-30
lines changed

3 files changed

+48
-30
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org).
66

7-
## [Unreleased]
7+
## [0.17.0] - 2025-11-12
88
### Added
99
* [#839](https://github.com/shlinkio/shlink-web-component/issues/839) Allow filtering short URLs by excluded tags when using Shlink >=4.6.0
1010
* [#838](https://github.com/shlinkio/shlink-web-component/issues/838) Allow filtering tag, orphan and non-orphan visits by domain, when using Shlink >=4.6.0
11+
* [#784](https://github.com/shlinkio/shlink-web-component/issues/784) Add optional `long-url` query parameter to short URL creation to prefill the long URL programmatically.
1112

1213
### Changed
1314
* [#519](https://github.com/shlinkio/shlink-web-component/issues/519) Redesign short URLs filtering bar for more clarity and consistency

src/short-urls/CreateShortUrl.tsx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { useParsedQuery } from '@shlinkio/shlink-frontend-kit';
12
import type { FC } from 'react';
23
import { useMemo } from 'react';
34
import type { ShlinkCreateShortUrlData } from '../api-contract';
45
import type { FCWithDeps } from '../container/utils';
56
import { componentFactory, useDependencies } from '../container/utils';
6-
import type { ShortUrlCreationSettings } from '../settings';
77
import { useSetting } from '../settings';
88
import { CreateShortUrlResult } from './helpers/CreateShortUrlResult';
99
import type { ShortUrlCreation } from './reducers/shortUrlCreation';
@@ -23,20 +23,6 @@ type CreateShortUrlDeps = {
2323
ShortUrlForm: FC<ShortUrlFormProps<ShlinkCreateShortUrlData>>;
2424
};
2525

26-
const getInitialState = (settings?: ShortUrlCreationSettings): ShlinkCreateShortUrlData => ({
27-
longUrl: '',
28-
tags: [],
29-
customSlug: '',
30-
title: undefined,
31-
shortCodeLength: undefined,
32-
domain: '',
33-
validSince: undefined,
34-
validUntil: undefined,
35-
maxVisits: undefined,
36-
findIfExists: false,
37-
forwardQuery: settings?.forwardQuery ?? true,
38-
});
39-
4026
const CreateShortUrl: FCWithDeps<CreateShortUrlConnectProps, CreateShortUrlDeps> = ({
4127
createShortUrl,
4228
shortUrlCreation,
@@ -45,7 +31,23 @@ const CreateShortUrl: FCWithDeps<CreateShortUrlConnectProps, CreateShortUrlDeps>
4531
}: CreateShortUrlConnectProps) => {
4632
const { ShortUrlForm } = useDependencies(CreateShortUrl);
4733
const shortUrlCreationSettings = useSetting('shortUrlCreation');
48-
const initialState = useMemo(() => getInitialState(shortUrlCreationSettings), [shortUrlCreationSettings]);
34+
const { 'long-url': longUrlFromQuery = '' } = useParsedQuery<{ 'long-url'?: string }>();
35+
const initialState = useMemo(
36+
(): ShlinkCreateShortUrlData => ({
37+
longUrl: longUrlFromQuery,
38+
tags: [],
39+
customSlug: '',
40+
title: undefined,
41+
shortCodeLength: undefined,
42+
domain: '',
43+
validSince: undefined,
44+
validUntil: undefined,
45+
maxVisits: undefined,
46+
findIfExists: false,
47+
forwardQuery: shortUrlCreationSettings?.forwardQuery ?? true,
48+
}),
49+
[longUrlFromQuery, shortUrlCreationSettings?.forwardQuery],
50+
);
4951

5052
return (
5153
<>
Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
11
import { render, screen } from '@testing-library/react';
22
import { fromPartial } from '@total-typescript/shoehorn';
3+
import { createMemoryHistory } from 'history';
4+
import { Router } from 'react-router';
5+
import type { ShlinkCreateShortUrlData } from '../../src/api-contract';
36
import { SettingsProvider } from '../../src/settings';
47
import { CreateShortUrlFactory } from '../../src/short-urls/CreateShortUrl';
58
import type { ShortUrlCreation } from '../../src/short-urls/reducers/shortUrlCreation';
9+
import type { ShortUrlFormProps } from '../../src/short-urls/ShortUrlForm';
610
import { checkAccessibility } from '../__helpers__/accessibility';
711

812
describe('<CreateShortUrl />', () => {
9-
const ShortUrlForm = () => <span>ShortUrlForm</span>;
13+
const ShortUrlForm = ({ initialState }: ShortUrlFormProps<ShlinkCreateShortUrlData>) => (
14+
<span>ShortUrlForm ({initialState.longUrl})</span>
15+
);
1016
const shortUrlCreationResult = fromPartial<ShortUrlCreation>({});
1117
const createShortUrl = vi.fn(async () => Promise.resolve());
1218
const CreateShortUrl = CreateShortUrlFactory(fromPartial({ ShortUrlForm }));
13-
const setUp = () => render(
14-
<SettingsProvider value={{}}>
15-
<CreateShortUrl
16-
shortUrlCreation={shortUrlCreationResult}
17-
createShortUrl={createShortUrl}
18-
resetCreateShortUrl={() => {}}
19-
/>
20-
</SettingsProvider>,
21-
);
19+
const setUp = (longUrlQuery?: string) => {
20+
const history = createMemoryHistory();
21+
if (longUrlQuery) {
22+
history.push({ search: `?long-url=${longUrlQuery}` });
23+
}
24+
25+
return render(
26+
<Router location={history.location} navigator={history}>
27+
<SettingsProvider value={{}}>
28+
<CreateShortUrl
29+
shortUrlCreation={shortUrlCreationResult}
30+
createShortUrl={createShortUrl}
31+
resetCreateShortUrl={() => {}}
32+
/>
33+
</SettingsProvider>
34+
</Router>,
35+
);
36+
};
2237

2338
it('passes a11y checks', () => checkAccessibility(setUp()));
2439

25-
it('renders computed initial state', () => {
26-
setUp();
27-
expect(screen.getByText('ShortUrlForm')).toBeInTheDocument();
40+
it.each([undefined, 'https://example.com'])('renders initial long URL', (longUrl) => {
41+
setUp(longUrl);
42+
expect(screen.getByText(`ShortUrlForm (${longUrl ?? ''})`)).toBeInTheDocument();
2843
});
2944
});

0 commit comments

Comments
 (0)