diff options
| author | gabrhr <[email protected]> | 2022-05-27 12:50:13 -0500 |
|---|---|---|
| committer | gabrhr <[email protected]> | 2022-05-27 12:50:13 -0500 |
| commit | 9c396976bb42a9d41407358ce1a1d37242230f21 (patch) | |
| tree | 41b5fdf7d321f70c7e39bb788ef6ce0db6b4a6e3 /front/odiparpack/app/utils/odipar/api.js | |
| parent | 55c0f57d42d82f1f1f5809e9c7d6845b6e0b68af (diff) | |
| download | DP1_project-9c396976bb42a9d41407358ce1a1d37242230f21.tar.gz DP1_project-9c396976bb42a9d41407358ce1a1d37242230f21.tar.bz2 DP1_project-9c396976bb42a9d41407358ce1a1d37242230f21.zip | |
Add redux, dispatch and etiquetas
- estetica de etiquetas (ok)
- add message, pedido Reducer
- add dispatch en Componentes con connect
- add api reducer que obtienen el JSON todo ok
#TODO
- FIX logica de aƱadiir
- FIX axios y dispatch fuera de un componente
Diffstat (limited to 'front/odiparpack/app/utils/odipar/api.js')
| -rw-r--r-- | front/odiparpack/app/utils/odipar/api.js | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/front/odiparpack/app/utils/odipar/api.js b/front/odiparpack/app/utils/odipar/api.js new file mode 100644 index 0000000..ea5e617 --- /dev/null +++ b/front/odiparpack/app/utils/odipar/api.js @@ -0,0 +1,113 @@ +import Axios from 'axios' +import {apiUrl} from './constants'; +import qs from "query-string"; +import { openSuccessMessage, openErrorMessage } from 'ba-actions/message'; + +export function getHeaders() { + let session = getSession(); + return { + //Authorization: `Token ${(session && session.accessToken) || null}` + } +} + + +export function getSession() { + if (window && window.localStorage) { + return window.localStorage.getObject('session'); + } + + return null; +} + +export function saveSession(value) { + if (window && window.localStorage) { + return window.localStorage.saveObject('session', value); + } + + return null; +} + +export function apiReq(endPoint, data, method, headers, requestOptions = {}) { + return new Promise((res, rej) => { + + headers = { + ...headers + } + if (method === 'get' || method === 'delete') { + data = { + ...requestOptions, + params: data, + paramsSerializer: function (params) { + return qs.stringify(params, { arrayFormat: 'repeat' }) + }, + headers + } + } + + Axios[method](endPoint, data, { headers }).then((result) => { + let { data } = result; + console.log("aca en la promesa ps") + if (data.status === false) { + return rej(data); + } + + return res(data); + }).catch((err) => { + return rej(err); + }); + }) +} + +export function generateUrl(path) { + return apiUrl + path; +} + +export function apiPost(endPoint, data, headers = {}) { + return apiReq(generateUrl(endPoint), data, 'post', headers); +} + +export function apiDelete(endPoint, data, headers = {}) { + return apiReq(generateUrl(endPoint), data, 'delete', headers); +} + +export function apiGet(endPoint, data, headers = {}, requestOptions) { + return apiReq(generateUrl(endPoint), data, 'get', headers, requestOptions); +} + +export function apiPut(endPoint, data, headers = {}) { + return apiReq(generateUrl(endPoint), data, 'put', headers); +} + + +export function addMessageCurry(promise, dispatch, errorMsg = '', successMsg = '', showError = true) { + return new Promise( + (resolve, reject) => { + + promise + .then(response => { + + if (successMsg) { + console.log("mensajitooooo=?????") + dispatch( + openSuccessMessage(successMsg) + ) + } + + resolve(response) + }) + .catch( + err => { + if(showError) { + dispatch( + openErrorMessage(errorMsg || ((err.response && err.response.data.message) || 'Server encountered an error')) + ) + } + + reject(err) + } + ) + + } + ) + +}
\ No newline at end of file |
