mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 17:40:23 +03:00
Created modal to warn when creating a duplicated server
This commit is contained in:
parent
f07e7fd31c
commit
1f9356cc21
3 changed files with 83 additions and 11 deletions
|
@ -1,13 +1,14 @@
|
||||||
import { FC } from 'react';
|
import { FC, useEffect, useState } from 'react';
|
||||||
import { v4 as uuid } from 'uuid';
|
import { v4 as uuid } from 'uuid';
|
||||||
import { RouterProps } from 'react-router';
|
import { RouterProps } from 'react-router';
|
||||||
import { Button } from 'reactstrap';
|
import { Button } from 'reactstrap';
|
||||||
import { Result } from '../utils/Result';
|
import { Result } from '../utils/Result';
|
||||||
import { NoMenuLayout } from '../common/NoMenuLayout';
|
import { NoMenuLayout } from '../common/NoMenuLayout';
|
||||||
import { StateFlagTimeout } from '../utils/helpers/hooks';
|
import { StateFlagTimeout, useToggle } from '../utils/helpers/hooks';
|
||||||
import { ServerForm } from './helpers/ServerForm';
|
import { ServerForm } from './helpers/ServerForm';
|
||||||
import { ImportServersBtnProps } from './helpers/ImportServersBtn';
|
import { ImportServersBtnProps } from './helpers/ImportServersBtn';
|
||||||
import { ServerData, ServersMap, ServerWithId } from './data';
|
import { ServerData, ServersMap, ServerWithId } from './data';
|
||||||
|
import { DuplicatedServerModal } from './helpers/DuplicatedServerModal';
|
||||||
|
|
||||||
const SHOW_IMPORT_MSG_TIME = 4000;
|
const SHOW_IMPORT_MSG_TIME = 4000;
|
||||||
|
|
||||||
|
@ -31,16 +32,30 @@ const CreateServer = (ImportServersBtn: FC<ImportServersBtnProps>, useStateFlagT
|
||||||
const hasServers = !!Object.keys(servers).length;
|
const hasServers = !!Object.keys(servers).length;
|
||||||
const [ serversImported, setServersImported ] = useStateFlagTimeout(false, SHOW_IMPORT_MSG_TIME);
|
const [ serversImported, setServersImported ] = useStateFlagTimeout(false, SHOW_IMPORT_MSG_TIME);
|
||||||
const [ errorImporting, setErrorImporting ] = useStateFlagTimeout(false, SHOW_IMPORT_MSG_TIME);
|
const [ errorImporting, setErrorImporting ] = useStateFlagTimeout(false, SHOW_IMPORT_MSG_TIME);
|
||||||
const handleSubmit = (serverData: ServerData) => {
|
const [ isConfirmModalOpen, toggleConfirmModal ] = useToggle();
|
||||||
|
const [ serverData, setServerData ] = useState<ServerData | undefined>();
|
||||||
|
const save = () => {
|
||||||
|
if (!serverData) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const id = uuid();
|
const id = uuid();
|
||||||
|
|
||||||
createServer({ ...serverData, id });
|
createServer({ ...serverData, id });
|
||||||
push(`/server/${id}`);
|
push(`/server/${id}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const serverExists = Object.values(servers).some(
|
||||||
|
({ url, apiKey }) => serverData?.url === url && serverData?.apiKey === apiKey,
|
||||||
|
);
|
||||||
|
|
||||||
|
serverExists ? toggleConfirmModal() : save();
|
||||||
|
}, [ serverData ]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NoMenuLayout>
|
<NoMenuLayout>
|
||||||
<ServerForm title={<h5 className="mb-0">Add new server</h5>} onSubmit={handleSubmit}>
|
<ServerForm title={<h5 className="mb-0">Add new server</h5>} onSubmit={setServerData}>
|
||||||
{!hasServers &&
|
{!hasServers &&
|
||||||
<ImportServersBtn tooltipPlacement="top" onImport={setServersImported} onImportError={setErrorImporting} />}
|
<ImportServersBtn tooltipPlacement="top" onImport={setServersImported} onImportError={setErrorImporting} />}
|
||||||
{hasServers && <Button outline onClick={goBack}>Cancel</Button>}
|
{hasServers && <Button outline onClick={goBack}>Cancel</Button>}
|
||||||
|
@ -49,6 +64,14 @@ const CreateServer = (ImportServersBtn: FC<ImportServersBtnProps>, useStateFlagT
|
||||||
|
|
||||||
{serversImported && <ImportResult type="success" />}
|
{serversImported && <ImportResult type="success" />}
|
||||||
{errorImporting && <ImportResult type="error" />}
|
{errorImporting && <ImportResult type="error" />}
|
||||||
|
|
||||||
|
<DuplicatedServerModal
|
||||||
|
isOpen={isConfirmModalOpen}
|
||||||
|
toggle={toggleConfirmModal}
|
||||||
|
serverData={serverData}
|
||||||
|
onDiscard={goBack}
|
||||||
|
onSave={save}
|
||||||
|
/>
|
||||||
</NoMenuLayout>
|
</NoMenuLayout>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
31
src/servers/helpers/DuplicatedServerModal.tsx
Normal file
31
src/servers/helpers/DuplicatedServerModal.tsx
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
import { FC } from 'react';
|
||||||
|
import { Button, Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap';
|
||||||
|
import { ServerData } from '../data';
|
||||||
|
|
||||||
|
interface DuplicatedServerModalProps {
|
||||||
|
serverData?: ServerData;
|
||||||
|
isOpen: boolean;
|
||||||
|
toggle: () => void;
|
||||||
|
onDiscard: () => void;
|
||||||
|
onSave: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const DuplicatedServerModal: FC<DuplicatedServerModalProps> = (
|
||||||
|
{ isOpen, toggle, serverData, onDiscard, onSave },
|
||||||
|
) => (
|
||||||
|
<Modal centered isOpen={isOpen} toggle={toggle}>
|
||||||
|
<ModalHeader>Duplicated server</ModalHeader>
|
||||||
|
<ModalBody>
|
||||||
|
<p>There is already a server with:</p>
|
||||||
|
<ul>
|
||||||
|
<li>URL: <b>{serverData?.url}</b></li>
|
||||||
|
<li>API key: <b>{serverData?.apiKey}</b></li>
|
||||||
|
</ul>
|
||||||
|
Do you want to save this server anyway?
|
||||||
|
</ModalBody>
|
||||||
|
<ModalFooter>
|
||||||
|
<Button color="link" onClick={onDiscard}>Discard</Button>
|
||||||
|
<Button color="primary" onClick={onSave}>Save anyway</Button>
|
||||||
|
</ModalFooter>
|
||||||
|
</Modal>
|
||||||
|
);
|
|
@ -4,18 +4,21 @@ import { History } from 'history';
|
||||||
import createServerConstruct from '../../src/servers/CreateServer';
|
import createServerConstruct from '../../src/servers/CreateServer';
|
||||||
import { ServerForm } from '../../src/servers/helpers/ServerForm';
|
import { ServerForm } from '../../src/servers/helpers/ServerForm';
|
||||||
import { ServerWithId } from '../../src/servers/data';
|
import { ServerWithId } from '../../src/servers/data';
|
||||||
|
import { DuplicatedServerModal } from '../../src/servers/helpers/DuplicatedServerModal';
|
||||||
|
|
||||||
describe('<CreateServer />', () => {
|
describe('<CreateServer />', () => {
|
||||||
let wrapper: ShallowWrapper;
|
let wrapper: ShallowWrapper;
|
||||||
const ImportServersBtn = () => null;
|
const ImportServersBtn = () => null;
|
||||||
const createServerMock = jest.fn();
|
const createServerMock = jest.fn();
|
||||||
const push = jest.fn();
|
const push = jest.fn();
|
||||||
const historyMock = Mock.of<History>({ push });
|
const goBack = jest.fn();
|
||||||
|
const historyMock = Mock.of<History>({ push, goBack });
|
||||||
const servers = { foo: Mock.all<ServerWithId>() };
|
const servers = { foo: Mock.all<ServerWithId>() };
|
||||||
const createWrapper = (serversImported = false, importFailed = false) => {
|
const createWrapper = (serversImported = false, importFailed = false) => {
|
||||||
const useStateFlagTimeout = jest.fn()
|
const useStateFlagTimeout = jest.fn()
|
||||||
.mockReturnValueOnce([ serversImported, () => '' ])
|
.mockReturnValueOnce([ serversImported, () => '' ])
|
||||||
.mockReturnValueOnce([ importFailed, () => '' ]);
|
.mockReturnValueOnce([ importFailed, () => '' ])
|
||||||
|
.mockReturnValue([]);
|
||||||
const CreateServer = createServerConstruct(ImportServersBtn, useStateFlagTimeout);
|
const CreateServer = createServerConstruct(ImportServersBtn, useStateFlagTimeout);
|
||||||
|
|
||||||
wrapper = shallow(<CreateServer createServer={createServerMock} history={historyMock} servers={servers} />);
|
wrapper = shallow(<CreateServer createServer={createServerMock} history={historyMock} servers={servers} />);
|
||||||
|
@ -23,10 +26,8 @@ describe('<CreateServer />', () => {
|
||||||
return wrapper;
|
return wrapper;
|
||||||
};
|
};
|
||||||
|
|
||||||
afterEach(() => {
|
beforeEach(jest.clearAllMocks);
|
||||||
jest.resetAllMocks();
|
afterEach(() => wrapper?.unmount());
|
||||||
wrapper?.unmount();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('renders components', () => {
|
it('renders components', () => {
|
||||||
const wrapper = createWrapper();
|
const wrapper = createWrapper();
|
||||||
|
@ -51,13 +52,30 @@ describe('<CreateServer />', () => {
|
||||||
expect(result.prop('type')).toEqual('error');
|
expect(result.prop('type')).toEqual('error');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('creates server and redirects to it when form is submitted', () => {
|
it('creates server data form is submitted', () => {
|
||||||
const wrapper = createWrapper();
|
const wrapper = createWrapper();
|
||||||
const form = wrapper.find(ServerForm);
|
const form = wrapper.find(ServerForm);
|
||||||
|
|
||||||
|
expect(wrapper.find(DuplicatedServerModal).prop('serverData')).not.toBeDefined();
|
||||||
form.simulate('submit', {});
|
form.simulate('submit', {});
|
||||||
|
expect(wrapper.find(DuplicatedServerModal).prop('serverData')).toEqual({});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('saves server and redirects on modal save', () => {
|
||||||
|
const wrapper = createWrapper();
|
||||||
|
|
||||||
|
wrapper.find(ServerForm).simulate('submit', {});
|
||||||
|
wrapper.find(DuplicatedServerModal).simulate('save');
|
||||||
|
|
||||||
expect(createServerMock).toHaveBeenCalledTimes(1);
|
expect(createServerMock).toHaveBeenCalledTimes(1);
|
||||||
expect(push).toHaveBeenCalledTimes(1);
|
expect(push).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('goes back on modal discard', () => {
|
||||||
|
const wrapper = createWrapper();
|
||||||
|
|
||||||
|
wrapper.find(DuplicatedServerModal).simulate('discard');
|
||||||
|
|
||||||
|
expect(goBack).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue