Skip to content

Commit f74c918

Browse files
committed
support offline api
1 parent 23ebbfb commit f74c918

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

src/api/OfflineApi/Adapter/MemoryDb.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class MemoryDbAdapter implements DbAdapter {
1111

1212
async getAll<T>(storeName: string): Promise<T[]> {
1313
if (!this.db[storeName]) {
14-
return [];
14+
return Promise.resolve([]);
1515
}
16-
return Object.values(this.db[storeName]) as T[];
16+
return Promise.resolve(Object.values(this.db[storeName]) as T[]);
1717
}
1818

1919
async byDocumentRootId<T extends DocumentType>(
@@ -22,31 +22,34 @@ class MemoryDbAdapter implements DbAdapter {
2222
if (!documentRootId) {
2323
return Promise.resolve([]);
2424
}
25-
return this.filter('documents', (doc) => doc.documentRootId === documentRootId) as Promise<
26-
Document<T>[]
27-
>;
25+
return Promise.resolve(
26+
this.filter('documents', (doc) => doc.documentRootId === documentRootId) as Promise<Document<T>[]>
27+
);
2828
}
2929

3030
async put<T>(storeName: string, item: T & { id: string }): Promise<void> {
3131
if (!this.db[storeName]) {
3232
this.db[storeName] = {};
3333
}
3434
this.db[storeName][item.id] = item;
35+
return Promise.resolve();
3536
}
3637

3738
async delete(storeName: string, id: string): Promise<void> {
3839
if (this.db[storeName] && this.db[storeName][id]) {
3940
delete this.db[storeName][id];
4041
}
42+
return Promise.resolve();
4143
}
4244
async filter<T>(storeName: string, filterFn: (item: T) => boolean): Promise<T[]> {
4345
const allItems = await this.getAll<T>(storeName);
44-
return allItems.filter(filterFn);
46+
return Promise.resolve(allItems.filter(filterFn));
4547
}
4648

4749
async destroyDb(): Promise<void> {
4850
this.db = {};
4951
console.log('MemoryDbAdapter: Database destroyed');
52+
return Promise.resolve();
5053
}
5154
}
5255

src/api/OfflineApi/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const LOG_REQUESTS = false;
1818
let OfflineUser: User = {
1919
id: 'c23c0238-4aeb-457f-9a2c-3d2d5d8931c0',
2020
21+
name: 'Offline User',
2122
firstName: 'Offline',
2223
lastName: 'User',
2324
role: process.env.NODE_ENV === 'production' ? Role.STUDENT : Role.ADMIN,
@@ -29,6 +30,7 @@ const DB_NAME = `${siteConfig.organizationName ?? 'gbsl'}-${siteConfig.projectNa
2930
const DOCUMENTS_STORE = 'documents';
3031
const STUDENT_GROUPS_STORE = 'studentGroups';
3132
const PERMISSIONS_STORE = 'permissions';
33+
export const getOfflineUser = () => ({ ...OfflineUser });
3234

3335
const resolveResponse = <T>(data: T, statusCode: number = 200, statusText: string = ''): AxiosPromise<T> => {
3436
return Promise.resolve({
@@ -90,6 +92,7 @@ export default class OfflineApi {
9092
this.dbAdapter = new MemoryDbAdapter();
9193
}
9294
} else {
95+
console.log('setup memory adapter');
9396
this.dbAdapter = new MemoryDbAdapter();
9497
}
9598
if (LOG_REQUESTS) {

src/stores/SocketDataStore.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ export class SocketDataStore extends iStore<'ping'> {
6161
reaction(
6262
() => this.isLive,
6363
action((isLive) => {
64-
console.log('Socket.IO live:', isLive);
64+
if (!OFFLINE_API) {
65+
console.log('Socket.IO live:', isLive);
66+
}
6567
})
6668
);
6769
}

src/theme/Root.tsx

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
99
import { useHistory } from '@docusaurus/router';
1010
import LoggedOutOverlay from '@tdev-components/LoggedOutOverlay';
1111
import { authClient } from '@tdev/auth-client';
12+
import { getOfflineUser } from '@tdev-api/OfflineApi';
1213
const { OFFLINE_API, SENTRY_DSN } = siteConfig.customFields as {
1314
SENTRY_DSN?: string;
1415
OFFLINE_API?: boolean | 'memory' | 'indexedDB';
@@ -43,11 +44,18 @@ const RemoteNavigationHandler = observer(() => {
4344
return null;
4445
});
4546

46-
const Authentication = observer(() => {
47-
const { data: session } = authClient.useSession();
47+
const ExposeRootStoreToWindow = observer(() => {
4848
React.useEffect(() => {
49+
/**
50+
* Expose the store to the window object
51+
*/
4952
(window as any).store = rootStore;
5053
}, [rootStore]);
54+
return null;
55+
});
56+
57+
const Authentication = observer(() => {
58+
const { data: session } = authClient.useSession();
5159
React.useEffect(() => {
5260
if (!rootStore) {
5361
return;
@@ -61,6 +69,18 @@ const Authentication = observer(() => {
6169
return null;
6270
});
6371

72+
const OfflineApi = observer(() => {
73+
React.useEffect(() => {
74+
if (!OFFLINE_API) {
75+
return;
76+
}
77+
console.log('Using Offline API mode:', OFFLINE_API);
78+
const offlineUser = getOfflineUser();
79+
rootStore.load(offlineUser.id);
80+
}, []);
81+
return null;
82+
});
83+
6484
const Sentry = observer(() => {
6585
React.useEffect(() => {
6686
import('@sentry/react')
@@ -83,12 +103,6 @@ const Sentry = observer(() => {
83103

84104
function Root({ children }: { children: React.ReactNode }) {
85105
const { siteConfig } = useDocusaurusContext();
86-
React.useEffect(() => {
87-
/**
88-
* Expose the store to the window object
89-
*/
90-
(window as any).store = rootStore;
91-
}, [rootStore]);
92106

93107
return (
94108
<>
@@ -100,7 +114,10 @@ function Root({ children }: { children: React.ReactNode }) {
100114
/>
101115
</Head>
102116
<StoresProvider value={rootStore}>
103-
{!OFFLINE_API && (
117+
<ExposeRootStoreToWindow />
118+
{OFFLINE_API ? (
119+
<OfflineApi />
120+
) : (
104121
<>
105122
<Authentication />
106123
<RemoteNavigationHandler />

0 commit comments

Comments
 (0)