From 07b44075fa97ca257cc3141fca9d0b5f50651265 Mon Sep 17 00:00:00 2001 From: sanaya Date: Fri, 4 Feb 2022 17:40:23 +0530 Subject: [PATCH] integrated apis --- package.json | 2 + src/auth.js | 17 ++++ src/index.js | 12 +++ src/views/AirwaysBill/AirwaysBill.js | 22 +++++- src/views/Courier/AddCourier.js | 53 +++++++++++-- src/views/Courier/Courier.js | 21 ++++- src/views/Vendor/AddVendor.js | 113 ++++++++++++++++++++++----- src/views/Vendor/Vendor.js | 24 +++++- src/views/pages/login/Login.js | 43 ++++++++-- 9 files changed, 274 insertions(+), 33 deletions(-) create mode 100644 src/auth.js diff --git a/package.json b/package.json index 7c01684..4bddee7 100644 --- a/package.json +++ b/package.json @@ -38,9 +38,11 @@ "@coreui/react-chartjs": "^2.0.0", "@coreui/utils": "^1.3.1", "@wojtekmaj/enzyme-adapter-react-17": "^0.6.5", + "axios": "^0.25.0", "chart.js": "^3.6.0", "classnames": "^2.3.1", "core-js": "^3.19.1", + "country-state-city": "^3.0.1", "enzyme": "^3.11.0", "prop-types": "^15.7.2", "react": "^17.0.2", diff --git a/src/auth.js b/src/auth.js new file mode 100644 index 0000000..19e6699 --- /dev/null +++ b/src/auth.js @@ -0,0 +1,17 @@ +export const isAutheticated = () => { + if (typeof window == "undefined") { + return true; + } + if (localStorage.getItem("auth")) { + + return JSON.parse(localStorage.getItem("auth")); + } else { + return false; + } +}; + +export const signout = () => { + localStorage.removeItem("auth"); + + return true; +}; diff --git a/src/index.js b/src/index.js index 8aa227b..8fc244c 100644 --- a/src/index.js +++ b/src/index.js @@ -6,6 +6,18 @@ import App from './App' import * as serviceWorker from './serviceWorker' import { Provider } from 'react-redux' import store from './store' +import axios from 'axios' + +const setupAxios = () => { + axios.defaults.baseURL = "https://api-courier-vendor.herokuapp.com/" + axios.defaults.headers = { + 'Cache-Control': 'no-cache,no-store', + 'Pragma': 'no-cache', + 'Expires': '0', + }; +}; + +setupAxios(); ReactDOM.render( diff --git a/src/views/AirwaysBill/AirwaysBill.js b/src/views/AirwaysBill/AirwaysBill.js index 2a4bc24..cf8bd10 100644 --- a/src/views/AirwaysBill/AirwaysBill.js +++ b/src/views/AirwaysBill/AirwaysBill.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react'; import { CAvatar, CButton, @@ -19,8 +19,28 @@ import { CTableRow, } from '@coreui/react' import { Link } from 'react-router-dom'; +import { isAutheticated } from 'src/auth'; +import axios from 'axios'; const AirwaysBill = () => { + const { token } = isAutheticated(); + console.log(token); + + useEffect(() => { + const getData = async () => { + const res = await axios.get('/api/vendor/view', { + headers: { + "Access-Control-Allow-Origin": "*", + "Content-type": "Application/json", + "Authorization": `Bearer ${token}` + } + }); + console.log(res.data); + } + getData(); + + }, []); + return
+ Upload Spreadsheet diff --git a/src/views/Courier/AddCourier.js b/src/views/Courier/AddCourier.js index 72329fc..e9c1eb1 100644 --- a/src/views/Courier/AddCourier.js +++ b/src/views/Courier/AddCourier.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import { CButton, CCard, @@ -14,8 +14,50 @@ import { } from '@coreui/react' import CIcon from '@coreui/icons-react' import { cil3d } from '@coreui/icons' +import { useState } from 'react'; +import axios from 'axios'; +import { isAutheticated } from 'src/auth'; const AddCourier = () => { + const [id, setId] = useState(0); + const [date, setDate] = useState('') + const [courier, setCourier] = useState('') + const { token } = isAutheticated(); + + useEffect(() => { + const getDate = () => { + let today = new Date(); + let dd = String(today.getDate()).padStart(2, '0'); + let mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0! + let yyyy = today.getFullYear(); + + today = mm + '/' + dd + '/' + yyyy; + return today + } + + + const generateCode = () => { + setId(Math.round(Math.random() * 1000000000)) + } + generateCode() + setDate(getDate()) + }, []) + const handleChange = (e) => { + const { name, value } = e.target; + setCourier(value) + } + const handleClick = async () => { + let res = await axios.post('/api/courier/add', { name: courier, addedOn: date, UID: id }, { + headers: { + "Authorization": `Bearer ${token}` + } + }) + if (res) { + console.log(res.data); + } + } + + return
@@ -25,12 +67,12 @@ const AddCourier = () => {

Fill the fields and submit to add a new vendor

Unique ID:
-
5324756898
+
{id}

(auto-generated)

Added On:
-
5324756898
+
{date}

(auto-generated)

@@ -41,10 +83,11 @@ const AddCourier = () => { - + Submit diff --git a/src/views/Courier/Courier.js b/src/views/Courier/Courier.js index 0765986..4faaca8 100644 --- a/src/views/Courier/Courier.js +++ b/src/views/Courier/Courier.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import { CAvatar, CButton, @@ -19,8 +19,27 @@ import { CTableRow, } from '@coreui/react' import { Link } from 'react-router-dom'; +import { isAutheticated } from 'src/auth'; +import axios from 'axios'; const Courier = () => { + const { token } = isAutheticated(); + console.log(token); + + useEffect(() => { + const getData = async () => { + const res = await axios.get('/api/vendor/view', { + headers: { + "Access-Control-Allow-Origin": "*", + "Content-type": "Application/json", + "Authorization": `Bearer ${token}` + } + }); + console.log(res); + } + getData(); + + }, []); return
+Add New diff --git a/src/views/Vendor/AddVendor.js b/src/views/Vendor/AddVendor.js index b68530f..b858fcb 100644 --- a/src/views/Vendor/AddVendor.js +++ b/src/views/Vendor/AddVendor.js @@ -8,14 +8,72 @@ import { CContainer, CForm, CFormInput, + CFormSelect, CInputGroup, CInputGroupText, CRow, } from '@coreui/react' import CIcon from '@coreui/icons-react' +import { Country, State, City } from 'country-state-city'; import { cilGlobeAlt, cilLocationPin, cilLockLocked, cilUser } from '@coreui/icons' import { Link } from 'react-router-dom'; +import { useState } from 'react'; +import { useEffect } from 'react'; +import axios from 'axios'; +import { getAllStates, getStatesOfCountry, getCitiesOfCountry } from 'country-state-city/dist/lib/state'; +import { isAutheticated } from 'src/auth'; + + const AddVendor = () => { + const [vendor, setVendor] = useState({ + vendor_name: '', + city: '', + state: 'Andhra Pradesh', + country: 'India', + address_1: '', + address_2: '', + }) + const { token } = isAutheticated(); + const [states, setStates] = useState([]); + const [cities, setCities] = useState([]); + const [code, setCode] = useState(0); + const [countryCode, setCountryCode] = useState('IN') + const [stateCode, setStateCode] = useState('AP') + const countries = Country.getAllCountries(); + const allStates = State.getAllStates(); + // const Code = Math.round(Math.random() * 1000000000); + const handleChange = (e) => (event) => { + setVendor({ ...vendor, [e]: event.target.value }); + }; + + useEffect(() => { + const generateCode = () => { + setCode(Math.round(Math.random() * 1000000000)) + } + generateCode() + }, []) + useEffect(() => { + const ccode = countries.find(item => item.name === vendor.country) + const scode = allStates.find(item => item.name === vendor.state) + console.log(ccode.isoCode + ":" + scode.isoCode); + console.log(vendor.country, vendor.state); + setCountryCode(ccode.isoCode) + setStateCode(scode.isoCode) + setStates(prev => State.getStatesOfCountry(countryCode)) + setCities(prev => City.getCitiesOfState(countryCode, stateCode)) + }, [vendor.country, vendor.state, countryCode, stateCode]); + + const handleClick = async () => { + let res = await axios.post('/api/vendor/add', vendor, { + headers: { + "Authorization": `Bearer ${token}` + } + }) + if (res) { + console.log(res.data); + } + } + return
@@ -25,14 +83,14 @@ const AddVendor = () => {

Fill the fields and submit to add a new vendor

Code:

-
5324756898
+
{code}

(auto-generated)

- + @@ -41,35 +99,52 @@ const AddVendor = () => { - - - + + { + countries.map((item) => + + ) + } + + + { + states.map((item) => + + ) + } + + + { + cities.map((item) => + + ) + } + - + Submit diff --git a/src/views/Vendor/Vendor.js b/src/views/Vendor/Vendor.js index 2622957..016bf80 100644 --- a/src/views/Vendor/Vendor.js +++ b/src/views/Vendor/Vendor.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import { CAvatar, CButton, @@ -19,8 +19,30 @@ import { CTableRow, } from '@coreui/react' import { Link } from 'react-router-dom'; +import axios from 'axios'; +import { isAutheticated } from '../../auth'; const Vendor = () => { + const { token } = isAutheticated(); + console.log(token); + + useEffect(() => { + const getData = async () => { + const res = await axios.get('/api/vendor/view', { + headers: { + "Access-Control-Allow-Origin": "*", + "Content-type": "Application/json", + "Authorization": `Bearer ${token}` + } + }); + console.log(res); + } + getData(); + + }, []); + + + return
+Add New Vendor diff --git a/src/views/pages/login/Login.js b/src/views/pages/login/Login.js index 6ee1d65..8c208c1 100644 --- a/src/views/pages/login/Login.js +++ b/src/views/pages/login/Login.js @@ -15,8 +15,38 @@ import { } from '@coreui/react' import CIcon from '@coreui/icons-react' import { cilLockLocked, cilUser } from '@coreui/icons' +import { useState } from 'react' +import axios from 'axios' +import { useHistory } from 'react-router-dom' const Login = () => { + const [auth, setAuth] = useState({ + email: "", + password: "" + }); + const history = useHistory(); + + const handleChange = (e) => (event) => { + setAuth({ ...auth, [e]: event.target.value }); + }; + + const Login = async () => { + const res = await axios.post("/admin-signin", auth); + if (res.data.status == "ok") { + localStorage.setItem("auth", JSON.stringify({ + // user: res.data.user, + token: res.data.token, + })); + history.push('/dashboard') + + } + else { + if (res.data.status === "blocked") + alert(res.data.message) + else + alert("Invalid Credentials"); + } + } return (
@@ -32,7 +62,7 @@ const Login = () => { - + @@ -40,16 +70,17 @@ const Login = () => { - - - Login - - + + + Login + +