Add decorator for client

This commit is contained in:
Paulo Gustavo Veiga 2021-03-01 22:10:16 -08:00
parent fb2e4b3414
commit 8eddf5025b
4 changed files with 119 additions and 4 deletions

View File

@ -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();

View File

@ -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<void> {
return this.client.deleteAccount();
}
importMap(model: ImportMapInfo): Promise<number> {
return this.client.importMap(model);
}
createMap(map: BasicMapInfo): Promise<number> {
return this.client.createMap(map);
}
deleteMaps(ids: number[]): Promise<void> {
return this.client.deleteMaps(ids);
}
deleteMap(id: number): Promise<void> {
return this.client.deleteMap(id);
}
renameMap(id: number, basicInfo: BasicMapInfo): Promise<void> {
return this.client.renameMap(id, basicInfo);
}
fetchAllMaps(): Promise<MapInfo[]> {
return this.client.fetchAllMaps();
}
fetchMapPermissions(id: number): Promise<Permission[]> {
return this.client.fetchMapPermissions(id);
}
addMapPermissions(id: number, message: string, permissions: Permission[]): Promise<void> {
return this.client.addMapPermissions(id, message, permissions);
}
deleteMapPermission(id: number, email: string): Promise<void> {
return this.client.deleteMapPermission(id, email);
}
duplicateMap(id: number, basicInfo: BasicMapInfo): Promise<number> {
return this.client.duplicateMap(id, basicInfo);
}
updateAccountLanguage(locale: LocaleCode): Promise<void> {
return this.client.updateAccountLanguage(locale);
}
updateAccountPassword(pasword: string): Promise<void> {
return this.client.updateAccountPassword(pasword);
}
updateAccountInfo(firstname: string, lastname: string): Promise<void> {
return this.client.updateAccountInfo(firstname, lastname);
}
updateStarred(id: number, starred: boolean): Promise<void> {
return this.client.updateStarred(id, starred);
}
updateMapToPublic(id: number, isPublic: boolean): Promise<void> {
return this.client.updateMapToPublic(id, isPublic);
}
fetchLabels(): Promise<Label[]> {
return this.client.fetchLabels();
}
deleteLabel(id: number): Promise<void> {
return this.client.deleteLabel(id);
}
fetchAccountInfo(): Promise<AccountInfo> {
return this.client.fetchAccountInfo();
}
registerNewUser(user: NewUser): Promise<void> {
return this.client.registerNewUser(user);
}
resetPassword(email: string): Promise<void> {
return this.client.resetPassword(email);
}
fetchHistory(id: number): Promise<ChangeHistory[]> {
return this.client.fetchHistory(id);
}
revertHistory(id: number, cid: number): Promise<void> {
return this.client.revertHistory(id,cid);
}
}
export default CacheDecoratorClient;

View File

@ -92,7 +92,7 @@ interface Client {
updateAccountInfo(firstname: string, lastname: string): Promise<void>;
updateStarred(id: number, starred: boolean): Promise<void>;
updateMapToPublic(id: number, starred: boolean): Promise<void>;
updateMapToPublic(id: number, isPublic: boolean): Promise<void>;
fetchLabels(): Promise<Label[]>;
deleteLabel(id: number): Promise<void>;

View File

@ -235,11 +235,10 @@ export default class RestClient implements Client {
updateMapToPublic(id: number, isPublic: boolean): Promise<void> {
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) => {