Fix route parameters

pull/2/head
Bailey Pumfleet 2021-04-07 21:37:41 +01:00
parent f55f2f6321
commit 5bedd1f8b2
2 changed files with 8 additions and 3 deletions

View File

@ -2,6 +2,7 @@ import {useEffect, useState} from 'react'
import Head from 'next/head' import Head from 'next/head'
import Link from 'next/link' import Link from 'next/link'
import prisma from '../../lib/prisma' import prisma from '../../lib/prisma'
import { useRouter } from 'next/router'
const dayjs = require('dayjs') const dayjs = require('dayjs')
const isSameOrBefore = require('dayjs/plugin/isSameOrBefore') const isSameOrBefore = require('dayjs/plugin/isSameOrBefore')
dayjs.extend(isSameOrBefore) dayjs.extend(isSameOrBefore)
@ -13,6 +14,10 @@ export default function Type(props) {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [busy, setBusy] = useState([]); const [busy, setBusy] = useState([]);
// Get router variables
const router = useRouter()
const { user } = router.query
// Handle month changes // Handle month changes
const incrementMonth = () => { const incrementMonth = () => {
setSelectedMonth(selectedMonth + 1) setSelectedMonth(selectedMonth + 1)
@ -38,7 +43,7 @@ export default function Type(props) {
// Handle date change // Handle date change
useEffect(async () => { useEffect(async () => {
setLoading(true); setLoading(true);
const res = await fetch('http://localhost:3000/api/availability/bailey?date=' + dayjs(selectedDate).format("YYYY-MM-DD")) const res = await fetch('/api/availability/' + user + '?date=' + dayjs(selectedDate).format("YYYY-MM-DD"))
const data = await res.json() const data = await res.json()
setBusy(data.primary.busy) setBusy(data.primary.busy)
setLoading(false) setLoading(false)

View File

@ -6,12 +6,12 @@ const dayjs = require('dayjs')
export default function Book(props) { export default function Book(props) {
const router = useRouter() const router = useRouter()
const { date } = router.query const { date, user } = router.query
const bookingHandler = event => { const bookingHandler = event => {
event.preventDefault() event.preventDefault()
const res = fetch( const res = fetch(
'http://localhost:3000/api/book/bailey', 'http://localhost:3000/api/book/' + user,
{ {
body: JSON.stringify({ body: JSON.stringify({
start: dayjs(date).format(), start: dayjs(date).format(),