From 017f6e38691c095fb9676e2fc341e1f3a4b341c9 Mon Sep 17 00:00:00 2001 From: rui hildt Date: Sat, 22 Aug 2020 22:49:03 +0200 Subject: [PATCH] Connect Schedule to API and --- src/App.js | 16 ++- src/components/PrivateRoute.js | 3 +- src/components/Schedule/DurationSelector.js | 22 +--- src/helpers/http-common.js | 6 + src/screens/Availability.js | 2 +- src/screens/Schedule.js | 117 +++++++++++++++++--- 6 files changed, 130 insertions(+), 36 deletions(-) diff --git a/src/App.js b/src/App.js index 4cdee34..70d5ce3 100644 --- a/src/App.js +++ b/src/App.js @@ -21,9 +21,15 @@ const existingUser = JSON.parse(localStorage.getItem('user')); export default function App() { const [isAuthenticated, setIsAuthenticated] = useState(!!existingToken); const [currentUser, setCurrentUser] = useState(existingUser || ''); - + const [possibleDates, setPossibleDates] = useState(); - // const [currentMeeting, setCurrentMeeting] = useState(); + const [currentMeeting, setCurrentMeeting] = useState({ + title: '', + description: '', + timezone: '', + duration: 0, + status: 0, + }); const [authToken, setAuthToken] = useState(existingToken || ''); @@ -68,13 +74,15 @@ export default function App() { component={Schedule} possibleDates={possibleDates} setPossibleDates={setPossibleDates} + currentMeeting={currentMeeting} + setCurrentMeeting={setCurrentMeeting} /> - - + diff --git a/src/components/PrivateRoute.js b/src/components/PrivateRoute.js index c2165eb..d4bf53c 100644 --- a/src/components/PrivateRoute.js +++ b/src/components/PrivateRoute.js @@ -8,10 +8,9 @@ const PrivateRoute = ({ component: Component, ...rest }) => { return ( isAuthenticated ? ( - + ) : ( ) diff --git a/src/components/Schedule/DurationSelector.js b/src/components/Schedule/DurationSelector.js index e509755..438e264 100644 --- a/src/components/Schedule/DurationSelector.js +++ b/src/components/Schedule/DurationSelector.js @@ -1,23 +1,13 @@ -import React, { useState } from 'react'; +import React from 'react'; import { InputGroup, Icon } from 'rsuite'; import { durations } from '../../assets/data/durations'; -export default function DurationSelector() { - const [durationIdx, setDurationIdx] = useState(0); - - const handleIncrement = () => { - if (durationIdx <= durations.length - 2) { - setDurationIdx(durationIdx + 1); - } - }; - - const handleDecrement = () => { - if (durationIdx > 0) { - setDurationIdx(durationIdx - 1); - } - }; - +export default function DurationSelector({ + durationIdx, + handleDecrement, + handleIncrement, +}) { return ( diff --git a/src/helpers/http-common.js b/src/helpers/http-common.js index 8510475..4e75a83 100644 --- a/src/helpers/http-common.js +++ b/src/helpers/http-common.js @@ -1,9 +1,15 @@ import axios from 'axios'; import { API_URL } from '../constants'; +const existingToken = JSON.parse(localStorage.getItem('token')); + export const backend = axios.create({ baseURL: API_URL, headers: { 'Content-type': 'application/json', }, }); + +if (existingToken) { + backend.defaults.headers['Authorization'] = existingToken; +} diff --git a/src/screens/Availability.js b/src/screens/Availability.js index 0a36b96..5e994bb 100644 --- a/src/screens/Availability.js +++ b/src/screens/Availability.js @@ -14,7 +14,7 @@ import { NavBar, TimezonePicker, IntervalSelector } from '../components'; import './styles/Availability.less'; import './styles/layout.less'; -export default function Availability() { +export default function Availability({ possibleDates }) { const [availability, setAvailability] = useState([...availabilityList]); const handleClear = () => { diff --git a/src/screens/Schedule.js b/src/screens/Schedule.js index faced2d..a2562d4 100644 --- a/src/screens/Schedule.js +++ b/src/screens/Schedule.js @@ -4,7 +4,6 @@ import { Form, FormControl, ControlLabel, - Input, HelpBlock, Button, ButtonGroup, @@ -12,6 +11,7 @@ import { FormGroup, } from 'rsuite'; import { DateTime } from 'luxon'; +import { useHistory } from 'react-router-dom'; import { NavBar, @@ -20,19 +20,36 @@ import { DurationSelector, SelectedDates, } from '../components'; - +import { backend } from '../helpers/http-common'; +import { durations } from '../assets/data/durations'; import './styles/Schedule.less'; -export default function Schedule() { +export default function Schedule({ + possibleDates, + setPossibleDates, + currentMeeting, + setCurrentMeeting, +}) { const [eventsList, setEventsList] = useState([]); const [datesList, setDatesList] = useState(eventsToDates(eventsList)); + const [durationIdx, setDurationIdx] = useState(0); + const [error, setError] = useState(false); + const history = useHistory(); + + useEffect(() => { + // Push the selected dates up the state tree + // but keep all related logic here + setPossibleDates(datesList) + }, [datesList, setPossibleDates]) + + // EVENTS & DATES useEffect(() => { let updatedDates = eventsToDates(eventsList).sort(); setDatesList(updatedDates); }, [eventsList]); - const handleSelect = (info) => { + const handleSelectDates = (info) => { let updatedEvents = []; let datesList = new Set(); @@ -90,10 +107,6 @@ export default function Schedule() { setEventsList(updatedEvents); }; - const handleClear = () => { - setEventsList([]); - }; - const handleDelete = (date) => { let currentEvent = { start: date.toFormat('yyyy-MM-dd'), @@ -117,6 +130,65 @@ export default function Schedule() { setEventsList(updatedEvents); }; + const handleClear = () => { + setEventsList([]); + }; + + // MEETING + const handleSchedule = () => { + backend + .post('/meetings', currentMeeting) + .then((response) => { + history.push('/availability'); + }) + .catch((error) => { + setError('Failed to add new account.'); + }); + }; + + const handleSelectTimezone = (value, item, event) => { + setCurrentMeeting({ + ...currentMeeting, + timezone: item.timezone, + }); + }; + + const handleClean = (event) => { + setCurrentMeeting({ + ...currentMeeting, + timezone: '', + }); + }; + + const handleChangeMeeting = (value, evt) => { + setCurrentMeeting({ + ...currentMeeting, + [evt.target.name]: value, + }); + }; + + const handleIncrement = () => { + if (durationIdx <= durations.length - 2) { + setDurationIdx(durationIdx + 1); + } + + setCurrentMeeting({ + ...currentMeeting, + duration: durations[durationIdx].duration, + }); + }; + + const handleDecrement = () => { + if (durationIdx > 0) { + setDurationIdx(durationIdx - 1); + } + + setCurrentMeeting({ + ...currentMeeting, + duration: durations[durationIdx].duration, + }); + }; + return ( <> @@ -125,27 +197,42 @@ export default function Schedule() {
Title - + This field is required Description -
Timezone - + Duration - +
@@ -163,9 +250,13 @@ export default function Schedule() { size='lg' block disabled={datesList.length === 0} + onClick={handleSchedule} > Confirm dates + {error && ( + + )}
{datesList.length > 0 && ( @@ -185,7 +276,7 @@ export default function Schedule() { />