Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit fe7e629

Browse files
author
Linsen Wu
committed
Add notebook children loading
1 parent 9630680 commit fe7e629

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed

src/components/notebookRenderStrategy.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ 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 * as OneNoteApi from 'onenoteapi';
16+
import { SpinnerIconSvg } from './icons/spinnerIcon.svg';
17+
import { Strings } from '../strings';
1518

1619
export class NotebookRenderStrategy implements ExpandableNodeRenderStrategy {
1720
onClickBinded = () => {};
21+
onExpandBinded = this.onExpand.bind(this);
1822

1923
constructor(private notebook: Notebook, private globals: InnerGlobals) { }
2024

@@ -42,6 +46,23 @@ export class NotebookRenderStrategy implements ExpandableNodeRenderStrategy {
4246
}
4347

4448
getChildren(childrenLevel: number): JSX.Element[] {
49+
if (typeof (this.notebook.apiHttpErrorCode) === 'number') {
50+
const errorString = Strings.getError(this.notebook.apiHttpErrorCode);
51+
return [
52+
<li role='status' aria-live='polite' aria-label={errorString} className='progress-row'>
53+
<div>{errorString}</div>
54+
</li>
55+
];
56+
}
57+
58+
if (this.notebook.needsToFetchChildren) {
59+
return [
60+
<li className='progress-row'>
61+
<SpinnerIconSvg />
62+
</li>
63+
];
64+
}
65+
4566
const createNewSection = this.globals.callbacks.onSectionCreated || this.globals.shouldShowCreateEntityInputs ?
4667
[<CreateNewSectionNode
4768
key={this.notebook.id + 'createnewsectionnode'}
@@ -121,4 +142,20 @@ export class NotebookRenderStrategy implements ExpandableNodeRenderStrategy {
121142
private onChevronClick() {
122143
this.expandNode();
123144
}
145+
146+
private onExpand() {
147+
if (this.notebook.needsToFetchChildren && this.notebook.apiUrl && this.globals.oneNoteDataProvider && !!this.globals.callbacks.onNotebookInfoReturned) {
148+
this.globals.oneNoteDataProvider.getNotebookBySelfUrl(this.notebook.apiUrl, 5).then((notebook) => {
149+
this.notebook.sections = notebook.sections
150+
this.notebook.sectionGroups = notebook.sectionGroups
151+
}).catch((apiError: OneNoteApi.RequestError) => {
152+
this.notebook.apiHttpErrorCode = apiError.statusCode;
153+
}).then(() => {
154+
this.notebook.needsToFetchChildren = false;
155+
if (!!this.globals.callbacks.onNotebookInfoReturned) {
156+
this.globals.callbacks.onNotebookInfoReturned(this.notebook);
157+
}
158+
})
159+
}
160+
}
124161
}

src/oneNoteDataStructures/notebook.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ 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+
apiHttpErrorCode?: number;
8+
69
expanded: boolean;
710
sectionGroups: SectionGroup[];
811
sections: Section[];
912
webUrl: string;
1013
apiUrl?: string;
1114
lastModifiedTime: Date;
15+
needsToFetchChildren?: boolean
1216
}

src/oneNoteDataStructures/sharedNotebook.ts

Lines changed: 0 additions & 1 deletion
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

src/props/oneNotePickerCallbacks.ts

Lines changed: 1 addition & 0 deletions
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;

src/providers/oneNoteApiDataProvider.ts

Lines changed: 6 additions & 0 deletions
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>) => {

src/providers/oneNoteDataProvider.ts

Lines changed: 1 addition & 0 deletions
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)