import React from 'react';
|
|
|
|
import notification from 'antd/lib/notification';
|
|
import Spin from 'antd/lib/spin';
|
|
|
|
export default class Service {
|
|
|
|
static spin(loading) {
|
|
return loading && <Spin />
|
|
}
|
|
|
|
static call(settings) {
|
|
const { url, method, body, setData, setLoading, errorTitle, errorMessage } = settings;
|
|
|
|
const errors = {};
|
|
|
|
if (!url || url == '') {
|
|
errors['url'] = 'Required!';
|
|
};
|
|
|
|
if (!setData) {
|
|
errors['setData'] = 'Required!';
|
|
};
|
|
|
|
if (!setLoading) {
|
|
errors['setLoading'] = 'Required!';
|
|
};
|
|
|
|
if (Object.keys(errors).length > 0) {
|
|
return console.error("Service call invalid setting:", errors);
|
|
}
|
|
|
|
setLoading(true);
|
|
const fail = () => {
|
|
setLoading(false);
|
|
notification["error"]({
|
|
message: errorTitle ? errorTitle : 'Erro no serviço',
|
|
description: errorMessage ? errorMessage : url,
|
|
style: {
|
|
marginTop: 100,
|
|
}
|
|
});
|
|
};
|
|
netuno.service({
|
|
url: `/services/${url}`,
|
|
method: method ? method.toUpperCase() : 'GET',
|
|
credentials: 'include',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: body ? body : null,
|
|
success: (response) => {
|
|
if (response.json) {
|
|
setData(response.json);
|
|
setLoading(false);
|
|
console.log('Service Call Success: ', url);
|
|
} else {
|
|
fail();
|
|
}
|
|
},
|
|
fail: () => {
|
|
fail();
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
}
|