mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 09:30:31 +03:00
Handled loading server in just one place, and added error handling for loading servers
This commit is contained in:
parent
f4cc8d3a0c
commit
c8d682cc98
5 changed files with 53 additions and 31 deletions
|
@ -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);
|
||||||
|
const { params: { serverId } } = match;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const { params: { serverId } } = match;
|
|
||||||
|
|
||||||
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>"{serverId}"</b> in this host.</MutedMessage>;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (selectedServer.serverNotReachable) {
|
||||||
|
return (
|
||||||
|
<MutedMessage>
|
||||||
|
Oops! Could not connect to Shlink server with ID <b>"{serverId}"</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,
|
||||||
});
|
});
|
||||||
|
|
|
@ -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>
|
||||||
|
|
|
@ -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,
|
||||||
});
|
});
|
||||||
|
|
|
@ -21,20 +21,37 @@ 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);
|
||||||
const { health } = buildShlinkApiClient(selectedServer);
|
|
||||||
const { version } = await health().catch(() => MIN_FALLBACK_VERSION);
|
|
||||||
|
|
||||||
dispatch({
|
if (!selectedServer) {
|
||||||
type: SELECT_SERVER,
|
dispatch({
|
||||||
selectedServer: {
|
type: SELECT_SERVER,
|
||||||
...selectedServer,
|
selectedServer: { serverNotFound: true },
|
||||||
version: versionToSemVer(version),
|
});
|
||||||
printableVersion: versionToPrintable(version),
|
|
||||||
},
|
return;
|
||||||
});
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { health } = buildShlinkApiClient(selectedServer);
|
||||||
|
const { version } = await health();
|
||||||
|
|
||||||
|
dispatch({
|
||||||
|
type: SELECT_SERVER,
|
||||||
|
selectedServer: {
|
||||||
|
...selectedServer,
|
||||||
|
version: versionToSemVer(version),
|
||||||
|
printableVersion: versionToPrintable(version),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
dispatch({
|
||||||
|
type: SELECT_SERVER,
|
||||||
|
selectedServer: { serverNotReachable: true },
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default handleActions({
|
export default handleActions({
|
||||||
|
|
|
@ -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);
|
||||||
|
|
Loading…
Reference in a new issue