mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-09 17:57:26 +03:00
Merge pull request #1339 from acelaya-forks/feature/avoid-side-effects
Remove not needed usages of useEffect
This commit is contained in:
commit
c73ece41f0
3 changed files with 22 additions and 36 deletions
|
@ -1,7 +1,7 @@
|
||||||
import type { TimeoutToggle } from '@shlinkio/shlink-frontend-kit';
|
import type { TimeoutToggle } from '@shlinkio/shlink-frontend-kit';
|
||||||
import { Result, useToggle } from '@shlinkio/shlink-frontend-kit';
|
import { Result, useToggle } from '@shlinkio/shlink-frontend-kit';
|
||||||
import type { FC } from 'react';
|
import type { FC } from 'react';
|
||||||
import { useCallback, useEffect, useState } from 'react';
|
import { useCallback, useState } from 'react';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import { Button } from 'reactstrap';
|
import { Button } from 'reactstrap';
|
||||||
import { NoMenuLayout } from '../common/NoMenuLayout';
|
import { NoMenuLayout } from '../common/NoMenuLayout';
|
||||||
|
@ -50,26 +50,23 @@ const CreateServer: FCWithDeps<CreateServerProps, CreateServerDeps> = ({ servers
|
||||||
createServers([{ ...theServerData, id }]);
|
createServers([{ ...theServerData, id }]);
|
||||||
navigate(`/server/${id}`);
|
navigate(`/server/${id}`);
|
||||||
}, [createServers, navigate]);
|
}, [createServers, navigate]);
|
||||||
|
const onSubmit = useCallback((newServerData: ServerData) => {
|
||||||
useEffect(() => {
|
setServerData(newServerData);
|
||||||
if (!serverData) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const serverExists = Object.values(servers).some(
|
const serverExists = Object.values(servers).some(
|
||||||
({ url, apiKey }) => serverData?.url === url && serverData?.apiKey === apiKey,
|
({ url, apiKey }) => newServerData.url === url && newServerData.apiKey === apiKey,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (serverExists) {
|
if (serverExists) {
|
||||||
toggleConfirmModal();
|
toggleConfirmModal();
|
||||||
} else {
|
} else {
|
||||||
saveNewServer(serverData);
|
saveNewServer(newServerData);
|
||||||
}
|
}
|
||||||
}, [saveNewServer, serverData, servers, toggleConfirmModal]);
|
}, [saveNewServer, servers, toggleConfirmModal]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NoMenuLayout>
|
<NoMenuLayout>
|
||||||
<ServerForm title={<h5 className="mb-0">Add new server</h5>} onSubmit={setServerData}>
|
<ServerForm title={<h5 className="mb-0">Add new server</h5>} onSubmit={onSubmit}>
|
||||||
{!hasServers && (
|
{!hasServers && (
|
||||||
<ImportServersBtn tooltipPlacement="top" onImport={setServersImported} onImportError={setErrorImporting} />
|
<ImportServersBtn tooltipPlacement="top" onImport={setServersImported} onImportError={setErrorImporting} />
|
||||||
)}
|
)}
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
||||||
import type { TimeoutToggle } from '@shlinkio/shlink-frontend-kit';
|
import type { TimeoutToggle } from '@shlinkio/shlink-frontend-kit';
|
||||||
import { Result, SearchField, SimpleCard } from '@shlinkio/shlink-frontend-kit';
|
import { Result, SearchField, SimpleCard } from '@shlinkio/shlink-frontend-kit';
|
||||||
import type { FC } from 'react';
|
import type { FC } from 'react';
|
||||||
import { useEffect, useState } from 'react';
|
import { useMemo, useState } from 'react';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
import { Button, Row } from 'reactstrap';
|
import { Button, Row } from 'reactstrap';
|
||||||
import { NoMenuLayout } from '../common/NoMenuLayout';
|
import { NoMenuLayout } from '../common/NoMenuLayout';
|
||||||
|
@ -34,26 +34,23 @@ const ManageServers: FCWithDeps<ManageServersProps, ManageServersDeps> = ({ serv
|
||||||
useTimeoutToggle,
|
useTimeoutToggle,
|
||||||
ManageServersRow,
|
ManageServersRow,
|
||||||
} = useDependencies(ManageServers);
|
} = useDependencies(ManageServers);
|
||||||
const allServers = Object.values(servers);
|
const [searchTerm, setSearchTerm] = useState('');
|
||||||
const [serversList, setServersList] = useState(allServers);
|
const allServers = useMemo(() => Object.values(servers), [servers]);
|
||||||
const filterServers = (searchTerm: string) => setServersList(
|
const filteredServers = useMemo(
|
||||||
allServers.filter(({ name, url }) => `${name} ${url}`.toLowerCase().match(searchTerm.toLowerCase())),
|
() => allServers.filter(({ name, url }) => `${name} ${url}`.toLowerCase().match(searchTerm.toLowerCase())),
|
||||||
|
[allServers, searchTerm],
|
||||||
);
|
);
|
||||||
const hasAutoConnect = serversList.some(({ autoConnect }) => !!autoConnect);
|
const hasAutoConnect = allServers.some(({ autoConnect }) => !!autoConnect);
|
||||||
const [errorImporting, setErrorImporting] = useTimeoutToggle(false, SHOW_IMPORT_MSG_TIME);
|
const [errorImporting, setErrorImporting] = useTimeoutToggle(false, SHOW_IMPORT_MSG_TIME);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setServersList(Object.values(servers));
|
|
||||||
}, [servers]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NoMenuLayout>
|
<NoMenuLayout>
|
||||||
<SearchField className="mb-3" onChange={filterServers} />
|
<SearchField className="mb-3" onChange={setSearchTerm} />
|
||||||
|
|
||||||
<Row className="mb-3">
|
<Row className="mb-3">
|
||||||
<div className="col-md-6 d-flex d-md-block mb-2 mb-md-0">
|
<div className="col-md-6 d-flex d-md-block mb-2 mb-md-0">
|
||||||
<ImportServersBtn className="flex-fill" onImportError={setErrorImporting}>Import servers</ImportServersBtn>
|
<ImportServersBtn className="flex-fill" onImportError={setErrorImporting}>Import servers</ImportServersBtn>
|
||||||
{allServers.length > 0 && (
|
{filteredServers.length > 0 && (
|
||||||
<Button outline className="ms-2 flex-fill" onClick={async () => serversExporter.exportServers()}>
|
<Button outline className="ms-2 flex-fill" onClick={async () => serversExporter.exportServers()}>
|
||||||
<FontAwesomeIcon icon={exportIcon} fixedWidth /> Export servers
|
<FontAwesomeIcon icon={exportIcon} fixedWidth /> Export servers
|
||||||
</Button>
|
</Button>
|
||||||
|
@ -77,8 +74,8 @@ const ManageServers: FCWithDeps<ManageServersProps, ManageServersDeps> = ({ serv
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{!serversList.length && <tr className="text-center"><td colSpan={4}>No servers found.</td></tr>}
|
{!filteredServers.length && <tr className="text-center"><td colSpan={4}>No servers found.</td></tr>}
|
||||||
{serversList.map((server) => (
|
{filteredServers.map((server) => (
|
||||||
<ManageServersRow key={server.id} server={server} hasAutoConnect={hasAutoConnect} />
|
<ManageServersRow key={server.id} server={server} hasAutoConnect={hasAutoConnect} />
|
||||||
))}
|
))}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { InputFormGroup, SimpleCard } from '@shlinkio/shlink-frontend-kit';
|
import { InputFormGroup, SimpleCard } from '@shlinkio/shlink-frontend-kit';
|
||||||
import type { FC, PropsWithChildren, ReactNode } from 'react';
|
import type { FC, PropsWithChildren, ReactNode } from 'react';
|
||||||
import { useEffect, useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { handleEventPreventingDefault } from '../../utils/utils';
|
import { handleEventPreventingDefault } from '../../utils/utils';
|
||||||
import type { ServerData } from '../data';
|
import type { ServerData } from '../data';
|
||||||
|
|
||||||
|
@ -11,19 +11,11 @@ type ServerFormProps = PropsWithChildren<{
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
export const ServerForm: FC<ServerFormProps> = ({ onSubmit, initialValues, children, title }) => {
|
export const ServerForm: FC<ServerFormProps> = ({ onSubmit, initialValues, children, title }) => {
|
||||||
const [name, setName] = useState('');
|
const [name, setName] = useState(initialValues?.name ?? '');
|
||||||
const [url, setUrl] = useState('');
|
const [url, setUrl] = useState(initialValues?.url ?? '');
|
||||||
const [apiKey, setApiKey] = useState('');
|
const [apiKey, setApiKey] = useState(initialValues?.apiKey ?? '');
|
||||||
const handleSubmit = handleEventPreventingDefault(() => onSubmit({ name, url, apiKey }));
|
const handleSubmit = handleEventPreventingDefault(() => onSubmit({ name, url, apiKey }));
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (initialValues) {
|
|
||||||
setName(initialValues.name);
|
|
||||||
setUrl(initialValues.url);
|
|
||||||
setApiKey(initialValues.apiKey);
|
|
||||||
}
|
|
||||||
}, [initialValues]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<form className="server-form" name="serverForm" onSubmit={handleSubmit}>
|
<form className="server-form" name="serverForm" onSubmit={handleSubmit}>
|
||||||
<SimpleCard className="mb-3" title={title}>
|
<SimpleCard className="mb-3" title={title}>
|
||||||
|
|
Loading…
Reference in a new issue