mirror of
https://github.com/pavlobu/deskreen.git
synced 2025-05-20 01:10:27 -07:00
39 lines
890 B
TypeScript
39 lines
890 B
TypeScript
import { createSlice } from '@reduxjs/toolkit';
|
|
// eslint-disable-next-line import/no-cycle
|
|
import { AppThunk, RootState } from '../../store';
|
|
|
|
const counterSlice = createSlice({
|
|
name: 'counter',
|
|
initialState: { value: 0 },
|
|
reducers: {
|
|
increment: (state) => {
|
|
state.value += 1;
|
|
},
|
|
decrement: (state) => {
|
|
state.value -= 1;
|
|
},
|
|
},
|
|
});
|
|
|
|
export const { increment, decrement } = counterSlice.actions;
|
|
|
|
export const incrementIfOdd = (): AppThunk => {
|
|
return (dispatch, getState) => {
|
|
const state = getState();
|
|
if (state.counter.value % 2 === 0) {
|
|
return;
|
|
}
|
|
dispatch(increment());
|
|
};
|
|
};
|
|
|
|
export const incrementAsync = (delay = 1000): AppThunk => (dispatch) => {
|
|
setTimeout(() => {
|
|
dispatch(increment());
|
|
}, delay);
|
|
};
|
|
|
|
export default counterSlice.reducer;
|
|
|
|
export const selectCount = (state: RootState) => state.counter.value;
|