[Notebook] Add active user to entries (#4764)

* if user provider, user added to notebook enntries and snapshot entries, updated code to work with asynnc nature of user api

Co-authored-by: Andrew Henry <akhenry@gmail.com>
Co-authored-by: Nikhil <nikhil.k.mandlik@nasa.gov>
This commit is contained in:
Jamie V 2022-02-22 12:37:47 -08:00 committed by GitHub
parent 384e36920c
commit 4e7debabb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 43 deletions

View File

@ -596,8 +596,9 @@ export default {
this.resetSearch();
const notebookStorage = this.createNotebookStorageObject();
this.updateDefaultNotebook(notebookStorage);
const id = addNotebookEntry(this.openmct, this.domainObject, notebookStorage, embed);
addNotebookEntry(this.openmct, this.domainObject, notebookStorage, embed).then(id => {
this.focusEntryId = id;
});
},
orientationChange() {
this.formatSidebar();

View File

@ -28,6 +28,9 @@
>
<div class="c-ne__time-and-content">
<div class="c-ne__time">
<template v-if="entry.createdBy">
<span class="c-icon icon-person">{{ entry.createdBy }}</span>
</template>
<span>{{ createdOnDate }}</span>
<span>{{ createdOnTime }}</span>
</div>
@ -182,7 +185,7 @@ export default {
this.dropOnEntry = this.dropOnEntry.bind(this);
},
methods: {
addNewEmbed(objectPath) {
async addNewEmbed(objectPath) {
const bounds = this.openmct.time.bounds();
const snapshotMeta = {
bounds,
@ -190,7 +193,7 @@ export default {
objectPath,
openmct: this.openmct
};
const newEmbed = createNewEmbed(snapshotMeta);
const newEmbed = await createNewEmbed(snapshotMeta);
this.entry.embeds.push(newEmbed);
},
cancelEditMode(event) {
@ -206,7 +209,7 @@ export default {
deleteEntry() {
this.$emit('deleteEntry', this.entry.id);
},
dropOnEntry($event) {
async dropOnEntry($event) {
event.stopImmediatePropagation();
const snapshotId = $event.dataTransfer.getData('openmct/snapshot/id');
@ -221,7 +224,7 @@ export default {
} else {
const data = $event.dataTransfer.getData('openmct/domain-object-path');
const objectPath = JSON.parse(data);
this.addNewEmbed(objectPath);
await this.addNewEmbed(objectPath);
}
this.$emit('updateEntry', this.entry);

View File

@ -41,7 +41,7 @@ export default class Snapshot {
fullSizeImageObjectIdentifier: object.identifier,
thumbnailImage
};
const embed = createNewEmbed(snapshotMeta, snapshot);
createNewEmbed(snapshotMeta, snapshot).then(embed => {
if (notebookType === NOTEBOOK_DEFAULT) {
const notebookStorage = getDefaultNotebook();
@ -51,6 +51,7 @@ export default class Snapshot {
} else {
this._saveToNotebookSnapshots(object, embed);
}
});
}
/**
@ -58,9 +59,8 @@ export default class Snapshot {
*/
_saveToDefaultNoteBook(notebookStorage, embed) {
this.openmct.objects.get(notebookStorage.identifier)
.then(async (domainObject) => {
addNotebookEntry(this.openmct, domainObject, notebookStorage, embed);
.then((domainObject) => {
return addNotebookEntry(this.openmct, domainObject, notebookStorage, embed).then(async () => {
let link = notebookStorage.link;
// Backwards compatibility fix (old notebook model without link)
@ -79,6 +79,7 @@ export default class Snapshot {
const msg = `Saved to Notebook ${defaultPath}`;
this._showNotification(msg, link);
});
});
}
/**

View File

@ -1,5 +1,17 @@
import objectLink from '../../../ui/mixins/object-link';
async function getUsername(openmct) {
let username = '';
if (openmct.user.hasProvider()) {
const user = await openmct.user.getCurrentUser();
username = user.getName();
}
return username;
}
export const DEFAULT_CLASS = 'notebook-default';
const TIME_BOUNDS = {
START_BOUND: 'tc.startBound',
@ -61,7 +73,7 @@ export function getHistoricLinkInFixedMode(openmct, bounds, historicLink) {
return params.join('&');
}
export function createNewEmbed(snapshotMeta, snapshot = '') {
export async function createNewEmbed(snapshotMeta, snapshot = '') {
const {
bounds,
link,
@ -83,10 +95,12 @@ export function createNewEmbed(snapshotMeta, snapshot = '') {
});
const name = domainObject.name;
const type = domainObject.identifier.key;
const createdBy = await getUsername(openmct);
return {
bounds,
createdOn: date,
createdBy,
cssClass,
domainObject,
historicLink,
@ -97,7 +111,7 @@ export function createNewEmbed(snapshotMeta, snapshot = '') {
};
}
export function addNotebookEntry(openmct, domainObject, notebookStorage, embed = null, entryText = '') {
export async function addNotebookEntry(openmct, domainObject, notebookStorage, embed = null, entryText = '') {
if (!openmct || !domainObject || !notebookStorage) {
return;
}
@ -109,10 +123,12 @@ export function addNotebookEntry(openmct, domainObject, notebookStorage, embed =
? [embed]
: [];
const createdBy = await getUsername(openmct);
const id = `entry-${date}`;
const entry = {
id,
createdOn: date,
createdBy,
text: entryText,
embeds
};

View File

@ -127,7 +127,7 @@ describe('Notebook Entries:', () => {
expect(entries.length).toEqual(0);
});
it('addNotebookEntry adds entry', () => {
it('addNotebookEntry adds entry', async () => {
const unlisten = openmct.objects.observe(notebookDomainObject, '*', (object) => {
const entries = NotebookEntries.getNotebookEntries(notebookDomainObject, selectedSection, selectedPage);
@ -135,17 +135,38 @@ describe('Notebook Entries:', () => {
unlisten();
});
NotebookEntries.addNotebookEntry(openmct, notebookDomainObject, notebookStorage);
await NotebookEntries.addNotebookEntry(openmct, notebookDomainObject, notebookStorage);
});
it('getEntryPosById returns valid position', () => {
const entryId1 = NotebookEntries.addNotebookEntry(openmct, notebookDomainObject, notebookStorage);
it('addNotebookEntry adds active user to entry', async () => {
const USER = 'Timmy';
openmct.user.hasProvider = () => true;
openmct.user.getCurrentUser = () => {
return Promise.resolve({
getName: () => {
return USER;
}
});
};
const unlisten = openmct.objects.observe(notebookDomainObject, '*', (object) => {
const entries = NotebookEntries.getNotebookEntries(notebookDomainObject, selectedSection, selectedPage);
expect(entries[0].createdBy).toEqual(USER);
unlisten();
});
await NotebookEntries.addNotebookEntry(openmct, notebookDomainObject, notebookStorage);
});
it('getEntryPosById returns valid position', async () => {
const entryId1 = await NotebookEntries.addNotebookEntry(openmct, notebookDomainObject, notebookStorage);
const position1 = NotebookEntries.getEntryPosById(entryId1, notebookDomainObject, selectedSection, selectedPage);
const entryId2 = NotebookEntries.addNotebookEntry(openmct, notebookDomainObject, notebookStorage);
const entryId2 = await NotebookEntries.addNotebookEntry(openmct, notebookDomainObject, notebookStorage);
const position2 = NotebookEntries.getEntryPosById(entryId2, notebookDomainObject, selectedSection, selectedPage);
const entryId3 = NotebookEntries.addNotebookEntry(openmct, notebookDomainObject, notebookStorage);
const entryId3 = await NotebookEntries.addNotebookEntry(openmct, notebookDomainObject, notebookStorage);
const position3 = NotebookEntries.getEntryPosById(entryId3, notebookDomainObject, selectedSection, selectedPage);
const success = position1 === 0
@ -155,9 +176,9 @@ describe('Notebook Entries:', () => {
expect(success).toBe(true);
});
it('deleteNotebookEntries deletes correct page entries', () => {
NotebookEntries.addNotebookEntry(openmct, notebookDomainObject, notebookStorage);
NotebookEntries.addNotebookEntry(openmct, notebookDomainObject, notebookStorage);
it('deleteNotebookEntries deletes correct page entries', async () => {
await NotebookEntries.addNotebookEntry(openmct, notebookDomainObject, notebookStorage);
await NotebookEntries.addNotebookEntry(openmct, notebookDomainObject, notebookStorage);
NotebookEntries.deleteNotebookEntries(openmct, notebookDomainObject, selectedSection, selectedPage);
const afterEntries = NotebookEntries.getNotebookEntries(notebookDomainObject, selectedSection, selectedPage);