Handled loading server in just one place, and added error handling for loading servers

This commit is contained in:
Alejandro Celaya 2020-03-08 10:00:25 +01:00
parent f4cc8d3a0c
commit c8d682cc98
5 changed files with 53 additions and 31 deletions

View file

@ -20,19 +20,30 @@ const propTypes = {
const MenuLayout = (TagsList, ShortUrls, AsideMenu, CreateShortUrl, ShortUrlVisits, ShlinkVersions) => { const MenuLayout = (TagsList, ShortUrls, AsideMenu, CreateShortUrl, ShortUrlVisits, ShlinkVersions) => {
const MenuLayoutComp = ({ match, location, selectedServer, selectServer }) => { const MenuLayoutComp = ({ match, location, selectedServer, selectServer }) => {
const [ showSideBar, setShowSidebar ] = useState(false); const [ showSideBar, setShowSidebar ] = useState(false);
useEffect(() => {
const { params: { serverId } } = match; const { params: { serverId } } = match;
useEffect(() => {
selectServer(serverId); selectServer(serverId);
}, []); }, [ serverId ]);
useEffect(() => setShowSidebar(false), [ location ]); useEffect(() => setShowSidebar(false), [ location ]);
if (!selectedServer) { if (!selectedServer) {
return <MutedMessage loading />; return <MutedMessage loading />;
} }
const { params: { serverId } } = match; if (selectedServer.serverNotFound) {
return <MutedMessage>Could not find a server with id <b>&quot;{serverId}&quot;</b> in this host.</MutedMessage>;
}
if (selectedServer.serverNotReachable) {
return (
<MutedMessage>
Oops! Could not connect to Shlink server with ID <b>&quot;{serverId}&quot;</b>. Make sure you have internet
connection, the server is properly configured and it is on-line.
</MutedMessage>
);
}
const burgerClasses = classNames('menu-layout__burger-icon', { const burgerClasses = classNames('menu-layout__burger-icon', {
'menu-layout__burger-icon--active': showSideBar, 'menu-layout__burger-icon--active': showSideBar,
}); });

View file

@ -8,7 +8,6 @@ const ServersDropdown = (serversExporter) => class ServersDropdown extends React
static propTypes = { static propTypes = {
servers: PropTypes.object, servers: PropTypes.object,
selectedServer: serverType, selectedServer: serverType,
selectServer: PropTypes.func,
listServers: PropTypes.func, listServers: PropTypes.func,
history: PropTypes.shape({ history: PropTypes.shape({
push: PropTypes.func, push: PropTypes.func,
@ -16,14 +15,10 @@ const ServersDropdown = (serversExporter) => class ServersDropdown extends React
}; };
renderServers = () => { renderServers = () => {
const { servers: { list, loading }, selectedServer, selectServer } = this.props; const { servers: { list, loading }, selectedServer } = this.props;
const servers = values(list); const servers = values(list);
const { push } = this.props.history; const { push } = this.props.history;
const loadServer = (id) => { const loadServer = (id) => push(`/server/${id}/list-short-urls/1`);
selectServer(id)
.then(() => push(`/server/${id}/list-short-urls/1`))
.catch(() => {});
};
if (loading) { if (loading) {
return <DropdownItem disabled><i>Trying to load servers...</i></DropdownItem>; return <DropdownItem disabled><i>Trying to load servers...</i></DropdownItem>;
@ -41,10 +36,7 @@ const ServersDropdown = (serversExporter) => class ServersDropdown extends React
</DropdownItem> </DropdownItem>
))} ))}
<DropdownItem divider /> <DropdownItem divider />
<DropdownItem <DropdownItem className="servers-dropdown__export-item" onClick={() => serversExporter.exportServers()}>
className="servers-dropdown__export-item"
onClick={() => serversExporter.exportServers()}
>
Export servers Export servers
</DropdownItem> </DropdownItem>
</React.Fragment> </React.Fragment>

View file

@ -1,10 +1,12 @@
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
export const serverType = PropTypes.shape({ export const serverType = PropTypes.shape({
id: PropTypes.string, id: PropTypes.string.isRequired,
name: PropTypes.string, name: PropTypes.string.isRequired,
url: PropTypes.string, url: PropTypes.string.isRequired,
apiKey: PropTypes.string, apiKey: PropTypes.string.isRequired,
version: PropTypes.string, version: PropTypes.string,
printableVersion: PropTypes.string, printableVersion: PropTypes.string,
serverNotFound: PropTypes.bool,
serverNotReachable: PropTypes.bool,
}); });

View file

@ -21,11 +21,22 @@ const versionToSemVer = pipe(
export const resetSelectedServer = createAction(RESET_SELECTED_SERVER); export const resetSelectedServer = createAction(RESET_SELECTED_SERVER);
export const selectServer = ({ findServerById }, buildShlinkApiClient) => (serverId) => async (dispatch) => { export const selectServer = ({ findServerById }, buildShlinkApiClient) => (serverId) => async (dispatch) => {
dispatch(resetSelectedServer());
dispatch(resetShortUrlParams()); dispatch(resetShortUrlParams());
const selectedServer = findServerById(serverId); const selectedServer = findServerById(serverId);
if (!selectedServer) {
dispatch({
type: SELECT_SERVER,
selectedServer: { serverNotFound: true },
});
return;
}
try {
const { health } = buildShlinkApiClient(selectedServer); const { health } = buildShlinkApiClient(selectedServer);
const { version } = await health().catch(() => MIN_FALLBACK_VERSION); const { version } = await health();
dispatch({ dispatch({
type: SELECT_SERVER, type: SELECT_SERVER,
@ -35,6 +46,12 @@ export const selectServer = ({ findServerById }, buildShlinkApiClient) => (serve
printableVersion: versionToPrintable(version), printableVersion: versionToPrintable(version),
}, },
}); });
} catch (e) {
dispatch({
type: SELECT_SERVER,
selectedServer: { serverNotReachable: true },
});
}
}; };
export default handleActions({ export default handleActions({

View file

@ -18,7 +18,7 @@ const provideServices = (bottle, connect, withRouter) => {
bottle.serviceFactory('ServersDropdown', ServersDropdown, 'ServersExporter'); bottle.serviceFactory('ServersDropdown', ServersDropdown, 'ServersExporter');
bottle.decorator('ServersDropdown', withRouter); bottle.decorator('ServersDropdown', withRouter);
bottle.decorator('ServersDropdown', connect([ 'servers', 'selectedServer' ], [ 'listServers', 'selectServer' ])); bottle.decorator('ServersDropdown', connect([ 'servers', 'selectedServer' ], [ 'listServers' ]));
bottle.serviceFactory('DeleteServerModal', () => DeleteServerModal); bottle.serviceFactory('DeleteServerModal', () => DeleteServerModal);
bottle.decorator('DeleteServerModal', withRouter); bottle.decorator('DeleteServerModal', withRouter);