1
- import { makeObservable , observable , action , computed } from "mobx"
1
+ import { makeObservable , observable , action , computed , configure , runInAction } from "mobx"
2
2
import { createContext , SyntheticEvent } from 'react'
3
3
4
4
import { IActivity } from "../models/activity"
5
5
import agent from '../api/agent'
6
6
7
+ configure ( {
8
+ enforceActions : 'always'
9
+ } )
10
+
7
11
class ActivityStore {
8
12
9
13
constructor ( ) {
@@ -27,27 +31,38 @@ class ActivityStore {
27
31
try {
28
32
const activityList = await agent . Activities . list ( )
29
33
30
- activityList . forEach ( ( activity ) => {
31
- activity . date = activity . date . split ( '.' ) [ 0 ]
32
- this . activityRegistry . set ( activity . id , activity )
34
+ runInAction ( ( ) => {
35
+ activityList . forEach ( ( activity ) => {
36
+ activity . date = activity . date . split ( '.' ) [ 0 ]
37
+ this . activityRegistry . set ( activity . id , activity )
38
+ } )
39
+
40
+ this . loadingInitial = false
33
41
} )
34
42
35
- this . loadingInitial = false
36
43
} catch ( error ) {
44
+ runInAction ( ( ) => {
45
+ this . loadingInitial = false
46
+ } )
37
47
console . log ( error )
38
- this . loadingInitial = false
39
48
}
40
49
}
41
50
42
51
@action createActivity = async ( activity : IActivity ) => {
43
52
this . submitting = true
44
53
try {
45
54
await agent . Activities . create ( activity )
46
- this . activityRegistry . set ( activity . id , activity )
47
- this . editMode = false
48
- this . submitting = false
55
+
56
+ runInAction ( ( ) => {
57
+ this . activityRegistry . set ( activity . id , activity )
58
+ this . editMode = false
59
+ this . submitting = false
60
+ } )
61
+
49
62
} catch ( error ) {
50
- this . submitting = false
63
+ runInAction ( ( ) => {
64
+ this . submitting = false
65
+ } )
51
66
console . log ( error )
52
67
}
53
68
}
@@ -56,12 +71,18 @@ class ActivityStore {
56
71
this . submitting = true
57
72
try {
58
73
await agent . Activities . update ( activity )
59
- this . activityRegistry . set ( activity . id , activity )
60
- this . selectedActivity = activity
61
- this . editMode = false
62
- this . submitting = false
74
+
75
+ runInAction ( ( ) => {
76
+ this . activityRegistry . set ( activity . id , activity )
77
+ this . selectedActivity = activity
78
+ this . editMode = false
79
+ this . submitting = false
80
+ } )
81
+
63
82
} catch ( error ) {
64
- this . submitting = false
83
+ runInAction ( ( ) => {
84
+ this . submitting = false
85
+ } )
65
86
console . log ( error )
66
87
}
67
88
}
@@ -71,13 +92,18 @@ class ActivityStore {
71
92
this . target = event . currentTarget . name
72
93
try {
73
94
await agent . Activities . delete ( id )
74
- this . activityRegistry . delete ( id )
75
- this . submitting = false
76
- this . target = ''
77
95
96
+ runInAction ( ( ) => {
97
+ this . activityRegistry . delete ( id )
98
+ this . submitting = false
99
+ this . target = ''
100
+ } )
78
101
} catch ( error ) {
79
- this . submitting = false
80
- this . target = ''
102
+ runInAction ( ( ) => {
103
+ this . submitting = false
104
+ this . target = ''
105
+ } )
106
+
81
107
console . log ( error )
82
108
}
83
109
}
0 commit comments