diff --git a/src/servers/ServersDropdown.js b/src/servers/ServersDropdown.js index 482a7c5f..5f59d204 100644 --- a/src/servers/ServersDropdown.js +++ b/src/servers/ServersDropdown.js @@ -5,10 +5,11 @@ import { Link } from 'react-router-dom'; import { DropdownItem, DropdownMenu, DropdownToggle, UncontrolledDropdown } from 'reactstrap'; import { listServers } from './reducers/server'; +import { selectServer } from '../servers/reducers/selectedServer'; export class ServersDropdown extends React.Component { renderServers = () => { - const { servers, selectedServer } = this.props; + const { servers, selectedServer, selectServer } = this.props; if (isEmpty(servers)) { return Add a server first... @@ -20,6 +21,7 @@ export class ServersDropdown extends React.Component { tag={Link} to={`/server/${id}/list-short-urls/1`} active={selectedServer && selectedServer.id === id} + onClick={() => selectServer(id)} // FIXME This should be implicit > {name} @@ -41,4 +43,4 @@ export class ServersDropdown extends React.Component { } } -export default connect(pick(['servers', 'selectedServer']), { listServers })(ServersDropdown); +export default connect(pick(['servers', 'selectedServer']), { listServers, selectServer })(ServersDropdown); diff --git a/src/servers/reducers/selectedServer.js b/src/servers/reducers/selectedServer.js index 57b61de4..18833b34 100644 --- a/src/servers/reducers/selectedServer.js +++ b/src/servers/reducers/selectedServer.js @@ -1,15 +1,18 @@ import ShlinkApiClient from '../../api/ShlinkApiClient'; import ServersService from '../../servers/services/ServersService'; +import { resetShortUrlParams } from '../../short-urls/reducers/shortUrlsListParams' const SELECT_SERVER = 'shlink/selectedServer/SELECT_SERVER'; const RESET_SELECTED_SERVER = 'shlink/selectedServer/RESET_SELECTED_SERVER'; -export default function reducer(state = null, action) { +const defaultState = null; + +export default function reducer(state = defaultState, action) { switch (action.type) { case SELECT_SERVER: return action.selectedServer; case RESET_SELECTED_SERVER: - return null; + return defaultState; default: return state; } @@ -17,12 +20,14 @@ export default function reducer(state = null, action) { export const resetSelectedServer = () => ({ type: RESET_SELECTED_SERVER }); -export const selectServer = serverId => { +export const selectServer = serverId => dispatch => { + dispatch(resetShortUrlParams()); + const selectedServer = ServersService.findServerById(serverId); ShlinkApiClient.setConfig(selectedServer); - return { + dispatch({ type: SELECT_SERVER, selectedServer - } + }) }; diff --git a/src/short-urls/reducers/shortUrlsListParams.js b/src/short-urls/reducers/shortUrlsListParams.js index 4d465e06..c99bb38b 100644 --- a/src/short-urls/reducers/shortUrlsListParams.js +++ b/src/short-urls/reducers/shortUrlsListParams.js @@ -1,10 +1,18 @@ import { LIST_SHORT_URLS } from './shortUrlsList'; -export default function reducer(state = { page: 1 }, action) { +const RESET_SHORT_URL_PARAMS = 'shlink/shortUrlsListParams/RESET_SHORT_URL_PARAMS'; + +const defaultState = { page: 1 }; + +export default function reducer(state = defaultState, action) { switch (action.type) { case LIST_SHORT_URLS: return { ...state, ...action.params }; + case RESET_SHORT_URL_PARAMS: + return defaultState; default: return state; } } + +export const resetShortUrlParams = () => ({ type: RESET_SHORT_URL_PARAMS });