From e1d90c698e001451410fcd11e5286be7f9a10b06 Mon Sep 17 00:00:00 2001 From: SlimCandy Date: Tue, 19 Jul 2022 19:45:42 +0300 Subject: [PATCH] Add fetch to createTrip and createUser --- server/index.js | 13 ++- server/json/createTrip.json | 3 + .../components/SPAremoveit/SPAremoveit.tsx | 84 +++++++++++++++---- 3 files changed, 79 insertions(+), 21 deletions(-) create mode 100644 server/json/createTrip.json diff --git a/server/index.js b/server/index.js index 8cbbe6c..1dcc643 100644 --- a/server/index.js +++ b/server/index.js @@ -17,14 +17,21 @@ app.get("/api/Items/", (req, res) => { res.send(require("./json/items.json")); }); -app.post("/api/trip/", (req, res) => { +app.post("/trip/", (req, res) => { res.setHeader("end-time", Date.now()); res.setHeader("Access-Control-Expose-Headers", "end-time"); res.send(require("./json/trip.json")); }); -// /api/User/User/CreateUser -app.post("/api/User/User/CreateUser", (req, res) => { +// /Trip/CreateTrip +app.post("/Trip/CreateTrip", (req, res) => { + res.setHeader("end-time", Date.now()); + res.setHeader("Access-Control-Expose-Headers", "end-time"); + res.send(require("./json/createTrip.json")); +}); + +// /User/User/CreateUser +app.post("/User/User/CreateUser", (req, res) => { res.setHeader("end-time", Date.now()); res.setHeader("Access-Control-Expose-Headers", "end-time"); res.send(require("./json/createUser.json")); diff --git a/server/json/createTrip.json b/server/json/createTrip.json new file mode 100644 index 0000000..d873718 --- /dev/null +++ b/server/json/createTrip.json @@ -0,0 +1,3 @@ +{ + "trip_uid": "3fa85f64-5717-4562-b3fc-2c963f66afa6" +} diff --git a/src/js/view/components/SPAremoveit/SPAremoveit.tsx b/src/js/view/components/SPAremoveit/SPAremoveit.tsx index e1d404f..0978014 100644 --- a/src/js/view/components/SPAremoveit/SPAremoveit.tsx +++ b/src/js/view/components/SPAremoveit/SPAremoveit.tsx @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/naming-convention */ import React, { useEffect, useState } from "react"; import { localStorageUsernameKey, @@ -8,9 +9,12 @@ import { pushLocalStorage, } from "../../../utils/localStorage"; -const serverUrl = process.env.SERVER || "http://localhost:3001"; +const SERVER_URL = process.env.REACT_APP_SERVER || "http://localhost:3001"; const SPAremoveit = () => { + // global store + const [_userUid, setUserUid] = useState(""); + const [_tripUid, setTripUid] = useState(""); // 1. Username const [username, setUsername] = useState(""); const onUsernameChange = (event: React.ChangeEvent) => { @@ -24,7 +28,7 @@ const SPAremoveit = () => { username: string; user_uid: string; } - const response = await fetch(`${serverUrl}/api/User/User/CreateUser`, { + const response = await fetch(`${SERVER_URL}/User/User/CreateUser`, { method: "POST", headers: { "Content-Type": "application/json", @@ -43,12 +47,16 @@ const SPAremoveit = () => { user_uid: userUid, }; + setUserUid(userUid); pushLocalStorage( localStorageUsernameKey, JSON.stringify(localStorageUserNameObj) ) - .then(() => console.log("username sucessfully saved")) - .catch(console.error); + .then( + () => {}, + () => {} + ) + .catch(() => {}); } }; @@ -64,7 +72,7 @@ const SPAremoveit = () => { ) setTripIds(JSON.parse(localStorageString) as string[]); }) - .catch(console.error); + .catch(() => {}); }, []); const onNewTripClick = (event: React.MouseEvent) => { event.preventDefault(); @@ -86,10 +94,41 @@ const SPAremoveit = () => { const onNewTripFormDescriptionChange = ( event: React.ChangeEvent ) => setNewTripFormDescription(event.target.value); - const onNewTripFormSubmit = (event: React.FormEvent) => { - event.preventDefault(); - // save to local storage - setPeopleFormOpen(true); + const onNewTripFormSubmit = async () => { + const response = await fetch( + `${SERVER_URL}/Trip/CreateTrip?user_uid=${_userUid}`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + title: newTripFormName, + description: newTripFormDescription, + start: newTripFormDates, + end: newTripFormDates, + }), + } + ); + + interface INewTripResponse { + trip_uid: string; + } + if (response.ok) { + const json: INewTripResponse = + (await response.json()) as INewTripResponse; + const { trip_uid: tripUid } = json; + + setPeopleFormOpen(true); + setTripUid(tripUid); + + pushLocalStorage("localStorageTripIdKey", JSON.stringify(_tripUid)) + .then( + () => {}, + () => {} + ) + .catch(() => {}); + } }; // 4. Форма нового человека @@ -105,7 +144,7 @@ const SPAremoveit = () => { const onFormSubmit = (event: React.MouseEvent) => { event.preventDefault(); - fetch(`${serverUrl}/api/trip/`, { + fetch(`${SERVER_URL}/trip/`, { method: "POST", body: JSON.stringify({ name: newTripFormName, @@ -114,8 +153,8 @@ const SPAremoveit = () => { people, }), }) - .then(console.log) - .catch(console.error) + .then(() => {}) + .catch(() => {}) .finally(() => setSuccessPageOpen(true)); // тут и ссылка на мероприятие будет }; @@ -147,7 +186,9 @@ const SPAremoveit = () => { onSubmit={(event: React.FormEvent) => { event.preventDefault(); - onUsernameSubmit().then(console.log).catch(console.error); + onUsernameSubmit() + .then(() => {}) + .catch(() => {}); }} > @@ -177,13 +218,20 @@ const SPAremoveit = () => {

У вас ещё нет мероприятий

)} {newTripFormOpen ? (
  • 3. Форма нового мероприятия

    -
    + ) => { + event.preventDefault(); + onNewTripFormSubmit() + .then(() => {}) + .catch(() => {}); + }} + >
  • @@ -245,7 +293,7 @@ const SPAremoveit = () => { ) : (