mirror of
				https://github.com/datahub-project/datahub.git
				synced 2025-10-30 18:26:58 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			114 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			114 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import Component from '@ember/component';
 | |
| import { getProperties, get, set } from '@ember/object';
 | |
| import { assert } from '@ember/debug';
 | |
| import { task } from 'ember-concurrency';
 | |
| 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';
 | |
| import Notifications, { NotificationEvent } from 'wherehows-web/services/notifications';
 | |
| import { action } from '@ember-decorators/object';
 | |
| import { service } from '@ember-decorators/service';
 | |
| 
 | |
| enum SchemaCommentActions {
 | |
|   modify = 'modify',
 | |
|   destroy = 'destroy',
 | |
|   add = 'add'
 | |
| }
 | |
| 
 | |
| interface IGetCommentsTaskArgs {
 | |
|   datasetId: number;
 | |
|   columnId: number;
 | |
|   comments: Array<IDatasetComment>;
 | |
| }
 | |
| 
 | |
| export class SchemaComment extends Component {
 | |
|   comments: Array<IDatasetComment> = [];
 | |
|   count = 0;
 | |
|   datasetId = 0;
 | |
|   columnId = 0;
 | |
|   isShowingFieldComment = false;
 | |
| 
 | |
|   /**
 | |
|    * Local reference to the notifications service
 | |
|    * @memberof SchemaComment
 | |
|    */
 | |
|   @service
 | |
|   notifications: Notifications;
 | |
| 
 | |
|   /**
 | |
|    * Enum of applicable values for schema comment actions
 | |
|    * @type {SchemaCommentActions}
 | |
|    */
 | |
|   SchemaCommentActions = SchemaCommentActions;
 | |
| 
 | |
|   /**
 | |
|    * Task to get related schema comments
 | |
|    * TODO: refactor move to container component
 | |
|    * @type {"ember-concurrency".Task<Promise<Array<IDatasetComment>>, (a?: IGetCommentsTaskArgs) => "ember-concurrency".TaskInstance<Promise<Array<IDatasetComment>>>>}
 | |
|    */
 | |
|   getCommentsTask = task(function*({ datasetId, columnId, comments }: IGetCommentsTaskArgs) {
 | |
|     const schemaComments: Array<IDatasetComment> = yield readDatasetSchemaComments(datasetId, columnId);
 | |
|     const withHtmlComments = augmentObjectsWithHtmlComments(
 | |
|       schemaComments.map(({ text }) => <IDatasetColumn>{ comment: text })
 | |
|     );
 | |
|     comments.setObjects.call(comments, withHtmlComments);
 | |
|   }).drop();
 | |
| 
 | |
|   @action
 | |
|   showComments() {
 | |
|     const props = getProperties(this, ['datasetId', 'columnId', 'comments']);
 | |
|     set(this, 'isShowingFieldComment', true);
 | |
| 
 | |
|     // @ts-ignore ts limitation with the ember object model, fixed in ember 3.1 with es5 getters
 | |
|     return get(this, 'getComments').perform(props);
 | |
|   }
 | |
| 
 | |
|   @action
 | |
|   hideComments() {
 | |
|     return set(this, 'isShowingFieldComment', false);
 | |
|   }
 | |
| 
 | |
|   /**
 | |
|    * Given a schema comment action, invokes the related action to process the schema comment
 | |
|    * @param {SchemaCommentActions} strategy
 | |
|    * @return {Promise<boolean>}
 | |
|    */
 | |
|   @action
 | |
|   async handleSchemaComment(strategy: SchemaCommentActions) {
 | |
|     const [, { text }] = arguments;
 | |
|     const { datasetId, columnId, notifications, comments, getCommentsTask } = getProperties(this, [
 | |
|       'datasetId',
 | |
|       'columnId',
 | |
|       'notifications',
 | |
|       'comments',
 | |
|       'getCommentsTask'
 | |
|     ]);
 | |
|     const { notify } = notifications;
 | |
| 
 | |
|     assert(`Expected action to be one of ${Object.keys(SchemaCommentActions)}`, strategy in SchemaCommentActions);
 | |
| 
 | |
|     const action = {
 | |
|       add: (): Promise<void> => createDatasetSchemaComment(datasetId, columnId, text),
 | |
|       modify: (): Promise<void> => updateDatasetSchemaComment(datasetId, columnId, text),
 | |
|       destroy: (): Promise<void> => deleteDatasetSchemaComment(datasetId, columnId)
 | |
|     }[strategy];
 | |
| 
 | |
|     try {
 | |
|       await action();
 | |
|       notify(NotificationEvent.success, { content: 'Success!' });
 | |
|       // @ts-ignore ts limitation with the ember object model, fixed in ember 3.1 with es5 getters
 | |
|       getCommentsTask.perform({ datasetId, columnId, comments });
 | |
|     } catch (e) {
 | |
|       notify(NotificationEvent.error, { content: e.message });
 | |
|     }
 | |
| 
 | |
|     return false;
 | |
|   }
 | |
| }
 | 
