| 
									
										
										
										
											2017-10-23 16:50:48 -07:00
										 |  |  | import Component from '@ember/component'; | 
					
						
							|  |  |  | import { assert } from '@ember/debug'; | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  | import { StreamCommentActionsUnion } from 'wherehows-web/constants'; | 
					
						
							|  |  |  | import { StringUnionKeyToValue } from 'wherehows-web/typings/generic'; | 
					
						
							| 
									
										
										
										
											2018-06-25 23:42:28 -07:00
										 |  |  | import { noop } from 'wherehows-web/utils/helpers/functions'; | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Actions available for comment stream | 
					
						
							|  |  |  |  * @type {CommentActions} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const CommentActions: StringUnionKeyToValue<StreamCommentActionsUnion> = { | 
					
						
							|  |  |  |   create: 'create', | 
					
						
							|  |  |  |   update: 'update', | 
					
						
							|  |  |  |   destroy: 'destroy' | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default Component.extend({ | 
					
						
							|  |  |  |   tagName: 'ul', | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   classNames: ['comment-stream'], | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Mapping of available comment action | 
					
						
							|  |  |  |    * @type {StringUnionKeyToValue<StreamCommentActionsUnion>} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   commentActions: CommentActions, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-23 16:50:48 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Default no-op function to add a comment | 
					
						
							|  |  |  |    * @type {Function} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   addCommentToStream: noop, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Default no-op function to delete a comment | 
					
						
							|  |  |  |    * @type {Function} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   deleteCommentFromStream: noop, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Default no-op function to update a comment | 
					
						
							|  |  |  |    * @type {Function} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   updateCommentInStream: noop, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  |   actions: { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Async handles CrUD operations for comment stream actions, proxies to parent closure actions | 
					
						
							|  |  |  |      * @param {StreamCommentActionsUnion} strategy | 
					
						
							|  |  |  |      * @return {Promise<boolean>} | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     handleStreamComment(strategy: StreamCommentActionsUnion): Promise<boolean> { | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  |       const [, ...args] = arguments; | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // assert that handler is in CommentAction needed since we are calling from component template
 | 
					
						
							|  |  |  |       // TS currently has no jurisdiction there
 | 
					
						
							|  |  |  |       assert(`Expected action to be one of ${Object.keys(CommentActions)}`, strategy in CommentActions); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return { | 
					
						
							| 
									
										
										
										
											2017-10-23 16:50:48 -07:00
										 |  |  |         create: (): Promise<boolean> => this.addCommentToStream(...args), | 
					
						
							|  |  |  |         destroy: (): Promise<boolean> => this.deleteCommentFromStream(...args), | 
					
						
							|  |  |  |         update: (): Promise<boolean> => this.updateCommentInStream(...args) | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  |       }[strategy](); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }); |