| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  | import Component from '@ember/component'; | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | import { set } from '@ember/object'; | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  | import { assert } from '@ember/debug'; | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   readDatasetSchemaComments, | 
					
						
							|  |  |  |   createDatasetSchemaComment, | 
					
						
							|  |  |  |   updateDatasetSchemaComment, | 
					
						
							|  |  |  |   deleteDatasetSchemaComment | 
					
						
							|  |  |  | } from 'wherehows-web/utils/api/datasets/schema-comments'; | 
					
						
							|  |  |  | import { augmentObjectsWithHtmlComments } from 'wherehows-web/utils/api/datasets/columns'; | 
					
						
							|  |  |  | import { IDatasetComment } from 'wherehows-web/typings/api/datasets/comments'; | 
					
						
							|  |  |  | import { IDatasetColumn } from 'wherehows-web/typings/api/datasets/columns'; | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | import Notifications from '@datahub/utils/services/notifications'; | 
					
						
							|  |  |  | import { NotificationEvent } from '@datahub/utils/constants/notifications'; | 
					
						
							|  |  |  | import { action } from '@ember/object'; | 
					
						
							|  |  |  | import { inject as service } from '@ember/service'; | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  | import { TaskInstance, task } from 'ember-concurrency'; | 
					
						
							|  |  |  | import { ETask } from '@datahub/utils/types/concurrency'; | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | enum SchemaCommentActions { | 
					
						
							|  |  |  |   modify = 'modify', | 
					
						
							|  |  |  |   destroy = 'destroy', | 
					
						
							|  |  |  |   add = 'add' | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | interface IGetCommentsTaskArgs { | 
					
						
							|  |  |  |   datasetId: number; | 
					
						
							|  |  |  |   columnId: number; | 
					
						
							| 
									
										
										
										
											2018-01-20 00:46:47 -08:00
										 |  |  |   comments: Array<IDatasetComment>; | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class SchemaComment extends Component { | 
					
						
							| 
									
										
										
										
											2018-01-20 00:46:47 -08:00
										 |  |  |   comments: Array<IDatasetComment> = []; | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  |   count = 0; | 
					
						
							|  |  |  |   datasetId = 0; | 
					
						
							|  |  |  |   columnId = 0; | 
					
						
							|  |  |  |   isShowingFieldComment = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							| 
									
										
										
										
											2018-01-20 00:46:47 -08:00
										 |  |  |    * Local reference to the notifications service | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  |    * @memberof SchemaComment | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2018-08-12 13:02:01 -07:00
										 |  |  |   @service | 
					
						
							|  |  |  |   notifications: Notifications; | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-20 00:46:47 -08:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Enum of applicable values for schema comment actions | 
					
						
							|  |  |  |    * @type {SchemaCommentActions} | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  |   SchemaCommentActions = SchemaCommentActions; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-20 00:46:47 -08:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Task to get related schema comments | 
					
						
							|  |  |  |    * TODO: refactor move to container component | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   @task(function*({ | 
					
						
							|  |  |  |     datasetId, | 
					
						
							|  |  |  |     columnId, | 
					
						
							|  |  |  |     comments | 
					
						
							|  |  |  |   }: IGetCommentsTaskArgs): IterableIterator<Promise<Array<IDatasetComment>>> { | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  |     const schemaComments: Array<IDatasetComment> = yield readDatasetSchemaComments(datasetId, columnId); | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (Array.isArray(schemaComments)) { | 
					
						
							|  |  |  |       const withHtmlComments = augmentObjectsWithHtmlComments(schemaComments.map( | 
					
						
							|  |  |  |         ({ text }): Partial<IDatasetColumn> => ({ | 
					
						
							|  |  |  |           comment: text | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       ) as Array<IDatasetColumn>); | 
					
						
							|  |  |  |       comments.setObjects.call(comments, withHtmlComments); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  |   getCommentsTask!: ETask<Promise<Array<IDatasetComment>>, IGetCommentsTaskArgs>; | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-12 13:02:01 -07:00
										 |  |  |   @action | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   showComments(): TaskInstance<Promise<Array<IDatasetComment>>> { | 
					
						
							|  |  |  |     const { datasetId, columnId, comments } = this; | 
					
						
							| 
									
										
										
										
											2018-08-12 13:02:01 -07:00
										 |  |  |     set(this, 'isShowingFieldComment', true); | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     return this.getCommentsTask.perform({ datasetId, columnId, comments }); | 
					
						
							| 
									
										
										
										
											2018-08-12 13:02:01 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-12 13:02:01 -07:00
										 |  |  |   @action | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   hideComments(): boolean { | 
					
						
							| 
									
										
										
										
											2018-08-12 13:02:01 -07:00
										 |  |  |     return set(this, 'isShowingFieldComment', false); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-12 13:02:01 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Given a schema comment action, invokes the related action to process the schema comment | 
					
						
							|  |  |  |    * @param {SchemaCommentActions} strategy | 
					
						
							|  |  |  |    * @return {Promise<boolean>} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   @action | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   async handleSchemaComment(strategy: SchemaCommentActions): Promise<boolean> { | 
					
						
							| 
									
										
										
										
											2018-08-12 13:02:01 -07:00
										 |  |  |     const [, { text }] = arguments; | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     const { datasetId, columnId, notifications, comments, getCommentsTask } = this; | 
					
						
							| 
									
										
										
										
											2018-08-12 13:02:01 -07:00
										 |  |  |     const { notify } = notifications; | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-12 13:02:01 -07:00
										 |  |  |     assert(`Expected action to be one of ${Object.keys(SchemaCommentActions)}`, strategy in SchemaCommentActions); | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-12 13:02:01 -07:00
										 |  |  |     const action = { | 
					
						
							|  |  |  |       add: (): Promise<void> => createDatasetSchemaComment(datasetId, columnId, text), | 
					
						
							|  |  |  |       modify: (): Promise<void> => updateDatasetSchemaComment(datasetId, columnId, text), | 
					
						
							|  |  |  |       destroy: (): Promise<void> => deleteDatasetSchemaComment(datasetId, columnId) | 
					
						
							|  |  |  |     }[strategy]; | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-12 13:02:01 -07:00
										 |  |  |     try { | 
					
						
							|  |  |  |       await action(); | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |       notify({ type: NotificationEvent.success, content: 'Success!' }); | 
					
						
							| 
									
										
										
										
											2018-08-12 13:02:01 -07:00
										 |  |  |       // @ts-ignore ts limitation with the ember object model, fixed in ember 3.1 with es5 getters
 | 
					
						
							|  |  |  |       getCommentsTask.perform({ datasetId, columnId, comments }); | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |       notify({ type: NotificationEvent.error, content: e.message }); | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-12 13:02:01 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-10-18 17:38:51 -07:00
										 |  |  | } |