Skip to content

Commit d8daaa7

Browse files
authored
Merge pull request #175 from OneNoteDev/linswu/addNotebookChildrenLoading
Linswu/add notebook children loading
2 parents 9630680 + d075f42 commit d8daaa7

11 files changed

+87
-14
lines changed

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "onenotepicker",
3-
"version": "3.0.11",
3+
"version": "3.0.12",
44
"files": [
55
"dist/**/*"
66
],

Diff for: src/components/notebookRenderStrategy.tsx

+44-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ import { NotebookOpenedIconSvg } from './icons/notebookOpenedIcon.svg';
1212
import { NotebookClosedIconSvg } from './icons/notebookClosedIcon.svg';
1313
import { ChevronSvg } from './icons/chevron.svg';
1414
import { CreateNewSectionNode } from './createNewSection/createNewSectionNode';
15+
import { SpinnerIconSvg } from './icons/spinnerIcon.svg';
16+
import { Strings } from '../strings';
1517

1618
export class NotebookRenderStrategy implements ExpandableNodeRenderStrategy {
1719
onClickBinded = () => {};
20+
onExpandBinded = this.onExpand.bind(this);
1821

1922
constructor(private notebook: Notebook, private globals: InnerGlobals) { }
2023

@@ -42,6 +45,23 @@ export class NotebookRenderStrategy implements ExpandableNodeRenderStrategy {
4245
}
4346

4447
getChildren(childrenLevel: number): JSX.Element[] {
48+
if (this.notebook.apiHttpErrorMessage) {
49+
const errorString = this.notebook.apiHttpErrorMessage;
50+
return [
51+
<li role='status' aria-live='polite' aria-label={errorString} className='progress-row'>
52+
<div>{errorString}</div>
53+
</li>
54+
];
55+
}
56+
57+
if (!this.notebook.sections && !this.notebook.sectionGroups) {
58+
return [
59+
<li className='progress-row'>
60+
<SpinnerIconSvg />
61+
</li>
62+
];
63+
}
64+
4565
const createNewSection = this.globals.callbacks.onSectionCreated || this.globals.shouldShowCreateEntityInputs ?
4666
[<CreateNewSectionNode
4767
key={this.notebook.id + 'createnewsectionnode'}
@@ -56,6 +76,10 @@ export class NotebookRenderStrategy implements ExpandableNodeRenderStrategy {
5676
</CreateNewSectionNode>] :
5777
[];
5878

79+
if (!this.notebook.sections || !this.notebook.sectionGroups) {
80+
return [...createNewSection]
81+
}
82+
5983
const setsize = this.notebook.sections.length + this.notebook.sectionGroups.length;
6084

6185
const sectionGroupRenderStrategies = this.notebook.sectionGroups.map(sectionGroup => new SectionGroupRenderStrategy(sectionGroup, this.globals));
@@ -65,7 +89,7 @@ export class NotebookRenderStrategy implements ExpandableNodeRenderStrategy {
6589
expanded={renderStrategy.isExpanded()} node={renderStrategy} globals={this.globals}
6690
treeViewId={Constants.TreeView.id} key={renderStrategy.getId()}
6791
id={renderStrategy.getId()} level={childrenLevel} ariaSelected={renderStrategy.isAriaSelected()} selected={renderStrategy.isSelected()}
68-
setsize={setsize} posinset={this.notebook.sections.length + i + 1} /> :
92+
setsize={setsize} posinset={this.notebook.sections ? this.notebook.sections.length + i + 1 : undefined} /> :
6993
<LeafNode node={renderStrategy} treeViewId={Constants.TreeView.id} key={renderStrategy.getId()} globals={this.globals}
7094
id={renderStrategy.getId()} level={childrenLevel} ariaSelected={renderStrategy.isAriaSelected()} />);
7195

@@ -121,4 +145,23 @@ export class NotebookRenderStrategy implements ExpandableNodeRenderStrategy {
121145
private onChevronClick() {
122146
this.expandNode();
123147
}
148+
149+
private onExpand() {
150+
if (!this.notebook.sections && !this.notebook.sectionGroups && this.notebook.apiUrl && this.globals.oneNoteDataProvider) {
151+
this.globals.oneNoteDataProvider.getNotebookBySelfUrl(this.notebook.apiUrl, 5).then((notebook) => {
152+
this.notebook.sections = notebook.sections
153+
this.notebook.sectionGroups = notebook.sectionGroups
154+
}).catch((apiError: any) => {
155+
try {
156+
this.notebook.apiHttpErrorMessage = JSON.parse(apiError.response).error.message
157+
} catch (error) {
158+
this.notebook.apiHttpErrorMessage = Strings.getError(apiError.statusCode);
159+
}
160+
}).then(() => {
161+
if (this.globals.callbacks.onNotebookInfoReturned) {
162+
this.globals.callbacks.onNotebookInfoReturned(this.notebook);
163+
}
164+
})
165+
}
166+
}
124167
}

Diff for: src/components/sharedNotebookRenderStrategy.tsx

+9-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { NotebookClosedIconSvg } from './icons/notebookClosedIcon.svg';
1515
import { SpinnerIconSvg } from './icons/spinnerIcon.svg';
1616
import { ChevronSvg } from './icons/chevron.svg';
1717
import { CreateNewSectionNode } from './createNewSection/createNewSectionNode';
18-
import * as OneNoteApi from 'onenoteapi';
1918

2019
export class SharedNotebookRenderStrategy implements ExpandableNodeRenderStrategy {
2120
onClickBinded = () => {};
@@ -52,8 +51,8 @@ export class SharedNotebookRenderStrategy implements ExpandableNodeRenderStrateg
5251
}
5352

5453
getChildren(childrenLevel: number): JSX.Element[] {
55-
if (typeof (this.notebook.apiHttpErrorCode) === 'number') {
56-
const errorString = Strings.getError(this.notebook.apiHttpErrorCode);
54+
if (this.notebook.apiHttpErrorMessage) {
55+
const errorString = this.notebook.apiHttpErrorMessage;
5756
return [
5857
<li role='status' aria-live='polite' aria-label={errorString} className='progress-row'>
5958
<div>{errorString}</div>
@@ -155,7 +154,7 @@ export class SharedNotebookRenderStrategy implements ExpandableNodeRenderStrateg
155154

156155
this.globals.oneNoteDataProvider.getSpNotebookProperties(this.notebook, depth, true).then((apiProperties) => {
157156
if (!apiProperties) {
158-
this.notebook.apiHttpErrorCode = 404;
157+
this.notebook.apiHttpErrorMessage = Strings.getError(404);
159158
return;
160159
}
161160

@@ -164,8 +163,12 @@ export class SharedNotebookRenderStrategy implements ExpandableNodeRenderStrateg
164163
if (this.globals.notebookListUpdater) {
165164
this.globals.notebookListUpdater.updateNotebookList([this.notebook]);
166165
}
167-
}).catch((apiError: OneNoteApi.RequestError) => {
168-
this.notebook.apiHttpErrorCode = apiError.statusCode;
166+
}).catch((apiError: any) => {
167+
try {
168+
this.notebook.apiHttpErrorMessage = JSON.parse(apiError.response).error.message
169+
} catch (error) {
170+
this.notebook.apiHttpErrorMessage = Strings.getError(apiError.statusCode);
171+
}
169172
}).then(() => {
170173
const { onSharedNotebookInfoReturned } = this.globals.callbacks;
171174
if (!!onSharedNotebookInfoReturned) {

Diff for: src/oneNoteDataStructures/notebook.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import { Section } from './section';
33
import { SectionGroup } from './sectionGroup';
44

55
export interface Notebook extends OneNoteItem {
6+
// Properties that need to be loaded-on-demand
7+
apiHttpErrorMessage?: string;
8+
69
expanded: boolean;
7-
sectionGroups: SectionGroup[];
8-
sections: Section[];
10+
sectionGroups?: SectionGroup[];
11+
sections?: Section[];
912
webUrl: string;
1013
apiUrl?: string;
1114
lastModifiedTime: Date;

Diff for: src/oneNoteDataStructures/notebookListUpdater.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class NotebookListUpdater {
5858
addSection(newSection: Section) {
5959
const loneParent = newSection.parent!;
6060
const parentInHierarchy = OneNoteItemUtils.find(this.notebooks, item => item.id === loneParent.id) as SectionParent | undefined;
61-
if (parentInHierarchy) {
61+
if (parentInHierarchy && parentInHierarchy.sections) {
6262
// Establish two-way reference
6363
parentInHierarchy.sections = [newSection, ...parentInHierarchy.sections];
6464
newSection.parent = parentInHierarchy;
@@ -69,6 +69,11 @@ export class NotebookListUpdater {
6969
// Preserve properties we want to keep from the original object ...
7070
next.expanded = original.expanded;
7171

72+
if (!original.sections || !original.sectionGroups ||
73+
!next.sections || !next.sectionGroups) {
74+
return;
75+
}
76+
7277
// ... then recurse through the children
7378
for (let newSectionGroup of next.sectionGroups) {
7479
const originalSectionGroup = original.sectionGroups.find(sg => sg.id === newSectionGroup.id);
@@ -118,6 +123,10 @@ export class NotebookListUpdater {
118123
}
119124

120125
private getSectionRefFromSectionParent(sectionId: string, sectionParent: SectionParent): Section | undefined {
126+
if (!sectionParent.sections || !sectionParent.sectionGroups) {
127+
return undefined;
128+
}
129+
121130
// Search this parent's sections for the matching id ...
122131
for (let childSection of sectionParent.sections) {
123132
if (childSection.id === sectionId) {

Diff for: src/oneNoteDataStructures/oneNoteApiResponseTransformer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export class OneNoteApiResponseTransformer {
3030
lastModifiedTime: notebook.lastModifiedTime
3131
};
3232

33-
transformed.sectionGroups = notebook.sectionGroups ? notebook.sectionGroups.map(sg => this.transformSectionGroup(sg, transformed)) : [];
34-
transformed.sections = notebook.sections ? notebook.sections.map(section => this.transformSection(section, transformed)) : [];
33+
transformed.sectionGroups = notebook.sectionGroups ? notebook.sectionGroups.map(sg => this.transformSectionGroup(sg, transformed)) : undefined;
34+
transformed.sections = notebook.sections ? notebook.sections.map(section => this.transformSection(section, transformed)) : undefined;
3535

3636
return transformed;
3737
}

Diff for: src/oneNoteDataStructures/oneNoteItemUtils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export class OneNoteItemUtils {
2929
sectionGroups = sectionParentNotebook.apiProperties.spSectionGroups;
3030
sections = sectionParentNotebook.apiProperties.spSections;
3131
} else {
32+
if (!sectionParent.sections || !sectionParent.sectionGroups) {
33+
continue;
34+
}
35+
3236
sectionGroups = sectionParent.sectionGroups;
3337
sections = sectionParent.sections;
3438
}
@@ -93,6 +97,10 @@ export class OneNoteItemUtils {
9397
* one descendent section; false otherwise.
9498
*/
9599
static prune(root: Notebook | SectionGroup): boolean {
100+
if (!root.sections || !root.sectionGroups) {
101+
return false
102+
}
103+
96104
root.sectionGroups = root.sectionGroups.filter(OneNoteItemUtils.prune);
97105
return root.sectionGroups.length > 0 || root.sections.length > 0;
98106
}

Diff for: src/oneNoteDataStructures/sharedNotebook.ts

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { SectionGroup } from './sectionGroup';
99
export interface SharedNotebook extends Notebook {
1010
// Properties that need to be loaded-on-demand
1111
apiProperties?: SharedNotebookApiProperties;
12-
apiHttpErrorCode?: number;
1312
startedLoading?: boolean;
1413

1514
// Properties returned from GetRecentNotebooks

Diff for: src/props/oneNotePickerCallbacks.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface OneNotePickerCallbacks {
2222
// Shared notebooks have to be loaded on demand, as we only have their URLs and names
2323
// to begin with. This is because getting sections for a shared notebook is expensive.
2424
onSharedNotebookInfoReturned?: (sharedNotebook: SharedNotebook) => void;
25+
onNotebookInfoReturned?: (notebook: Notebook) => void;
2526

2627
// Selection callbacks
2728
onNotebookSelected?: (notebook: Notebook, breadcrumbs: OneNoteItem[]) => void;

Diff for: src/providers/oneNoteApiDataProvider.ts

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ export class OneNoteApiDataProvider implements OneNoteDataProvider {
4747
});
4848
}
4949

50+
getNotebookBySelfUrl(selfUrl: string, expands?: number): Promise<Notebook> {
51+
return this.api.getNotebookBySelfUrl(selfUrl, expands).then((response) => {
52+
return this.responseTransformer.transformNotebook(response.parsedResponse);
53+
});
54+
}
55+
5056
getPages(section: Section): Promise<Page[]> {
5157
// tslint:disable-next-line:no-any
5258
return this.api.getPages({ sectionId: section.id }).then((responsePackage: OneNoteApi.ResponsePackage<any>) => {

Diff for: src/providers/oneNoteDataProvider.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface OneNoteDataProvider {
1414
createSectionUnderSectionGroup(parent: SectionGroup, name: string): Promise<Section>;
1515

1616
getNotebooks(expands?: number, excludeReadOnlyNotebooks?: boolean): Promise<Notebook[]>;
17+
getNotebookBySelfUrl(selfUrl: string, expands?: number): Promise<Notebook>;
1718
getPages(section: Section): Promise<Page[]>;
1819

1920
getSpNotebooks(): Promise<SharedNotebook[]>;

0 commit comments

Comments
 (0)