From 4137c64f7f0893f566c76b9d1335f00f86acb1b3 Mon Sep 17 00:00:00 2001 From: merelendor Date: Tue, 23 Nov 2021 06:47:27 +0300 Subject: [PATCH] pre-api --- actions/authActions.js | 101 +++++++++++++++++++++++- actions/contractsActions.js | 106 ++++++++++++++++++++++++++ actions/index.js | 3 +- constants/actionTypes.js | 4 +- package.json | 1 + pages/api/contracts.js | 57 +++++++++++++- pages/components/FormRequest/index.js | 32 ++++---- pages/index.js | 17 +++++ pages/login.js | 46 ++++++----- reducers/companyReducer.js | 2 + reducers/initialState.js | 6 ++ reducers/userReducer.js | 24 ++++++ store/index.js | 4 +- yarn.lock | 85 +++++++++++++++++++++ 14 files changed, 445 insertions(+), 43 deletions(-) create mode 100644 actions/contractsActions.js create mode 100644 reducers/userReducer.js diff --git a/actions/authActions.js b/actions/authActions.js index 877f0a3..e8f6632 100644 --- a/actions/authActions.js +++ b/actions/authActions.js @@ -22,10 +22,107 @@ if(process.browser) }; } -export const sendLoginForm = (fields) => +export const sendLoginFormEmail = ({ email, password, dispatch }) => { return new Promise((resolve, reject) => { + axios.post(`${ process.env.NEXT_PUBLIC_API_HOST }/api/account/auth/email/`, { email, password }) + .then((response) => + { + console.log("sendLoginFormEmail RESPONSE"); + console.log(response.data); + + if(response.data.status === "success") + { + 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); + + 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('/'); + } + else + { + reject(); + } + }) + .catch((error) => + { + console.log("error"); + console.error(error); + + reject(); + }); + + /* + if(fields.username === "test@test.com" && fields.password === "test") + { + const cookies = new Cookies(); + cookies.set('jwt', 1, new Date(moment().add(1, 'day').toDate())); + } + Router.push('/'); + */ + + /* + axios.post(`${ process.env.NEXT_PUBLIC_API_HOST }/api/forms/terms/`, fields) + .then((response) => + { + console.log("sendTermsForm RESPONSE"); + console.log(response.data); + + if(response.data.status) + { + resolve(); + } + else + { + reject(); + } + }) + .catch((error) => + { + console.log("error"); + console.error(error); + + reject(); + }); + */ + }); +} + +export const sendLoginFormPhone = (fields) => +{ + return new Promise((resolve, reject) => + { + axios.post(`${ process.env.NEXT_PUBLIC_API_HOST }/api/account/auth/`, fields) + .then((response) => + { + console.log("sendTermsForm RESPONSE"); + console.log(response.data); + + if(response.data.status) + { + resolve(); + } + else + { + reject(); + } + }) + .catch((error) => + { + console.log("error"); + console.error(error); + + reject(); + }); + if(fields.username === "test@test.com" && fields.password === "test") { const cookies = new Cookies(); @@ -34,7 +131,7 @@ export const sendLoginForm = (fields) => Router.push('/'); /* - axios.post(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/forms/terms/`, fields) + axios.post(`${ process.env.NEXT_PUBLIC_API_HOST }/api/forms/terms/`, fields) .then((response) => { console.log("sendTermsForm RESPONSE"); diff --git a/actions/contractsActions.js b/actions/contractsActions.js new file mode 100644 index 0000000..dc4088b --- /dev/null +++ b/actions/contractsActions.js @@ -0,0 +1,106 @@ +import axios from 'axios'; +import { Cookies } from 'react-cookie'; +import Router from 'next/router'; +import moment from 'moment'; + +import * as actionTypes from '../constants/actionTypes'; +import * as currentState from '../reducers/initialState'; + +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 getContractsList = ({ query, date_from, date_to }) => +{ + return new Promise((resolve, reject) => + { + let cookies = new Cookies(); + let cookies_list = cookies.getAll(); + console.log(cookies_list); + + axios.post(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/contracts`, {}, { + //headers: { + // "Authorization": `Bearer ${ cookies_list.jwt }`, + //}, + withCredentials: true, + }); + + resolve(); + + /* + axios.post(`${ process.env.NEXT_PUBLIC_API_HOST }/api/account/contracts/`, { }, { + headers: + { + Authorization: `Bearer ${ }`, + } + }) + .then((response) => + { + console.log("getContractsList RESPONSE"); + console.log(response.data); + + if(response.data.status === "success") + { + resolve(); + } + else + { + reject(); + } + }) + .catch((error) => + { + console.log("error"); + console.error(error); + + reject(); + }); + */ + + /* + if(fields.username === "test@test.com" && fields.password === "test") + { + const cookies = new Cookies(); + cookies.set('jwt', 1, new Date(moment().add(1, 'day').toDate())); + } + Router.push('/'); + */ + + /* + axios.post(`${ process.env.NEXT_PUBLIC_API_HOST }/api/forms/terms/`, fields) + .then((response) => + { + console.log("sendTermsForm RESPONSE"); + console.log(response.data); + + if(response.data.status) + { + resolve(); + } + else + { + reject(); + } + }) + .catch((error) => + { + console.log("error"); + console.error(error); + + reject(); + }); + */ + }); +} \ No newline at end of file diff --git a/actions/index.js b/actions/index.js index a030b9c..b7a5693 100644 --- a/actions/index.js +++ b/actions/index.js @@ -1 +1,2 @@ -export * from './authActions'; \ No newline at end of file +export * from './authActions'; +export * from './contractsActions'; \ No newline at end of file diff --git a/constants/actionTypes.js b/constants/actionTypes.js index 21bab81..e82c81e 100644 --- a/constants/actionTypes.js +++ b/constants/actionTypes.js @@ -1,3 +1,5 @@ export const AUTH = 'AUTH'; +export const USER = 'USER'; export const COMPANY = 'COMPANY'; -export const CONTRACTS = 'CONTRACTS'; \ No newline at end of file +export const CONTRACTS = 'CONTRACTS'; +export const CALENDAR = 'CALENDAR'; \ No newline at end of file diff --git a/package.json b/package.json index 92f69c4..225f0e2 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dependencies": { "axios": "^0.24.0", "cookie": "^0.4.1", + "jsonwebtoken": "^8.5.1", "moment": "^2.29.1", "next": "11.1.2", "next-fonts": "^1.5.1", diff --git a/pages/api/contracts.js b/pages/api/contracts.js index 1520986..a73e5c5 100644 --- a/pages/api/contracts.js +++ b/pages/api/contracts.js @@ -1,5 +1,58 @@ // 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'; -export default function handler(req, res) { - res.status(200).json({ name: 'John Doe' }) +export default function handler(req, res) +{ + if(req.headers.cookie !== undefined) + { + const cookies = cookie.parse(req.headers?.cookie ? req.headers?.cookie : ""); + + 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(`${ process.env.CRM_API_HOST }/lk/Account/GetContracts/`); + + try + { + axios.get(`${ process.env.CRM_API_HOST }/lk/Account/GetContracts/`, { + params: client_jwt_decoded, + headers: { + "Authorization": `Bearer ${ crm_jwt }`, + }, + withCredentials: true, + }) + .then((crm_response) => + { + console.log("crm_response"); + console.log(crm_response); + }) + .catch((error) => + { + console.error(error); + }); + } + catch(e) + { + console.error(e); + } + + res.status(200).json({ name: 'John Doe' }); + } + else + { + res.status(403); + } + } } \ No newline at end of file diff --git a/pages/components/FormRequest/index.js b/pages/components/FormRequest/index.js index f2a50f2..fe61d93 100644 --- a/pages/components/FormRequest/index.js +++ b/pages/components/FormRequest/index.js @@ -11,30 +11,30 @@ export default class FormRequest extends React.Component { return (
-
-

Купить в лизинг?

-
-
+
+

Купить в лизинг?

+
+

Напишите на info@evoleasing.ru или заполните форму

-
- +
+ {} }/>
-
- +
+ {} }/>
-
- +
+ {} }/>
-
- +
+ {} }/>
-
- - +
+ {} }/> +
- +
diff --git a/pages/index.js b/pages/index.js index 7a8f9b2..ab7928f 100644 --- a/pages/index.js +++ b/pages/index.js @@ -10,12 +10,29 @@ import { reduxWrapper } from '../store'; import Header from './components/Header'; import Footer from './components/Footer'; +import { getContractsList } from '../actions'; class IndexPage extends React.Component { constructor(props) { super(props); + this.state = { + contracts: null, + }; + } + getContractsList + + static getDerivedStateFromProps(nextProps, prevState) + { + return { + contracts: nextProps.contracts, + }; + } + + componentDidMount() + { + getContractsList({}).then(() => {}).catch(() => {}); } render() diff --git a/pages/login.js b/pages/login.js index f24df02..93e74e8 100644 --- a/pages/login.js +++ b/pages/login.js @@ -10,7 +10,7 @@ import Footer from './components/Footer'; import MainHeader from "./components/MainHeader"; import FormRequest from "./components/FormRequest"; -import { sendLoginForm } from '../actions'; +import { sendLoginFormEmail, sendLoginFormPhone } from '../actions'; class LoginPage extends React.Component { @@ -18,10 +18,18 @@ class LoginPage extends React.Component { super(props); this.state = { - username: "", + email: "", password: "", phone: "", tab: "email", + company: {}, + }; + } + + static getDerivedStateFromProps(nextProps, prevState) + { + return { + company: nextProps.company, }; } @@ -29,8 +37,8 @@ class LoginPage extends React.Component { event.preventDefault(); - const { username, password, } = this.state; - sendLoginForm({ username, password }); + const { email, password, } = this.state; + sendLoginFormEmail({ email, password, dispatch: this.props.dispatch }); } _handle_onPhoneSubmit = (event) => @@ -38,7 +46,7 @@ class LoginPage extends React.Component event.preventDefault(); const { phone, } = this.state; - sendLoginForm({ phone }); + sendLoginFormPhone({ phone }); } _handle_onChangeTab = (tab) => @@ -48,7 +56,7 @@ class LoginPage extends React.Component render() { - const { username, password, phone, tab } = this.state; + const { email, password, phone, tab } = this.state; return ( @@ -62,10 +70,10 @@ class LoginPage extends React.Component
-
-
-

Личный кабинет

-
@@ -105,9 +113,7 @@ class LoginPage extends React.Component function mapStateToProps(state, ownProps) { return { - contracts: state.contracts.list, - page: state.contracts.page, - pages: state.contracts.pages, + company: state.company, } } diff --git a/reducers/companyReducer.js b/reducers/companyReducer.js index dc39e6e..8ccf2d5 100644 --- a/reducers/companyReducer.js +++ b/reducers/companyReducer.js @@ -7,6 +7,7 @@ const companyReducer = (state = initialState.company, action) => { switch (action.type) { + /* case HYDRATE: { return { @@ -14,6 +15,7 @@ const companyReducer = (state = initialState.company, action) => ...action.payload.company, }; } + */ case actionTypes.COMPANY: { diff --git a/reducers/initialState.js b/reducers/initialState.js index 904a98b..be1241f 100644 --- a/reducers/initialState.js +++ b/reducers/initialState.js @@ -2,6 +2,12 @@ export const defaultState = { auth: { logged: false, }, + user: { + name: "", + lastname: "", + secondname: "", + phone: "", + }, company: { title: "ООО \"Тест\"", inn: 770011223344, diff --git a/reducers/userReducer.js b/reducers/userReducer.js new file mode 100644 index 0000000..6ed1b9c --- /dev/null +++ b/reducers/userReducer.js @@ -0,0 +1,24 @@ +import { HYDRATE } from 'next-redux-wrapper'; + +import * as actionTypes from '../constants/actionTypes'; +import initialState from "./initialState"; + +const userReducer = (state = initialState.user, action) => +{ + switch (action.type) + { + case actionTypes.USER: + { + return { + ...state, + ...action.data, + }; + } + + default: { + return state; + } + } +}; + +export default userReducer; \ No newline at end of file diff --git a/store/index.js b/store/index.js index 802e098..a2eeb84 100644 --- a/store/index.js +++ b/store/index.js @@ -2,11 +2,13 @@ import { createStore, combineReducers } from 'redux'; import { createWrapper } from 'next-redux-wrapper'; import authReducer from '../reducers/authReducer'; +import userReducer from '../reducers/userReducer'; import companyReducer from '../reducers/companyReducer'; import contractsReducer from '../reducers/contractsReducer'; const combinedReducer = combineReducers({ auth: authReducer, + user: userReducer, company: companyReducer, contracts: contractsReducer, }); @@ -26,7 +28,7 @@ const makeStore = (context) => const persistConfig = { key: 'nextjs', - whitelist: [ 'auth', 'company', ], + whitelist: [ 'auth', 'user', 'company', ], storage }; diff --git a/yarn.lock b/yarn.lock index 59042ee..8ca7643 100644 --- a/yarn.lock +++ b/yarn.lock @@ -617,6 +617,11 @@ browserslist@4.16.6: escalade "^3.1.1" node-releases "^1.1.71" +buffer-equal-constant-time@1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz#f8e71132f7ffe6e01a5c9697a4c6f3e48d5cc819" + integrity sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk= + buffer-xor@^1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/buffer-xor/-/buffer-xor-1.0.3.tgz#26e61ed1422fb70dd42e6e36729ed51d855fe8d9" @@ -977,6 +982,13 @@ domain-browser@^1.1.1: resolved "https://registry.yarnpkg.com/domain-browser/-/domain-browser-1.2.0.tgz#3d31f50191a6749dd1375a7f522e823d42e54eda" integrity sha512-jnjyiM6eRyZl2H+W8Q/zLMA481hzi0eszAaBUzIVnmYVDBbnLxVNnfu1HgEBvCbL+71FrxMl3E6lpKH7Ge3OXA== +ecdsa-sig-formatter@1.0.11: + version "1.0.11" + resolved "https://registry.yarnpkg.com/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz#ae0f0fa2d85045ef14a817daa3ce9acd0489e5bf" + integrity sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ== + dependencies: + safe-buffer "^5.0.1" + electron-to-chromium@^1.3.723: version "1.3.866" resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.866.tgz#d446338f5ad6948b27a50739760e7b0b5cc5032f" @@ -1903,6 +1915,22 @@ json5@^2.1.2: dependencies: minimist "^1.2.5" +jsonwebtoken@^8.5.1: + version "8.5.1" + resolved "https://registry.yarnpkg.com/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz#00e71e0b8df54c2121a1f26137df2280673bcc0d" + integrity sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w== + dependencies: + jws "^3.2.2" + lodash.includes "^4.3.0" + lodash.isboolean "^3.0.3" + lodash.isinteger "^4.0.4" + lodash.isnumber "^3.0.3" + lodash.isplainobject "^4.0.6" + lodash.isstring "^4.0.1" + lodash.once "^4.0.0" + ms "^2.1.1" + semver "^5.6.0" + "jsx-ast-utils@^2.4.1 || ^3.0.0", jsx-ast-utils@^3.1.0: version "3.2.1" resolved "https://registry.yarnpkg.com/jsx-ast-utils/-/jsx-ast-utils-3.2.1.tgz#720b97bfe7d901b927d87c3773637ae8ea48781b" @@ -1911,6 +1939,23 @@ json5@^2.1.2: array-includes "^3.1.3" object.assign "^4.1.2" +jwa@^1.4.1: + version "1.4.1" + resolved "https://registry.yarnpkg.com/jwa/-/jwa-1.4.1.tgz#743c32985cb9e98655530d53641b66c8645b039a" + integrity sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA== + dependencies: + buffer-equal-constant-time "1.0.1" + ecdsa-sig-formatter "1.0.11" + safe-buffer "^5.0.1" + +jws@^3.2.2: + version "3.2.2" + resolved "https://registry.yarnpkg.com/jws/-/jws-3.2.2.tgz#001099f3639468c9414000e99995fa52fb478304" + integrity sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA== + dependencies: + jwa "^1.4.1" + safe-buffer "^5.0.1" + kind-of@^6.0.2: version "6.0.3" resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-6.0.3.tgz#07c05034a6c349fa06e24fa35aa76db4580ce4dd" @@ -1969,11 +2014,46 @@ locate-path@^5.0.0: dependencies: p-locate "^4.1.0" +lodash.includes@^4.3.0: + version "4.3.0" + resolved "https://registry.yarnpkg.com/lodash.includes/-/lodash.includes-4.3.0.tgz#60bb98a87cb923c68ca1e51325483314849f553f" + integrity sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8= + +lodash.isboolean@^3.0.3: + version "3.0.3" + resolved "https://registry.yarnpkg.com/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz#6c2e171db2a257cd96802fd43b01b20d5f5870f6" + integrity sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY= + +lodash.isinteger@^4.0.4: + version "4.0.4" + resolved "https://registry.yarnpkg.com/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz#619c0af3d03f8b04c31f5882840b77b11cd68343" + integrity sha1-YZwK89A/iwTDH1iChAt3sRzWg0M= + +lodash.isnumber@^3.0.3: + version "3.0.3" + resolved "https://registry.yarnpkg.com/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz#3ce76810c5928d03352301ac287317f11c0b1ffc" + integrity sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w= + +lodash.isplainobject@^4.0.6: + version "4.0.6" + resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb" + integrity sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs= + +lodash.isstring@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451" + integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE= + lodash.merge@^4.6.2: version "4.6.2" resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a" integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ== +lodash.once@^4.0.0: + version "4.1.1" + resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac" + integrity sha1-DdOXEhPHxW34gJd9UEyI+0cal6w= + lodash.sortby@^4.7.0: version "4.7.0" resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438" @@ -2802,6 +2882,11 @@ schema-utils@^3.0.0: ajv "^6.12.5" ajv-keywords "^3.5.2" +semver@^5.6.0: + version "5.7.1" + resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7" + integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ== + semver@^6.0.0, semver@^6.3.0: version "6.3.0" resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"