From 6e54585c9ce1299dc87fc708529b0ae62809811c Mon Sep 17 00:00:00 2001 From: merelendor Date: Sun, 28 Nov 2021 13:41:59 +0100 Subject: [PATCH] spinner for payments, API actions --- actions/authActions.js | 23 +++-- actions/companyActions.js | 47 ++++++++++ actions/index.js | 3 +- next.config.js | 2 +- package.json | 3 +- pages/api/calendar.js | 11 --- pages/api/company.js | 51 +++++++++++ pages/api/contract/documents.js | 47 +++++----- pages/api/contract/insurance.js | 12 +-- pages/contract/index.js | 156 +++++++++++++++++--------------- yarn.lock | 5 + 11 files changed, 238 insertions(+), 122 deletions(-) create mode 100644 actions/companyActions.js create mode 100644 pages/api/company.js diff --git a/actions/authActions.js b/actions/authActions.js index d98a4e3..7b3057e 100644 --- a/actions/authActions.js +++ b/actions/authActions.js @@ -6,6 +6,8 @@ import moment from 'moment'; import * as actionTypes from '../constants/actionTypes'; import * as currentState from '../reducers/initialState'; +import { getCompanyInfo } from './companyActions'; + if(process.browser) { FormData.prototype.appendObject = function(obj, namespace) @@ -37,15 +39,20 @@ export const sendLoginFormEmail = ({ email, password, dispatch }) => const cookies = new Cookies(); cookies.set('jwt', response.data.token, new Date(moment().add(7, 'day').toDate())); - console.log("try to actionTypes.COMPANY", actionTypes.COMPANY); - console.log("response.data.company", response.data.company); + getCompanyInfo({ dispatch }) + .then(() => + { + dispatch({ type: actionTypes.AUTH, data: { logged: true } }); + dispatch({ type: actionTypes.USER, data: response.data.user }); - dispatch({ type: actionTypes.AUTH, data: { logged: true } }); - dispatch({ type: actionTypes.USER, data: response.data.user }); - dispatch({ type: actionTypes.COMPANY, data: response.data.company }); - - resolve(); - Router.push('/'); + resolve(); + Router.push('/'); + }) + .catch(() => + { + reject(); + }); + //dispatch({ type: actionTypes.COMPANY, data: response.data.company }); } else { diff --git a/actions/companyActions.js b/actions/companyActions.js new file mode 100644 index 0000000..a8a58a9 --- /dev/null +++ b/actions/companyActions.js @@ -0,0 +1,47 @@ +import axios from 'axios'; +import { Cookies } from 'react-cookie'; +import Router from 'next/router'; +import moment from 'moment'; + +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 getCompanyInfo = ({ dispatch }) => +{ + console.log("getCompanyInfo"); + return new Promise((resolve, reject) => + { + axios.post(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/company`, {}, { + withCredentials: true, + }) + .then((response) => + { + console.log("getCompanyInfo", "response", response.data); + + dispatch({ type: actionTypes.COMPANY, data: response.data }); + resolve(); + }) + .catch((error) => + { + console.log("error"); + console.error(error); + + reject(); + }); + }); +} diff --git a/actions/index.js b/actions/index.js index fde2506..9fea2eb 100644 --- a/actions/index.js +++ b/actions/index.js @@ -1,4 +1,5 @@ export * from './authActions'; export * from './contractsActions'; export * from './contractActions'; -export * from './calendarActions'; \ No newline at end of file +export * from './calendarActions'; +export * from './companyActions'; \ No newline at end of file diff --git a/next.config.js b/next.config.js index 9d086d8..cf8d0ce 100644 --- a/next.config.js +++ b/next.config.js @@ -4,7 +4,7 @@ const withLess = require("next-with-less"); module.exports = withImages(withFonts(withLess({ images: { - domains: [ 'evo-lk.quickcode.ru', 'wow.evoleasing.ru', 'www.evoleasing.ru', 'lk.evoleasing.ru', 'evoleasing.ru', 'localhost', 'localhost:3000'], + domains: [ 'lk-evo.quickcode.ru', 'wow.evoleasing.ru', 'www.evoleasing.ru', 'lk.evoleasing.ru', 'evoleasing.ru', 'localhost', 'localhost:3000'], }, reactStrictMode: true, /* diff --git a/package.json b/package.json index 49cf791..52f1680 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ "react-redux": "^7.2.6", "react-widgets": "^5.5.1", "redux": "^4.1.2", - "redux-persist": "^6.0.0" + "redux-persist": "^6.0.0", + "spinners-react": "^1.0.6" }, "devDependencies": { "eslint": "^8.3.0", diff --git a/pages/api/calendar.js b/pages/api/calendar.js index 2cdc59b..5315cf8 100644 --- a/pages/api/calendar.js +++ b/pages/api/calendar.js @@ -16,18 +16,9 @@ export default async function handler(req, res) if(cookies.jwt !== undefined && cookies.jwt !== null) { - console.log("cookies.jwt"); - console.log(cookies.jwt); - var client_jwt_decoded = jwt.verify(cookies.jwt, process.env.JWT_SECRET_CLIENT); var crm_jwt = jwt.sign(client_jwt_decoded, process.env.JWT_SECRET_CRM, { noTimestamp: true }); - console.log("client_jwt_decoded", client_jwt_decoded); - console.log("crm_jwt", crm_jwt); - - console.log(`URL`); - console.log(`${ process.env.CRM_API_HOST }/lk/Account/GetPaymentCalendar/`); - try { axios.get(`${ process.env.CRM_API_HOST }/lk/Account/GetPaymentCalendar/`, { @@ -38,8 +29,6 @@ export default async function handler(req, res) }) .then((crm_response) => { - console.log("crm_response"); - console.log(crm_response); res.status(200).json(crm_response.data); }) .catch((error) => diff --git a/pages/api/company.js b/pages/api/company.js new file mode 100644 index 0000000..c9e5261 --- /dev/null +++ b/pages/api/company.js @@ -0,0 +1,51 @@ +// Next.js API route support: https://nextjs.org/docs/api-routes/introduction +import axios from 'axios'; +import { Cookies } from 'react-cookie'; +import cookie from 'cookie'; +import moment from 'moment'; +import jwt from 'jsonwebtoken'; +import { cors } from '../../lib/cors'; + +export default async function handler(req, res) +{ + await cors(req, res); + + if(req.headers.cookie !== undefined) + { + const cookies = cookie.parse(req.headers?.cookie ? req.headers?.cookie : ""); + + if(cookies.jwt !== undefined && cookies.jwt !== null) + { + var client_jwt_decoded = jwt.verify(cookies.jwt, process.env.JWT_SECRET_CLIENT); + var crm_jwt = jwt.sign(client_jwt_decoded, process.env.JWT_SECRET_CRM, { noTimestamp: true }); + + try + { + axios.get(`${ process.env.CRM_API_HOST }/lk/Account/GetCompanyInfo/`, { + params: client_jwt_decoded, + headers: { + "Authorization": `Bearer ${ crm_jwt }`, + } + }) + .then((crm_response) => + { + res.status(200).json(crm_response.data); + }) + .catch((error) => + { + console.error(error); + res.status(500); + }); + } + catch(e) + { + console.error(e); + res.status(500); + } + } + else + { + res.status(403); + } + } +} \ No newline at end of file diff --git a/pages/api/contract/documents.js b/pages/api/contract/documents.js index b2320c3..ba2b331 100644 --- a/pages/api/contract/documents.js +++ b/pages/api/contract/documents.js @@ -18,28 +18,33 @@ export default async function handler(req, res) var client_jwt_decoded = jwt.verify(cookies.jwt, process.env.JWT_SECRET_CLIENT); var crm_jwt = jwt.sign(client_jwt_decoded, process.env.JWT_SECRET_CRM, { noTimestamp: true }); - try + const result = { + upd: null, + fines: null, + }; + + await Promise.all([ + new Promise((resolve) => { + axios.get(`${ process.env.CRM_API_HOST }/lk/Contract/GetUPDListByContract`, { + params: { ...client_jwt_decoded, contract_number: req.body.number }, + headers: { "Authorization": `Bearer ${ crm_jwt }`, }, + }) + .then((crm_response) => { result.upd = crm_response.data; resolve(); }) + .catch((error) => { console.error(error); resolve(); }); + }), + new Promise((resolve) => { + axios.get(`${ process.env.CRM_API_HOST }/lk/Contract/GetFineGIBDDList`, { + params: { ...client_jwt_decoded, contract_number: req.body.number }, + headers: { "Authorization": `Bearer ${ crm_jwt }`, }, + }) + .then((crm_response) => { result.fines = crm_response.data; resolve(); }) + .catch((error) => { console.error(error); resolve(); }); + }), + ]) + .then(() => { - await axios.get(`${ process.env.CRM_API_HOST }/lk/Contract/GetUPDListByContract`, { - params: { ...client_jwt_decoded, contract_number: req.body.number }, - headers: { "Authorization": `Bearer ${ crm_jwt }`, }, - withCredentials: true, - }) - .then((crm_response) => - { - res.status(200).json(crm_response.data); - }) - .catch((error) => - { - console.error(error); - res.status(500); - }); - } - catch(e) - { - console.error(e); - res.status(500); - } + res.status(200).json(result); + }); } else { diff --git a/pages/api/contract/insurance.js b/pages/api/contract/insurance.js index 98571b5..287816e 100644 --- a/pages/api/contract/insurance.js +++ b/pages/api/contract/insurance.js @@ -39,26 +39,26 @@ export default async function handler(req, res) await Promise.all([ new Promise((resolve) => { axios.get(`${ process.env.CRM_API_HOST }/lk/Contract/GetOsago`, { - params: client_jwt_decoded, + params: { ...client_jwt_decoded, contract_number: req.body.number }, headers: { "Authorization": `Bearer ${ crm_jwt }`, }, }) - .then((crm_response) => { result.osago = crm_response; resolve(); }) + .then((crm_response) => { result.osago = crm_response.data; resolve(); }) .catch((error) => { console.error(error); resolve(); }); }), new Promise((resolve) => { axios.get(`${ process.env.CRM_API_HOST }/lk/Contract/GetKasko`, { - params: client_jwt_decoded, + params: { ...client_jwt_decoded, contract_number: req.body.number }, headers: { "Authorization": `Bearer ${ crm_jwt }`, }, }) - .then((crm_response) => { result.kasko = crm_response; resolve(); }) + .then((crm_response) => { result.kasko = crm_response.data; resolve(); }) .catch((error) => { console.error(error); resolve(); }); }), new Promise((resolve) => { axios.get(`${ process.env.CRM_API_HOST }/lk/Contract/GetNsib`, { - params: client_jwt_decoded, + params: { ...client_jwt_decoded, contract_number: req.body.number }, headers: { "Authorization": `Bearer ${ crm_jwt }`, }, }) - .then((crm_response) => { result.nsib = crm_response; resolve(); }) + .then((crm_response) => { result.nsib = crm_response.data; resolve(); }) .catch((error) => { console.error(error); resolve(); }); }), ]) diff --git a/pages/contract/index.js b/pages/contract/index.js index 15a3265..9b2b6c6 100644 --- a/pages/contract/index.js +++ b/pages/contract/index.js @@ -3,6 +3,8 @@ import Head from 'next/head'; import Image from 'next/image'; import { connect } from "react-redux"; import { withRouter } from 'next/router'; +import { SpinnerCircular } from 'spinners-react'; + import { reduxWrapper } from '../../store'; import Header from '../components/Header'; @@ -75,7 +77,7 @@ class ContractSchedulePage extends React.Component render() { - const { payments, contract_date, full, opened } = this.state; + const { payments, contract_date, full, opened, loading } = this.state; const { number } = this.props; console.log("RENDER", "payments"); @@ -121,83 +123,91 @@ class ContractSchedulePage extends React.Component
Статус платежа
Платежное поручение
- { !full && ( - - ) } - { payments !== null && Object.values(payments).map((payment, index) => - { - let pd = moment(payment.date, "DD-MM-YYYY"); - if(!full && today > pd) { return null; } + { loading ? ( +
+ +
+ ) : ( + <> + { !full && ( + + ) } + { payments !== null && Object.values(payments).map((payment, index) => + { + let pd = moment(payment.date, "DD-MM-YYYY"); + if(!full && today > pd) { return null; } - return ( -
-1 ? "opened" : "" }` } data-status={ payment.status === "NotPaid" && pd < today ? "notpaid" : statuses[payment.status] } key={ index }> -
{ payment.number }
-
{ moment(payment.date, "DD-MM-YYYY").format("DD.MM.YYYY") }
-
{ numeral(payment.total_amount).format('') }
-
{ numeral(payment.vat_amount).format('') }
-
- { payment.status === "Paid" && "Оплачено" } - { payment.status === "NotPaid" && "Не оплачено" } - { payment.status === "HalfPaid" && "Частично оплачено" } - { payment.status === "OverPaid" && ( -
Переплата { numeral(payment.total_amount).format('') } ₽
- ) } -
-
- { payment.invoices.map((invoice, invoice_index) => - ( - -

№{ invoice.number } от { moment(invoice.date, "DD-MM-YYYY").format("DD.MM.YYYY") } на сумму { numeral(invoice.total_amount).format('') } ₽

-
- )) } -
- + return ( +
-1 ? "opened" : "" }` } data-status={ payment.status === "NotPaid" && pd < today ? "notpaid" : statuses[payment.status] } key={ index }> +
{ payment.number }
+
{ moment(payment.date, "DD-MM-YYYY").format("DD.MM.YYYY") }
+
{ numeral(payment.total_amount).format('') }
+
{ numeral(payment.vat_amount).format('') }
+
+ { payment.status === "Paid" && "Оплачено" } + { payment.status === "NotPaid" && "Не оплачено" } + { payment.status === "HalfPaid" && "Частично оплачено" } + { payment.status === "OverPaid" && ( +
Переплата { numeral(payment.total_amount).format('') } ₽
+ ) } +
+
+ { payment.invoices.map((invoice, invoice_index) => + ( + +

№{ invoice.number } от { moment(invoice.date, "DD-MM-YYYY").format("DD.MM.YYYY") } на сумму { numeral(invoice.total_amount).format('') } ₽

+
+ )) } +
+ +
+ ) + }) } + {/*} +
+
11
+
21.02.2021
+
239 724,05
+
43 079,18
+
Переплата 15 000,00 ₽
+
№18432 от 20/01/2021 на сумму 255 000,00 ₽
+ +
- ) - }) } - {/*} -
-
11
-
21.02.2021
-
239 724,05
-
43 079,18
-
Переплата 15 000,00 ₽
-
№18432 от 20/01/2021 на сумму 255 000,00 ₽
+
+
12
+
21.02.2021
+
239 724,05
+
43 079,18
+
Переплата 10 000,00 ₽
+
№34223 от 21/02/2021 на сумму 229 724,05 ₽
- -
-
-
12
-
21.02.2021
-
239 724,05
-
43 079,18
-
Переплата 10 000,00 ₽
-
№34223 от 21/02/2021 на сумму 229 724,05 ₽
+ +
+
+
13
+
21.02.2021
+
239 724,05
+
43 079,18
+
Оплачено
+
№44911 от 10/03/2021 на сумму 100 000,00 ₽ + №49877 от 21/03/2021 на сумму 139 724,05 ₽
- -
-
-
13
-
21.02.2021
-
239 724,05
-
43 079,18
-
Оплачено
-
№44911 от 10/03/2021 на сумму 100 000,00 ₽ - №49877 от 21/03/2021 на сумму 139 724,05 ₽
+ +
+
+
14
+
21.02.2021
+
239 724,05
+
43 079,18
+
Не оплачено
+
-
- -
-
-
14
-
21.02.2021
-
239 724,05
-
43 079,18
-
Не оплачено
-
-
- - -
- {*/} + +
+ {*/} + + ) }
diff --git a/yarn.lock b/yarn.lock index 012cb08..5a39dda 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3173,6 +3173,11 @@ source-map@^0.6.1: resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g== +spinners-react@^1.0.6: + version "1.0.6" + resolved "https://registry.yarnpkg.com/spinners-react/-/spinners-react-1.0.6.tgz#b7e43d6097fa30107718df718108f3586d50088f" + integrity sha512-5J3sPZ9xogVA3CbLjFD3vTWhcuHb+mKFSBBEm5c8+8JSdPGweLQ6sPU+SD51FwZ2lqHks8DpiLk/lyLUa70oJA== + stacktrace-parser@0.1.10: version "0.1.10" resolved "https://registry.yarnpkg.com/stacktrace-parser/-/stacktrace-parser-0.1.10.tgz#29fb0cae4e0d0b85155879402857a1639eb6051a"