From 8eddf5025b16018fa2259801e046fe5784d75945 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Mon, 1 Mar 2021 22:10:16 -0800 Subject: [PATCH] Add decorator for client --- .../webapp/src/classes/app-config/index.ts | 5 +- .../client/cache-decorator-client/index.ts | 113 ++++++++++++++++++ packages/webapp/src/classes/client/index.ts | 2 +- .../src/classes/client/rest-client/index.ts | 3 +- 4 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 packages/webapp/src/classes/client/cache-decorator-client/index.ts diff --git a/packages/webapp/src/classes/app-config/index.ts b/packages/webapp/src/classes/app-config/index.ts index 32b2a18b..7524aed4 100644 --- a/packages/webapp/src/classes/app-config/index.ts +++ b/packages/webapp/src/classes/app-config/index.ts @@ -1,5 +1,6 @@ import { sessionExpired } from "../../redux/clientSlice"; import Client from "../client"; +import CacheDecoratorClient from "../client/cache-decorator-client"; import MockClient from "../client/mock-client"; import RestClient from "../client/rest-client"; @@ -60,7 +61,9 @@ class _AppConfig { console.log('Warning:Service using mockservice client'); result = new MockClient(); } - return result; + + // Wrap with a cache decorator ... + return new CacheDecoratorClient(result); } } const AppConfig = new _AppConfig(); diff --git a/packages/webapp/src/classes/client/cache-decorator-client/index.ts b/packages/webapp/src/classes/client/cache-decorator-client/index.ts new file mode 100644 index 00000000..5a584dcd --- /dev/null +++ b/packages/webapp/src/classes/client/cache-decorator-client/index.ts @@ -0,0 +1,113 @@ +import Client, { + AccountInfo, + BasicMapInfo, + ChangeHistory, + ImportMapInfo, + Label, + MapInfo, + NewUser, + Permission, +} from '..'; +import { LocaleCode } from '../../app-i18n'; + +class CacheDecoratorClient implements Client { + private client: Client; + + constructor(client: Client) { + this.client = client; + } + + deleteAccount(): Promise { + return this.client.deleteAccount(); + } + + importMap(model: ImportMapInfo): Promise { + return this.client.importMap(model); + } + + createMap(map: BasicMapInfo): Promise { + return this.client.createMap(map); + } + + deleteMaps(ids: number[]): Promise { + return this.client.deleteMaps(ids); + } + + deleteMap(id: number): Promise { + return this.client.deleteMap(id); + } + + renameMap(id: number, basicInfo: BasicMapInfo): Promise { + return this.client.renameMap(id, basicInfo); + } + + fetchAllMaps(): Promise { + return this.client.fetchAllMaps(); + } + + fetchMapPermissions(id: number): Promise { + return this.client.fetchMapPermissions(id); + } + + addMapPermissions(id: number, message: string, permissions: Permission[]): Promise { + return this.client.addMapPermissions(id, message, permissions); + } + + deleteMapPermission(id: number, email: string): Promise { + return this.client.deleteMapPermission(id, email); + } + + duplicateMap(id: number, basicInfo: BasicMapInfo): Promise { + return this.client.duplicateMap(id, basicInfo); + } + + updateAccountLanguage(locale: LocaleCode): Promise { + return this.client.updateAccountLanguage(locale); + } + + updateAccountPassword(pasword: string): Promise { + return this.client.updateAccountPassword(pasword); + } + + updateAccountInfo(firstname: string, lastname: string): Promise { + return this.client.updateAccountInfo(firstname, lastname); + } + + updateStarred(id: number, starred: boolean): Promise { + return this.client.updateStarred(id, starred); + } + + updateMapToPublic(id: number, isPublic: boolean): Promise { + return this.client.updateMapToPublic(id, isPublic); + } + + fetchLabels(): Promise { + return this.client.fetchLabels(); + } + + deleteLabel(id: number): Promise { + return this.client.deleteLabel(id); + } + + fetchAccountInfo(): Promise { + return this.client.fetchAccountInfo(); + } + + registerNewUser(user: NewUser): Promise { + return this.client.registerNewUser(user); + } + + resetPassword(email: string): Promise { + return this.client.resetPassword(email); + } + + fetchHistory(id: number): Promise { + return this.client.fetchHistory(id); + } + + revertHistory(id: number, cid: number): Promise { + return this.client.revertHistory(id,cid); + } +} + +export default CacheDecoratorClient; \ No newline at end of file diff --git a/packages/webapp/src/classes/client/index.ts b/packages/webapp/src/classes/client/index.ts index a683fbd4..ae653ede 100644 --- a/packages/webapp/src/classes/client/index.ts +++ b/packages/webapp/src/classes/client/index.ts @@ -92,7 +92,7 @@ interface Client { updateAccountInfo(firstname: string, lastname: string): Promise; updateStarred(id: number, starred: boolean): Promise; - updateMapToPublic(id: number, starred: boolean): Promise; + updateMapToPublic(id: number, isPublic: boolean): Promise; fetchLabels(): Promise; deleteLabel(id: number): Promise; diff --git a/packages/webapp/src/classes/client/rest-client/index.ts b/packages/webapp/src/classes/client/rest-client/index.ts index 0127e4d1..7632ebe6 100644 --- a/packages/webapp/src/classes/client/rest-client/index.ts +++ b/packages/webapp/src/classes/client/rest-client/index.ts @@ -235,11 +235,10 @@ export default class RestClient implements Client { updateMapToPublic(id: number, isPublic: boolean): Promise { const handler = (success: () => void, reject: (error: ErrorInfo) => void) => { axios - .put(`${this.baseUrl}/c/restful/maps/${id}/publish`, isPublic, { + .put(`${this.baseUrl}/c/restful/maps/${id}/publish`, isPublic.toString(), { headers: { 'Content-Type': 'text/plain' }, }) .then(() => { - // All was ok, let's sent to success page ...; success(); }) .catch((error) => {