@@ -3,39 +3,54 @@ import { ModuleBuilder } from "../../.."
3
3
import { BirthdayState , Birthday } from "./state"
4
4
import { Module } from "vuex"
5
5
import { RootState } from "../root"
6
+ import * as Vuex from "vuex"
7
+ import removeFirstAfter from "../../../../dist/tests/store/birthday/actions/removeFirstAfter"
6
8
7
9
const initialState : BirthdayState = {
8
10
birthdays : [
9
11
{
10
- name : "Jacob " ,
11
- dob : new Date ( 2006 , 10 , 11 )
12
+ name : "Richard " ,
13
+ dob : new Date ( 1995 , 10 , 11 )
12
14
} ,
13
15
{
14
- name : "Danny" ,
15
- dob : new Date ( 2009 , 11 , 30 )
16
+ name : "Erlich" ,
17
+ dob : new Date ( 1983 , 1 , 17 )
18
+ } ,
19
+ {
20
+ name : "Nelson" ,
21
+ dob : new Date ( 1996 , 3 , 28 )
22
+ } ,
23
+ {
24
+ name : "Dinesh" ,
25
+ dob : new Date ( 1989 , 1 , 7 )
26
+ } ,
27
+ {
28
+ name : "Bertram" ,
29
+ dob : new Date ( 1985 , 7 , 14 )
30
+ } ,
31
+ {
32
+ name : "Donald" ,
33
+ dob : new Date ( 1994 , 5 , 31 )
34
+ } ,
35
+ {
36
+ name : "Monica" ,
37
+ dob : new Date ( 1996 , 8 , 26 )
16
38
}
17
39
]
18
40
}
19
41
20
- const builder = new ModuleBuilder < BirthdayState , RootState > ( "birthday" , initialState )
42
+ const m = new ModuleBuilder < BirthdayState , RootState > ( "birthday" , initialState )
21
43
22
- const commitAddBirthday = builder . commit ( ( state , payload : { birthday : Birthday } ) =>
23
- {
24
- state . birthdays . push ( payload . birthday )
25
- } )
44
+ const addBirthdayMut = ( state : BirthdayState , payload : { birthday : Birthday } ) => state . birthdays . push ( payload . birthday )
45
+ const removeFirstBirthdayMut = ( state : BirthdayState ) => state . birthdays . shift ( )
26
46
27
- const commitRemoveFirstBirthday = builder . commit ( ( state ) =>
28
- {
29
- state . birthdays . shift ( )
30
- } )
31
-
32
- const getOldestName = builder . read ( ( state ) : Birthday | undefined =>
47
+ const oldestNameGetter = m . read ( ( state ) : Birthday | undefined =>
33
48
{
34
49
const sortedBirthdays = ( < Birthday [ ] > [ ] ) . sort ( ( a , b ) => a . dob . getTime ( ) - b . dob . getTime ( ) )
35
50
return sortedBirthdays [ 0 ]
36
- } )
51
+ } , "oldestName" )
37
52
38
- const getDOBforName = builder . read ( ( state ) => ( name : string ) =>
53
+ const dateOfBirthForMethod = m . read ( ( state ) => ( name : string ) =>
39
54
{
40
55
const matches = state . birthdays . filter ( b => b . name === name )
41
56
if ( matches . length )
@@ -46,33 +61,23 @@ const getDOBforName = builder.read((state) => (name: string) =>
46
61
return
47
62
} , "dob" )
48
63
49
- const dispatchRemoveFirstAfter = builder . dispatch ( async ( context , delay : number ) =>
50
- {
51
- if ( context . state . birthdays . length > 2 )
52
- {
53
- await new Promise ( ( resolve , _ ) => setTimeout ( resolve , 1000 ) ) ; // second delay
54
- commitRemoveFirstBirthday ( )
55
- }
56
- } )
57
-
58
- const module = {
64
+ const birthday = {
59
65
// getters + methods
60
- get oldestName ( ) { return getOldestName ( ) } ,
61
- dateOfBirthFor ( name : string ) { return getDOBforName ( ) ( name ) } ,
66
+ get oldestName ( ) { return oldestNameGetter ( ) } ,
67
+ dateOfBirthFor ( name : string ) { return dateOfBirthForMethod ( ) ( name ) } ,
62
68
63
69
// mutations
64
- commitAddBirthday,
65
- commitRemoveFirstBirthday,
70
+ commitAddBirthday : m . commit ( addBirthdayMut ) ,
71
+ commitRemoveFirstBirthday : m . commit ( removeFirstBirthdayMut ) ,
66
72
67
73
// actions
68
- dispatchRemoveFirstAfter,
74
+ dispatchRemoveFirstAfter : m . dispatch ( removeFirstAfter )
69
75
}
70
76
71
- module . commitAddBirthday ( { birthday : { dob : new Date ( 1980 , 2 , 3 ) , name : "Louise" } } )
72
- module . commitRemoveFirstBirthday ( )
73
- module . dateOfBirthFor ( "Louise" )
74
- module . dispatchRemoveFirstAfter ( 1000 )
75
-
76
- export default module
77
+ // birthday.commitAddBirthday({ birthday: { dob: new Date(1980, 2, 3), name: "Louise" } })
78
+ // birthday.commitRemoveFirstBirthday()
79
+ // birthday.dateOfBirthFor("Louise")
80
+ // birthday.dispatchRemoveFirstAfter(1000)
77
81
78
- export const vuexModule : Module < BirthdayState , RootState > = builder . toVuexModule ( )
82
+ export default birthday
83
+ export const birthdayModule : Module < BirthdayState , RootState > = m . toVuexModule ( )
0 commit comments