Migrated selectedServer reducer to use payload actions

This commit is contained in:
Alejandro Celaya 2022-11-05 10:36:57 +01:00
parent 10d4419387
commit a31cdcc9f0
2 changed files with 9 additions and 10 deletions

View file

@ -1,3 +1,4 @@
import { PayloadAction } from '@reduxjs/toolkit';
import { identity, memoizeWith, pipe } from 'ramda'; import { identity, memoizeWith, pipe } from 'ramda';
import { Action, Dispatch } from 'redux'; import { Action, Dispatch } from 'redux';
import { versionToPrintable, versionToSemVer as toSemVer } from '../../utils/helpers/version'; import { versionToPrintable, versionToSemVer as toSemVer } from '../../utils/helpers/version';
@ -14,9 +15,7 @@ export const MIN_FALLBACK_VERSION = '1.0.0';
export const MAX_FALLBACK_VERSION = '999.999.999'; export const MAX_FALLBACK_VERSION = '999.999.999';
export const LATEST_VERSION_CONSTRAINT = 'latest'; export const LATEST_VERSION_CONSTRAINT = 'latest';
export interface SelectServerAction extends Action<string> { export type SelectServerAction = PayloadAction<SelectedServer>;
selectedServer: SelectedServer;
}
const versionToSemVer = pipe( const versionToSemVer = pipe(
(version: string) => (version === LATEST_VERSION_CONSTRAINT ? MAX_FALLBACK_VERSION : version), (version: string) => (version === LATEST_VERSION_CONSTRAINT ? MAX_FALLBACK_VERSION : version),
@ -35,7 +34,7 @@ const initialState: SelectedServer = null;
export default buildReducer<SelectedServer, SelectServerAction>({ export default buildReducer<SelectedServer, SelectServerAction>({
[RESET_SELECTED_SERVER]: () => initialState, [RESET_SELECTED_SERVER]: () => initialState,
[SELECT_SERVER]: (_, { selectedServer }) => selectedServer, [SELECT_SERVER]: (_, { payload }) => payload,
}, initialState); }, initialState);
export const resetSelectedServer = buildActionCreator(RESET_SELECTED_SERVER); export const resetSelectedServer = buildActionCreator(RESET_SELECTED_SERVER);
@ -57,7 +56,7 @@ export const selectServer = (
if (!selectedServer) { if (!selectedServer) {
dispatch<SelectServerAction>({ dispatch<SelectServerAction>({
type: SELECT_SERVER, type: SELECT_SERVER,
selectedServer: { serverNotFound: true }, payload: { serverNotFound: true },
}); });
return; return;
@ -69,7 +68,7 @@ export const selectServer = (
dispatch<SelectServerAction>({ dispatch<SelectServerAction>({
type: SELECT_SERVER, type: SELECT_SERVER,
selectedServer: { payload: {
...selectedServer, ...selectedServer,
version, version,
printableVersion, printableVersion,
@ -79,7 +78,7 @@ export const selectServer = (
} catch (e) { } catch (e) {
dispatch<SelectServerAction>({ dispatch<SelectServerAction>({
type: SELECT_SERVER, type: SELECT_SERVER,
selectedServer: { ...selectedServer, serverNotReachable: true }, payload: { ...selectedServer, serverNotReachable: true },
}); });
} }
}; };

View file

@ -14,12 +14,12 @@ import { NonReachableServer, NotFoundServer, RegularServer } from '../../../src/
describe('selectedServerReducer', () => { describe('selectedServerReducer', () => {
describe('reducer', () => { describe('reducer', () => {
it('returns default when action is RESET_SELECTED_SERVER', () => it('returns default when action is RESET_SELECTED_SERVER', () =>
expect(reducer(null, { type: RESET_SELECTED_SERVER, selectedServer: null })).toBeNull()); expect(reducer(null, { type: RESET_SELECTED_SERVER, payload: null })).toBeNull());
it('returns selected server when action is SELECT_SERVER', () => { it('returns selected server when action is SELECT_SERVER', () => {
const selectedServer = Mock.of<RegularServer>({ id: 'abc123' }); const payload = Mock.of<RegularServer>({ id: 'abc123' });
expect(reducer(null, { type: SELECT_SERVER, selectedServer })).toEqual(selectedServer); expect(reducer(null, { type: SELECT_SERVER, payload })).toEqual(payload);
}); });
}); });