mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-14 11:33:51 +00:00
Implemented state machine for short URL creation
This commit is contained in:
@@ -20,7 +20,7 @@ export const CreateShortUrlResult = (useTimeoutToggle: TimeoutToggle) => (
|
|||||||
{ creation, resetCreateShortUrl, canBeClosed = false }: CreateShortUrlResultProps,
|
{ creation, resetCreateShortUrl, canBeClosed = false }: CreateShortUrlResultProps,
|
||||||
) => {
|
) => {
|
||||||
const [showCopyTooltip, setShowCopyTooltip] = useTimeoutToggle();
|
const [showCopyTooltip, setShowCopyTooltip] = useTimeoutToggle();
|
||||||
const { error, errorData, result } = creation;
|
const { error, saved } = creation;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
resetCreateShortUrl();
|
resetCreateShortUrl();
|
||||||
@@ -30,16 +30,16 @@ export const CreateShortUrlResult = (useTimeoutToggle: TimeoutToggle) => (
|
|||||||
return (
|
return (
|
||||||
<Result type="error" className="mt-3">
|
<Result type="error" className="mt-3">
|
||||||
{canBeClosed && <FontAwesomeIcon icon={closeIcon} className="float-end pointer" onClick={resetCreateShortUrl} />}
|
{canBeClosed && <FontAwesomeIcon icon={closeIcon} className="float-end pointer" onClick={resetCreateShortUrl} />}
|
||||||
<ShlinkApiError errorData={errorData} fallbackMessage="An error occurred while creating the URL :(" />
|
<ShlinkApiError errorData={creation.errorData} fallbackMessage="An error occurred while creating the URL :(" />
|
||||||
</Result>
|
</Result>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!result) {
|
if (!saved) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { shortUrl } = result;
|
const { shortUrl } = creation.result;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Result type="success" className="mt-3">
|
<Result type="success" className="mt-3">
|
||||||
|
|||||||
@@ -7,13 +7,25 @@ import { ProblemDetailsError } from '../../api/types/errors';
|
|||||||
|
|
||||||
export const CREATE_SHORT_URL = 'shlink/createShortUrl/CREATE_SHORT_URL';
|
export const CREATE_SHORT_URL = 'shlink/createShortUrl/CREATE_SHORT_URL';
|
||||||
|
|
||||||
export interface ShortUrlCreation {
|
export type ShortUrlCreation = {
|
||||||
result?: ShortUrl;
|
saving: false;
|
||||||
saving: boolean;
|
saved: false;
|
||||||
saved: boolean;
|
error: false;
|
||||||
error: boolean;
|
} | {
|
||||||
|
saving: true;
|
||||||
|
saved: false;
|
||||||
|
error: false;
|
||||||
|
} | {
|
||||||
|
saving: false;
|
||||||
|
saved: false;
|
||||||
|
error: true;
|
||||||
errorData?: ProblemDetailsError;
|
errorData?: ProblemDetailsError;
|
||||||
}
|
} | {
|
||||||
|
result: ShortUrl;
|
||||||
|
saving: false;
|
||||||
|
saved: true;
|
||||||
|
error: false;
|
||||||
|
};
|
||||||
|
|
||||||
export type CreateShortUrlAction = PayloadAction<ShortUrl>;
|
export type CreateShortUrlAction = PayloadAction<ShortUrl>;
|
||||||
|
|
||||||
@@ -31,15 +43,15 @@ export const shortUrlCreationReducerCreator = (buildShlinkApiClient: ShlinkApiCl
|
|||||||
|
|
||||||
const { reducer, actions } = createSlice({
|
const { reducer, actions } = createSlice({
|
||||||
name: 'shortUrlCreationReducer',
|
name: 'shortUrlCreationReducer',
|
||||||
initialState,
|
initialState: initialState as ShortUrlCreation, // Without this casting it infers type ShortUrlCreationWaiting
|
||||||
reducers: {
|
reducers: {
|
||||||
resetCreateShortUrl: () => initialState,
|
resetCreateShortUrl: () => initialState,
|
||||||
},
|
},
|
||||||
extraReducers: (builder) => {
|
extraReducers: (builder) => {
|
||||||
builder.addCase(createShortUrl.pending, (state) => ({ ...state, saving: true, saved: false, error: false }));
|
builder.addCase(createShortUrl.pending, () => ({ saving: true, saved: false, error: false }));
|
||||||
builder.addCase(
|
builder.addCase(
|
||||||
createShortUrl.rejected,
|
createShortUrl.rejected,
|
||||||
(state, { error }) => ({ ...state, saving: false, saved: false, error: true, errorData: parseApiError(error) }),
|
(_, { error }) => ({ saving: false, saved: false, error: true, errorData: parseApiError(error) }),
|
||||||
);
|
);
|
||||||
builder.addCase(
|
builder.addCase(
|
||||||
createShortUrl.fulfilled,
|
createShortUrl.fulfilled,
|
||||||
|
|||||||
@@ -4,34 +4,39 @@ import { CreateShortUrlResult as createResult } from '../../../src/short-urls/he
|
|||||||
import { ShortUrl } from '../../../src/short-urls/data';
|
import { ShortUrl } from '../../../src/short-urls/data';
|
||||||
import { TimeoutToggle } from '../../../src/utils/helpers/hooks';
|
import { TimeoutToggle } from '../../../src/utils/helpers/hooks';
|
||||||
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
||||||
|
import { ShortUrlCreation } from '../../../src/short-urls/reducers/shortUrlCreation';
|
||||||
|
|
||||||
describe('<CreateShortUrlResult />', () => {
|
describe('<CreateShortUrlResult />', () => {
|
||||||
const copyToClipboard = jest.fn();
|
const copyToClipboard = jest.fn();
|
||||||
const useTimeoutToggle = jest.fn(() => [false, copyToClipboard]) as TimeoutToggle;
|
const useTimeoutToggle = jest.fn(() => [false, copyToClipboard]) as TimeoutToggle;
|
||||||
const CreateShortUrlResult = createResult(useTimeoutToggle);
|
const CreateShortUrlResult = createResult(useTimeoutToggle);
|
||||||
const setUp = (result?: ShortUrl, error = false) => renderWithEvents(
|
const setUp = (creation: ShortUrlCreation) => renderWithEvents(
|
||||||
<CreateShortUrlResult resetCreateShortUrl={() => {}} creation={{ result, saving: false, error, saved: false }} />,
|
<CreateShortUrlResult resetCreateShortUrl={() => {}} creation={creation} />,
|
||||||
);
|
);
|
||||||
|
|
||||||
afterEach(jest.clearAllMocks);
|
afterEach(jest.clearAllMocks);
|
||||||
|
|
||||||
it('renders an error when error is true', () => {
|
it('renders an error when error is true', () => {
|
||||||
setUp(Mock.all<ShortUrl>(), true);
|
setUp({ error: true, saved: false, saving: false });
|
||||||
expect(screen.getByText('An error occurred while creating the URL :(')).toBeInTheDocument();
|
expect(screen.getByText('An error occurred while creating the URL :(')).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('renders nothing when no result is provided', () => {
|
it.each([[true], [false]])('renders nothing when not saved yet', (saving) => {
|
||||||
const { container } = setUp();
|
const { container } = setUp({ error: false, saved: false, saving });
|
||||||
expect(container.firstChild).toBeNull();
|
expect(container.firstChild).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('renders a result message when result is provided', () => {
|
it('renders a result message when result is provided', () => {
|
||||||
setUp(Mock.of<ShortUrl>({ shortUrl: 'https://doma.in/abc123' }));
|
setUp(
|
||||||
|
{ result: Mock.of<ShortUrl>({ shortUrl: 'https://doma.in/abc123' }), saving: false, saved: true, error: false },
|
||||||
|
);
|
||||||
expect(screen.getByText(/The short URL is/)).toHaveTextContent('Great! The short URL is https://doma.in/abc123');
|
expect(screen.getByText(/The short URL is/)).toHaveTextContent('Great! The short URL is https://doma.in/abc123');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Invokes tooltip timeout when copy to clipboard button is clicked', async () => {
|
it('Invokes tooltip timeout when copy to clipboard button is clicked', async () => {
|
||||||
const { user } = setUp(Mock.of<ShortUrl>({ shortUrl: 'https://doma.in/abc123' }));
|
const { user } = setUp(
|
||||||
|
{ result: Mock.of<ShortUrl>({ shortUrl: 'https://doma.in/abc123' }), saving: false, saved: true, error: false },
|
||||||
|
);
|
||||||
|
|
||||||
expect(copyToClipboard).not.toHaveBeenCalled();
|
expect(copyToClipboard).not.toHaveBeenCalled();
|
||||||
await user.click(screen.getByRole('button'));
|
await user.click(screen.getByRole('button'));
|
||||||
|
|||||||
Reference in New Issue
Block a user