-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.ts
36 lines (29 loc) · 1.13 KB
/
example.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { getActionCreator, getReducerBuilder } from "./src/getters";
import { TDynamicExportedActionCreator } from "./types/types";
const initialState = {
counter: 0,
canGoBelowZero: false
}
const actionBuilder = getActionCreator('COUNTER')
const actions = actionBuilder.getConstants([
'DECREACE',
'TO_ZERO'
])
export const increase = actionBuilder.build('INCREASE', (amount: number = 1) => ({ amount }))
export const decrease: TDynamicExportedActionCreator<number, { amount: number }> = actionBuilder.buildDynamic((amount: number = 1) => ({
type: actions.DECREACE,
payload: { amount }
}))
export const toZero = actionBuilder.buildDynamic(() => ({
type: actions.TO_ZERO
}))
export const reducer = getReducerBuilder(initialState)
.copyState()
.handle(increase, (s, a) => ({ counter: s.counter + a.payload.amount }))
.handleType<{ amount: number }>( // Unnecessary to specify type
actions.DECREACE, (s, a) => ({
counter: (s.canGoBelowZero || a.payload.amount < s.counter) ? s.counter - a.payload.amount : 0
})
)
.handleType(actions.TO_ZERO, () => ({ counter: 0 }))
.build()