2018-02-21 09:46:04 -08:00
|
|
|
import Component from '@ember/component';
|
2018-03-12 17:35:01 -07:00
|
|
|
import { get, set, getProperties, setProperties } from '@ember/object';
|
2018-02-21 09:46:04 -08:00
|
|
|
import { task, TaskInstance } from 'ember-concurrency';
|
2018-05-14 14:54:03 -07:00
|
|
|
import { action } from '@ember-decorators/object';
|
2018-02-27 00:03:06 -08:00
|
|
|
import Notifications from 'wherehows-web/services/notifications';
|
|
|
|
import { NotificationEvent } from 'wherehows-web/services/notifications';
|
2018-03-12 17:35:01 -07:00
|
|
|
import { IOwner, IOwnerResponse } from 'wherehows-web/typings/api/datasets/owners';
|
2018-02-21 09:46:04 -08:00
|
|
|
import {
|
|
|
|
OwnerType,
|
|
|
|
readDatasetOwnersByUrn,
|
2018-05-29 13:17:15 -07:00
|
|
|
readDatasetSuggestedOwnersByUrn,
|
2018-02-21 09:46:04 -08:00
|
|
|
readDatasetOwnerTypesWithoutConsumer,
|
|
|
|
updateDatasetOwnersByUrn
|
|
|
|
} from 'wherehows-web/utils/api/datasets/owners';
|
2018-08-12 13:02:01 -07:00
|
|
|
import { service } from '@ember-decorators/service';
|
2018-09-13 17:59:39 -07:00
|
|
|
import { IAppConfig } from 'wherehows-web/typings/api/configurator/configurator';
|
|
|
|
import Configurator from 'wherehows-web/services/configurator';
|
|
|
|
import { containerDataSource } from 'wherehows-web/utils/components/containers/data-source';
|
2018-02-21 09:46:04 -08:00
|
|
|
|
2018-09-13 17:59:39 -07:00
|
|
|
@containerDataSource('getContainerDataTask')
|
2018-02-21 09:46:04 -08:00
|
|
|
export default class DatasetOwnershipContainer extends Component {
|
|
|
|
/**
|
|
|
|
* The urn identifier for the dataset
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
urn: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of owners for the dataset
|
|
|
|
* @type {Array<IOwner>}
|
|
|
|
*/
|
|
|
|
owners: Array<IOwner>;
|
|
|
|
|
2018-05-29 13:17:15 -07:00
|
|
|
/**
|
|
|
|
* List of suggested owners for the dataset
|
|
|
|
* @type {Array<IOwner>}
|
|
|
|
*/
|
|
|
|
suggestedOwners: Array<IOwner>;
|
|
|
|
|
2018-02-21 09:46:04 -08:00
|
|
|
/**
|
|
|
|
* List of types available for a dataset owner
|
|
|
|
* @type {Array<OwnerType>}
|
|
|
|
*/
|
|
|
|
ownerTypes: Array<OwnerType>;
|
|
|
|
|
2018-02-27 00:03:06 -08:00
|
|
|
/**
|
|
|
|
* Reference to the application notifications Service
|
|
|
|
* @type {ComputedProperty<Notifications>}
|
|
|
|
*/
|
2018-08-12 13:02:01 -07:00
|
|
|
@service
|
|
|
|
notifications: Notifications;
|
2018-02-27 00:03:06 -08:00
|
|
|
|
2018-03-12 17:35:01 -07:00
|
|
|
/**
|
|
|
|
* Flag indicates that a ownership metadata is inherited from an upstream dataset
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
fromUpstream = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to the upstream dataset
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
upstreamUrn: string;
|
|
|
|
|
2018-09-13 17:59:39 -07:00
|
|
|
/**
|
|
|
|
* Avatar properties used to generate avatar images
|
|
|
|
* @type {(IAppConfig['userEntityProps'] | undefined)}
|
|
|
|
* @memberof DatasetOwnershipContainer
|
|
|
|
*/
|
|
|
|
avatarProperties: IAppConfig['userEntityProps'] | undefined;
|
2018-02-21 09:46:04 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An async parent task to group all data tasks for this container component
|
|
|
|
* @type {Task<TaskInstance<Promise<any>>, (a?: any) => TaskInstance<TaskInstance<Promise<any>>>>}
|
|
|
|
*/
|
2018-09-13 17:59:39 -07:00
|
|
|
getContainerDataTask = task(function*(
|
|
|
|
this: DatasetOwnershipContainer
|
|
|
|
): IterableIterator<TaskInstance<Promise<any> | IAppConfig['userEntityProps']>> {
|
2018-05-29 13:17:15 -07:00
|
|
|
const tasks = Object.values(
|
2018-09-13 17:59:39 -07:00
|
|
|
getProperties(this, [
|
|
|
|
'getDatasetOwnersTask',
|
|
|
|
'getSuggestedOwnersTask',
|
|
|
|
'getDatasetOwnerTypesTask',
|
|
|
|
'getAvatarProperties'
|
|
|
|
])
|
2018-05-29 13:17:15 -07:00
|
|
|
);
|
2018-02-21 09:46:04 -08:00
|
|
|
|
|
|
|
yield* tasks.map(task => task.perform());
|
|
|
|
});
|
|
|
|
|
2018-09-13 17:59:39 -07:00
|
|
|
/**
|
|
|
|
* Fetches & sets avatar props to build owner avatar images
|
|
|
|
* @memberof DatasetOwnershipContainer
|
|
|
|
*/
|
|
|
|
getAvatarProperties = task(function*(
|
|
|
|
this: DatasetOwnershipContainer
|
|
|
|
): IterableIterator<IAppConfig['userEntityProps']> {
|
|
|
|
return set(this, 'avatarProperties', Configurator.getConfig('userEntityProps'));
|
|
|
|
});
|
|
|
|
|
2018-02-21 09:46:04 -08:00
|
|
|
/**
|
|
|
|
* Reads the owners for this dataset
|
2018-03-12 17:35:01 -07:00
|
|
|
* @type {Task<Promise<Array<IOwner>>, (a?: any) => TaskInstance<Promise<IOwnerResponse>>>}
|
2018-02-21 09:46:04 -08:00
|
|
|
*/
|
2018-03-12 17:35:01 -07:00
|
|
|
getDatasetOwnersTask = task(function*(this: DatasetOwnershipContainer): IterableIterator<Promise<IOwnerResponse>> {
|
2018-03-14 10:55:25 -07:00
|
|
|
const { owners = [], fromUpstream, datasetUrn }: IOwnerResponse = yield readDatasetOwnersByUrn(get(this, 'urn'));
|
2018-02-21 09:46:04 -08:00
|
|
|
|
2018-03-12 17:35:01 -07:00
|
|
|
setProperties(this, { owners, fromUpstream, upstreamUrn: datasetUrn });
|
2018-02-21 09:46:04 -08:00
|
|
|
});
|
|
|
|
|
2018-05-29 13:17:15 -07:00
|
|
|
/**
|
|
|
|
* Fetches the suggested owners for this dataset
|
|
|
|
* @type {Task<Promise<Array<IOwner>>, (a?: any) => TaskInstance<Promise<IOwnerResponse>>>}
|
|
|
|
*/
|
|
|
|
getSuggestedOwnersTask = task(function*(this: DatasetOwnershipContainer): IterableIterator<Promise<IOwnerResponse>> {
|
2018-09-26 22:05:20 -07:00
|
|
|
const { owners = [] }: IOwnerResponse = yield readDatasetSuggestedOwnersByUrn(this.urn);
|
2018-05-29 13:17:15 -07:00
|
|
|
|
2018-09-26 22:05:20 -07:00
|
|
|
setProperties(this, { suggestedOwners: owners });
|
2018-05-29 13:17:15 -07:00
|
|
|
});
|
|
|
|
|
2018-02-21 09:46:04 -08:00
|
|
|
/**
|
|
|
|
* Reads the owner types available
|
|
|
|
* @type {Task<Promise<Array<OwnerType>>, (a?: any) => TaskInstance<Promise<Array<OwnerType>>>>}
|
|
|
|
*/
|
|
|
|
getDatasetOwnerTypesTask = task(function*(
|
|
|
|
this: DatasetOwnershipContainer
|
|
|
|
): IterableIterator<Promise<Array<OwnerType>>> {
|
2018-03-14 10:55:25 -07:00
|
|
|
const ownerTypes: Array<OwnerType> = yield readDatasetOwnerTypesWithoutConsumer();
|
2018-02-21 09:46:04 -08:00
|
|
|
set(this, 'ownerTypes', ownerTypes);
|
|
|
|
});
|
|
|
|
|
2018-02-27 00:03:06 -08:00
|
|
|
/**
|
|
|
|
* Handles user notifications when save succeeds or fails
|
|
|
|
* @template T the return type for the save request
|
|
|
|
* @param {Promise<T>} request to update owners
|
|
|
|
* @returns {Promise<T>}
|
|
|
|
* @memberof DatasetOwnershipContainer
|
|
|
|
*/
|
|
|
|
async notifyOnSave<T>(this: DatasetOwnershipContainer, request: Promise<T>): Promise<T> {
|
|
|
|
const { notify } = get(this, 'notifications');
|
|
|
|
|
|
|
|
try {
|
|
|
|
await request;
|
|
|
|
notify(NotificationEvent.success, { content: 'Changes have been successfully saved!' });
|
|
|
|
} catch (e) {
|
|
|
|
notify(NotificationEvent.error, { content: 'An error occurred while saving.' });
|
|
|
|
}
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
2018-02-21 09:46:04 -08:00
|
|
|
/**
|
|
|
|
* Persists the changes to the owners list
|
|
|
|
* @param {Array<IOwner>} updatedOwners
|
2018-02-27 00:03:06 -08:00
|
|
|
* @return {Promise<{}>}
|
2018-02-21 09:46:04 -08:00
|
|
|
*/
|
2018-02-21 14:41:05 -08:00
|
|
|
@action
|
2018-02-27 00:03:06 -08:00
|
|
|
async saveOwnerChanges(this: DatasetOwnershipContainer, updatedOwners: Array<IOwner>): Promise<{}> {
|
|
|
|
const result = await this.notifyOnSave(updateDatasetOwnersByUrn(get(this, 'urn'), '', updatedOwners));
|
2018-02-28 10:20:26 -08:00
|
|
|
const { notify } = get(this, 'notifications');
|
|
|
|
|
|
|
|
try {
|
|
|
|
get(this, 'getDatasetOwnersTask').perform();
|
|
|
|
} catch (e) {
|
|
|
|
notify(NotificationEvent.error, { content: 'Error occurred getting updated owners.' });
|
|
|
|
}
|
2018-02-27 00:03:06 -08:00
|
|
|
return result;
|
2018-02-21 09:46:04 -08:00
|
|
|
}
|
|
|
|
}
|