evoleasing-account/actions/questionnaireActions.js
2023-03-31 10:47:58 +03:00

121 lines
3.2 KiB
JavaScript

import axios from 'axios';
import { Cookies } from 'react-cookie';
import Router from 'next/router';
import moment from 'moment';
import { nSQL } from "@nano-sql/core";
import fileDownload from 'js-file-download';
import * as actionTypes from '../constants/actionTypes';
if(process.browser)
{
FormData.prototype.appendObject = function(obj, namespace)
{
let keyName;
for (var key in obj)
{
if (obj.hasOwnProperty(key))
{
keyName = [namespace, '[', key, ']'].join('');
this.append(keyName, obj[key]);
}
}
};
}
export const getQuestionnaire = ({ dispatch }) =>
{
console.log("ACTION", "support", "getAppeals()", `${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/questionnaire/get`);
return new Promise((resolve, reject) =>
{
axios.get(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/questionnaire/get`)
.then((response) =>
{
console.log("ACTION", "support", "getAppeals()", "response", response.data);
//dispatch({ type: actionTypes.SUPPORT_APPEALS, data: { appeals: { list: response.data.appeals, new: response.data.new, } } });
resolve();
})
.catch((error) =>
{
console.log("error");
console.error(error);
reject();
});
});
}
export const updateQuestionnaire = ({ dispatch, questionnaire }) =>
{
console.log("ACTION", "questionnaireActions", "updateQuestionnaire()", { questionnaire });
return new Promise((resolve, reject) =>
{
dispatch({ type: actionTypes.QUESTIONNAIRE_UPDATE, data: { questionnaire, } });
/*
setTimeout(() => {
console.log("ACTION", "questionnaireActions", "updateQuestionnaire", global.store.getState(), { dispatch: dispatch });
resolve();
}, 500);
*/
});
}
export const downloadQuestionnaire = (download = true) =>
{
console.log("ACTION", "questionnaireActions", "downloadQuestionnaire()", );
//{ questionnaire }
//questionnaire
return new Promise((resolve, reject) =>
{
let cookies = new Cookies();
let cookies_list = cookies.getAll();
const { questionnaire } = global.store.getState();
console.log("questionnaire", questionnaire);
const { main, contacts, signatory_person, founded_persons, head_person, non_profit, } = questionnaire;
const playload = { main, contacts, signatory_person, founded_persons, head_person, non_profit, };
console.log({ playload });
/* axios.post(`${ process.env.NEXT_PUBLIC_INT_API_HOST }/questionnaire/download`, { questionnaire: playload }, {
headers: {
"Authorization": `Bearer ${ cookies_list.jwt }`,
},
})
*/
axios.post(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/questionnaire/download`, { questionnaire: playload }, {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/pdf'
},
responseType: 'arraybuffer',
})
.then((response) =>
{
if(download)
{
console.log("downloadQuestionnaire", "response.data");
fileDownload(response.data, response.headers.filename);
resolve();
}
else
{
resolve({ file: response.data, filename: response.headers.filename });
}
//dispatch({ type: actionTypes.SUPPORT_APPEALS, data: { appeals: { list: response.data.appeals, new: response.data.new, } } });
})
.catch((error) =>
{
console.log("error");
console.error(error);
reject();
});
});
}