Migrated sidebar reducer to RTK

This commit is contained in:
Alejandro Celaya 2022-11-04 19:45:03 +01:00
parent 85e2aab4df
commit f209fa2d58
3 changed files with 18 additions and 28 deletions

View file

@ -1,25 +1,22 @@
import { Action } from 'redux'; import { createSlice } from '@reduxjs/toolkit';
import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
export const SIDEBAR_PRESENT = 'shlink/common/SIDEBAR_PRESENT';
export const SIDEBAR_NOT_PRESENT = 'shlink/common/SIDEBAR_NOT_PRESENT';
export interface Sidebar { export interface Sidebar {
sidebarPresent: boolean; sidebarPresent: boolean;
} }
type SidebarRenderedAction = Action<string>;
type SidebarNotRenderedAction = Action<string>;
const initialState: Sidebar = { const initialState: Sidebar = {
sidebarPresent: false, sidebarPresent: false,
}; };
export default buildReducer<Sidebar, SidebarRenderedAction & SidebarNotRenderedAction>({ const { actions, reducer } = createSlice({
[SIDEBAR_PRESENT]: () => ({ sidebarPresent: true }), name: 'sidebarReducer',
[SIDEBAR_NOT_PRESENT]: () => ({ sidebarPresent: false }), initialState,
}, initialState); reducers: {
sidebarPresent: () => ({ sidebarPresent: true }),
sidebarNotPresent: () => ({ sidebarPresent: false }),
},
});
export const sidebarPresent = buildActionCreator(SIDEBAR_PRESENT); export const { sidebarPresent, sidebarNotPresent } = actions;
export const sidebarNotPresent = buildActionCreator(SIDEBAR_NOT_PRESENT); export const sidebarReducer = reducer;

View file

@ -19,7 +19,7 @@ import mercureInfoReducer from '../mercure/reducers/mercureInfo';
import settingsReducer from '../settings/reducers/settings'; import settingsReducer from '../settings/reducers/settings';
import visitsOverviewReducer from '../visits/reducers/visitsOverview'; import visitsOverviewReducer from '../visits/reducers/visitsOverview';
import { appUpdatesReducer } from '../app/reducers/appUpdates'; import { appUpdatesReducer } from '../app/reducers/appUpdates';
import sidebarReducer from '../common/reducers/sidebar'; import { sidebarReducer } from '../common/reducers/sidebar';
import { ShlinkState } from '../container/types'; import { ShlinkState } from '../container/types';
export default (container: IContainer) => combineReducers<ShlinkState>({ export default (container: IContainer) => combineReducers<ShlinkState>({

View file

@ -1,31 +1,24 @@
import { Mock } from 'ts-mockery'; import { sidebarReducer, sidebarNotPresent, sidebarPresent } from '../../../src/common/reducers/sidebar';
import reducer, {
Sidebar,
SIDEBAR_NOT_PRESENT,
SIDEBAR_PRESENT,
sidebarNotPresent,
sidebarPresent,
} from '../../../src/common/reducers/sidebar';
describe('sidebarReducer', () => { describe('sidebarReducer', () => {
describe('reducer', () => { describe('reducer', () => {
it.each([ it.each([
[SIDEBAR_PRESENT, { sidebarPresent: true }], [sidebarPresent.toString(), { sidebarPresent: true }],
[SIDEBAR_NOT_PRESENT, { sidebarPresent: false }], [sidebarNotPresent.toString(), { sidebarPresent: false }],
])('returns expected on %s', (type, expected) => { ])('returns expected on %s', (type, expected) => {
expect(reducer(Mock.all<Sidebar>(), { type })).toEqual(expected); expect(sidebarReducer(undefined, { type })).toEqual(expected);
}); });
}); });
describe('sidebarPresent', () => { describe('sidebarPresent', () => {
it('returns expected action', () => { it('returns expected action', () => {
expect(sidebarPresent()).toEqual({ type: SIDEBAR_PRESENT }); expect(sidebarPresent()).toEqual({ type: sidebarPresent.toString() });
}); });
}); });
describe('sidebarNotPresent', () => { describe('sidebarNotPresent', () => {
it('returns expected action', () => { it('returns expected action', () => {
expect(sidebarNotPresent()).toEqual({ type: SIDEBAR_NOT_PRESENT }); expect(sidebarNotPresent()).toEqual({ type: sidebarNotPresent.toString() });
}); });
}); });
}); });