mirror of
https://bitbucket.org/wisemapping/wisemapping-frontend.git
synced 2024-11-22 06:37:56 +01:00
Complete delete rest
This commit is contained in:
parent
56c843970b
commit
86d78935f0
@ -32,49 +32,6 @@ export type ErrorInfo = {
|
||||
fields?: Map<String, String>;
|
||||
}
|
||||
|
||||
export const parseResponseOnError = (response: any): ErrorInfo => {
|
||||
|
||||
let result: ErrorInfo | undefined;
|
||||
if (response) {
|
||||
const status: number = response.status;
|
||||
const data = response.data;
|
||||
console.log(data);
|
||||
|
||||
switch (status) {
|
||||
case 401:
|
||||
// this.authFailed();
|
||||
break;
|
||||
default:
|
||||
if (data) {
|
||||
// Set global errors ...
|
||||
result = {};
|
||||
let globalErrors = data.globalErrors;
|
||||
if (globalErrors && globalErrors.length > 0) {
|
||||
result.msg = globalErrors[0];
|
||||
}
|
||||
|
||||
// Set field errors ...
|
||||
if (data.fieldErrors && Object.keys(data.fieldErrors).length > 0) {
|
||||
result.fields = data.fieldErrors;
|
||||
if (!result.msg) {
|
||||
const key = Object.keys(data.fieldErrors)[0];
|
||||
result.msg = data.fieldErrors[key];
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
result = { msg: response.statusText };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Network related problem ...
|
||||
if (!result) {
|
||||
result = { msg: 'Unexpected error. Please, try latter' };
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
interface Client {
|
||||
createMap(map: BasicMapInfo): Promise<number>;
|
||||
|
@ -1,5 +1,5 @@
|
||||
import axios from 'axios';
|
||||
import { ErrorInfo, MapInfo, BasicMapInfo, NewUser, parseResponseOnError } from '..';
|
||||
import { ErrorInfo, MapInfo, BasicMapInfo, NewUser } from '..';
|
||||
import MockClient from '../mock-client/';
|
||||
|
||||
//@Remove inheritance once is it completed.
|
||||
@ -12,6 +12,50 @@ export default class RestClient extends MockClient {
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
private parseResponseOnError = (response: any): ErrorInfo => {
|
||||
|
||||
let result: ErrorInfo | undefined;
|
||||
if (response) {
|
||||
const status: number = response.status;
|
||||
const data = response.data;
|
||||
console.log(data);
|
||||
|
||||
switch (status) {
|
||||
case 401:
|
||||
// this.authFailed();
|
||||
break;
|
||||
default:
|
||||
if (data) {
|
||||
// Set global errors ...
|
||||
result = {};
|
||||
let globalErrors = data.globalErrors;
|
||||
if (globalErrors && globalErrors.length > 0) {
|
||||
result.msg = globalErrors[0];
|
||||
}
|
||||
|
||||
// Set field errors ...
|
||||
if (data.fieldErrors && Object.keys(data.fieldErrors).length > 0) {
|
||||
result.fields = data.fieldErrors;
|
||||
if (!result.msg) {
|
||||
const key = Object.keys(data.fieldErrors)[0];
|
||||
result.msg = data.fieldErrors[key];
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
result = { msg: response.statusText };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Network related problem ...
|
||||
if (!result) {
|
||||
result = { msg: 'Unexpected error. Please, try latter' };
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
createMap(model: BasicMapInfo): Promise<number> {
|
||||
const handler = (success: (mapId:number) => void, reject: (error: ErrorInfo) => void) => {
|
||||
axios.post(this.baseUrl + `/c/restful/maps?title=${model.title}&description=${model.description ? model.description : ''}`,
|
||||
@ -22,7 +66,7 @@ export default class RestClient extends MockClient {
|
||||
success(mapId);
|
||||
}).catch(error => {
|
||||
const response = error.response;
|
||||
const errorInfo = parseResponseOnError(response);
|
||||
const errorInfo = this.parseResponseOnError(response);
|
||||
reject(errorInfo);
|
||||
});
|
||||
}
|
||||
@ -58,7 +102,7 @@ export default class RestClient extends MockClient {
|
||||
console.log(error)
|
||||
|
||||
const response = error.response;
|
||||
const errorInfo = parseResponseOnError(response);
|
||||
const errorInfo = this.parseResponseOnError(response);
|
||||
reject(errorInfo);
|
||||
});
|
||||
}
|
||||
@ -74,14 +118,31 @@ export default class RestClient extends MockClient {
|
||||
// All was ok, let's sent to success page ...;
|
||||
success();
|
||||
}).catch(error => {
|
||||
console.log(error);
|
||||
const response = error.response;
|
||||
const errorInfo = parseResponseOnError(response);
|
||||
const errorInfo = this.parseResponseOnError(response);
|
||||
reject(errorInfo);
|
||||
});
|
||||
}
|
||||
return new Promise(handler);
|
||||
}
|
||||
|
||||
deleteMap(id: number): Promise<void> {
|
||||
const handler = (success: () => void, reject: (error: ErrorInfo) => void) => {
|
||||
axios.delete(this.baseUrl + `/c/restful/maps/${id}`,
|
||||
{ headers: { 'Content-Type': 'application/json' } }
|
||||
).then(response => {
|
||||
success();
|
||||
}).catch(error => {
|
||||
const response = error.response;
|
||||
const errorInfo = this.parseResponseOnError(response);
|
||||
reject(errorInfo);
|
||||
});
|
||||
}
|
||||
return new Promise(handler);
|
||||
}
|
||||
|
||||
|
||||
resetPassword(email: string): Promise<void> {
|
||||
|
||||
const handler = (success: () => void, reject: (error: ErrorInfo) => void) => {
|
||||
@ -93,7 +154,7 @@ export default class RestClient extends MockClient {
|
||||
success();
|
||||
}).catch(error => {
|
||||
const response = error.response;
|
||||
const errorInfo = parseResponseOnError(response);
|
||||
const errorInfo = this.parseResponseOnError(response);
|
||||
reject(errorInfo);
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user