diff --git a/restapi/api.ts b/restapi/api.ts index 869d3ae..0d0d000 100644 --- a/restapi/api.ts +++ b/restapi/api.ts @@ -10,36 +10,9 @@ const Rock = require("./models/Rock"); const api:Router = express.Router() const mongoose = require("mongoose"); -interface User { - name: string; - email: string; -} - -//This is not a restapi as it mantains state but it is here for -//simplicity. A database should be used instead. -let users: Array = []; - -api.get( - "/users/list", - async (req: Request, res: Response): Promise => { - return res.status(200).send(users); - } -); - - -api.post( - "/users/add",[ - check('name').isLength({ min: 1 }).trim().escape(), - check('email').isEmail().normalizeEmail(), - ], - async (req: Request, res: Response): Promise => { - let name = req.body.name; - let email = req.body.email; - let user: User = {name:name,email:email} - users.push(user); - return res.sendStatus(200); - } -); +api.get("/users/list",findUsers); + +api.post("/users/add",addUser); api.post("/users/login", loginUser); @@ -66,6 +39,6 @@ api.get("/orders/userList", findOrdersByUserDni); api.post("/orders/add", addOrder); -api.get("/orders/deliveryCosts", getDeliveryCosts) +api.post("/orders/deliveryCosts", getDeliveryCosts) export default api; \ No newline at end of file diff --git a/restapi/controllers/OrderController.ts b/restapi/controllers/OrderController.ts index 98fbf02..c543901 100644 --- a/restapi/controllers/OrderController.ts +++ b/restapi/controllers/OrderController.ts @@ -44,7 +44,7 @@ export const addOrder = async (req:Request, res:Response): Promise => { let string = JSON.stringify(addressCordinates); let objectValue = JSON.parse(string); - + console.log(addressCordinates); let latitudeAddress = objectValue[0].latitude let longitudeAddress = objectValue[0].longitude diff --git a/webapp/src/App.tsx b/webapp/src/App.tsx index 4be576c..46779fd 100644 --- a/webapp/src/App.tsx +++ b/webapp/src/App.tsx @@ -1,5 +1,5 @@ -import { useQuery } from 'react-query'; +//import { useQuery } from 'react-query'; import Box from '@mui/material/Box'; //import Link from '@mui/material/Link'; import Grid from '@mui/material/Grid'; diff --git a/webapp/src/api/api.ts b/webapp/src/api/api.ts index deb13b5..11d7304 100644 --- a/webapp/src/api/api.ts +++ b/webapp/src/api/api.ts @@ -59,4 +59,21 @@ export async function checkUser(email:String,password:String):Promise{ else return false; +} + +export async function getDeliveryCosts(address:String):Promise{ + console.log("BBBBBBBBBBBBBBBBBBBBBBBBB"); + const apiEndPoint= process.env.REACT_APP_API_URI || 'http://localhost:5000/api' + let response = await fetch(apiEndPoint+'/orders/deliveryCosts', { + method: 'POST', + headers: {'Content-Type':'application/json'}, + body: JSON.stringify({'address':address}) + }); + + if (response.status===200){ + return response.json(); + } + else + return -1; + } \ No newline at end of file diff --git a/webapp/src/components/PaymentPage.tsx b/webapp/src/components/PaymentPage.tsx index 53e7f96..7bb379a 100644 --- a/webapp/src/components/PaymentPage.tsx +++ b/webapp/src/components/PaymentPage.tsx @@ -7,8 +7,10 @@ import Button from '@mui/material/Button'; import '../css/PaymentPage.css' import { CardContent, Typography } from '@mui/material'; import { North } from '@mui/icons-material'; -import { useState } from 'react'; +import { useState, useEffect } from 'react'; import { Rock } from '../shared/shareddtypes'; +import { getDeliveryCosts } from '../api/api'; +import { findConfigFile } from 'typescript'; type Props = { @@ -24,21 +26,44 @@ const PaymentPage: React.FC = ({cartContent, setNewCart}) => { const handlePay = () => { if(!isPaid) return; - setNewCart(true); setPaid(false); } + function getAddressContent() { + const memoryCart = localStorage.getItem("address"); + + if (memoryCart) + return memoryCart; + else + return ""; + + } + const [deliveryCosts, setDeliveryCosts] = useState(); + const findDC = async () => { + setDeliveryCosts(await getDeliveryCosts(getAddressContent())); + } + + useEffect(() => { + findDC(); + }, []); + function getFinalDeliveryCosts(){ + if (deliveryCosts){ + return (Number(deliveryCosts.toString()) + Number(getTotalPrice())).toFixed(2); + }else{ + return 0; + } + } return (
-

Your BUY

+

Tu Compra

-

Articles

+

Articulos

{ cartContent.map(Rock => (
@@ -52,16 +77,18 @@ const PaymentPage: React.FC = ({cartContent, setNewCart}) => {
-

Payment summary

-

Cost (no iva): { (getTotalPrice() - (getTotalPrice()*0.21)).toFixed(2) }€

-

Cost: {getTotalPrice().toFixed(2)}€

+

Resumen de Pago

+

Costes (no iva): { (getTotalPrice() - (getTotalPrice()*0.21)).toFixed(2) }€

+

Costes: {getTotalPrice().toFixed(2)}€

+

Costes de Envio: {Number(deliveryCosts).toFixed(2)}€

+

Total: {getFinalDeliveryCosts()}€

+ -

Cost (shipping costs): {(getTotalPrice()+ 12).toFixed(2)}€

{/* Aqui cogemos la dir de los pods y sacamos los costes envio */}
- {isPaid ?

Purchase made

: null} + {isPaid ?

Compra realizada

: null}