File tree 4 files changed +78
-4
lines changed
4 files changed +78
-4
lines changed Original file line number Diff line number Diff line change @@ -51,7 +51,7 @@ export type EventsAction =
51
51
| IUnresolveHelpRequest
52
52
| IUpdateHelpRequest ;
53
53
54
- export function setEvent ( event : Event ) : ISetEvent {
54
+ export function setEvent ( event : Event | null ) : ISetEvent {
55
55
return {
56
56
type : 'SET_EVENT' ,
57
57
event,
Original file line number Diff line number Diff line change @@ -106,6 +106,34 @@ export default ({ event, onComplete }: Props) => {
106
106
}
107
107
} ;
108
108
109
+ const onDelete = async ( ) => {
110
+ if ( ! confirm ( 'Are you sure?' ) || ! event ) {
111
+ return ;
112
+ }
113
+
114
+ setLoading ( true ) ;
115
+
116
+ try {
117
+ const res = await post ( `/api/events/delete` , {
118
+ eventId : event . _id ,
119
+ } ) ;
120
+
121
+ if ( ! res . ok ) {
122
+ const json = await res . json ( ) ;
123
+ throw new Error ( json . message ) ;
124
+ }
125
+ } catch ( e ) {
126
+ setLoading ( false ) ;
127
+ return setErrors ( {
128
+ ...errors ,
129
+ general : e . message ,
130
+ } ) ;
131
+ }
132
+
133
+ setLoading ( false ) ;
134
+ dispatch ( setEvent ( null ) ) ;
135
+ } ;
136
+
109
137
const clearErrors = ( keys : Array < keyof typeof defaultErrors > ) => {
110
138
const updatedErrors = { ...errors } ;
111
139
keys . forEach ( key => ( updatedErrors [ key ] = '' ) ) ;
@@ -159,8 +187,21 @@ export default ({ event, onComplete }: Props) => {
159
187
{ errors . endDate && < Alert variant = "danger" > { errors . endDate } </ Alert > }
160
188
</ Form . Group >
161
189
< div className = "controls" >
190
+ { event ? (
191
+ < Button
192
+ disabled = { loading }
193
+ onClick = { ( e : React . FormEvent ) => {
194
+ e . preventDefault ( ) ;
195
+ onDelete ( ) ;
196
+ } }
197
+ variant = "danger"
198
+ >
199
+ Delete
200
+ </ Button >
201
+ ) : (
202
+ < div />
203
+ ) }
162
204
< Button
163
- type = "submit"
164
205
disabled = { loading }
165
206
onClick = { ( e : React . FormEvent ) => {
166
207
e . preventDefault ( ) ;
@@ -182,7 +223,8 @@ export default ({ event, onComplete }: Props) => {
182
223
margin-top: 10px;
183
224
}
184
225
.controls {
185
- text-align: right;
226
+ display: flex;
227
+ justify-content: space-between;
186
228
}
187
229
` } </ style >
188
230
</ div >
Original file line number Diff line number Diff line change @@ -58,7 +58,7 @@ export default () => {
58
58
< Form . Label > Password</ Form . Label >
59
59
< Form . Control
60
60
type = "password"
61
- placeholder = "Hope you remember it! "
61
+ placeholder = "Enter your password "
62
62
required
63
63
value = { password }
64
64
onChange = { ( e : InputEvent ) => {
Original file line number Diff line number Diff line change @@ -242,4 +242,36 @@ router.post(
242
242
} ) ,
243
243
) ;
244
244
245
+ router . post (
246
+ '/api/events/delete' ,
247
+ requiresAuth ( { error : true } ) ,
248
+ asyncHandler ( async ( req : ReqWithUser , res : express . Response ) => {
249
+ const { eventId } = req . body ;
250
+ const db : typeof mongoose = req . app . locals . db ;
251
+
252
+ const event = await Event . findById ( eventId ) ;
253
+
254
+ if ( ! event || String ( event . owner ) !== String ( req . user . _id ) ) {
255
+ throw new NotFoundError ( 'Event not found' ) ;
256
+ }
257
+
258
+ const session = await db . startSession ( ) ;
259
+ session . startTransaction ( ) ;
260
+
261
+ const q = Event . deleteOne ( { _id : event . _id } ) ;
262
+ q . setOptions ( { session } ) ;
263
+ await q . exec ( ) ;
264
+
265
+ await User . updateOne (
266
+ { _id : req . user . _id } ,
267
+ { $set : { events : [ ] } } ,
268
+ { session } ,
269
+ ) . exec ( ) ;
270
+
271
+ await session . commitTransaction ( ) ;
272
+
273
+ res . json ( { success : true } ) ;
274
+ } ) ,
275
+ ) ;
276
+
245
277
export default router ;
You can’t perform that action at this time.
0 commit comments