Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions app/staffing/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,26 @@ import {
import { Input } from "@/components/ui/input";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Response } from "@/models/response";
import { StaffingRequest as StaffingRequestType } from "@/models/staffingRequest";
import { zodResolver } from "@hookform/resolvers/zod";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import { useContext, useEffect, useState } from "react";
import { Row, Col, Button, Spinner } from "react-bootstrap";
import DatePicker from "react-datepicker";
import { useForm } from "react-hook-form";
import { toast } from "react-toastify";
import { z } from "zod";

import { AuthContext } from "../Providers";


const formSchema = z.object({
email: z.string().email(),
organization: z.string().nonempty(),
estimatedPilots: z.number().positive(),
start: z.date(),
start: z.date().refine((date) => date > new Date(), {
message: "Start date must be in the future",
}),
duration: z.string().nonempty(),
});

Expand All @@ -43,13 +49,15 @@ const generateDurationOptions = () => {

export default function StaffingRequest() {

const authContext = useContext(AuthContext);
const router = useRouter();

const [loading, setLoading] = useState(true);

const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
email: "",
organization: "",
estimatedPilots: 0,
start: new Date(),
Expand All @@ -64,13 +72,22 @@ export default function StaffingRequest() {

async function onSubmit(values: z.infer<typeof formSchema>) {
setLoading(true);
const requestBody: StaffingRequestType = {
cid: authContext?.user?.cid as number,
fullName: authContext?.user?.fullName as string,
email: values.email,
organization: values.organization,
estimatedPilots: values.estimatedPilots,
start: values.start,
duration: values.duration,
};
await fetch(`${process.env.NEXT_PUBLIC_API_URL}/staffingrequests`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${localStorage.getItem("accessToken")}`,
},
body: JSON.stringify(values),
body: JSON.stringify(requestBody),
}).then(async (response) => {
if (!response.ok) {
const error = await response.json() as Response<string>;
Expand Down Expand Up @@ -102,6 +119,21 @@ export default function StaffingRequest() {
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
<Row>
<Col>
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel className="text-lg text-white">Email</FormLabel>
<FormControl>
<Input className="text-black" placeholder="email" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</Col>
<Col>
<FormField
control={form.control}
Expand All @@ -110,13 +142,15 @@ export default function StaffingRequest() {
<FormItem>
<FormLabel className="text-lg text-white">Organization</FormLabel>
<FormControl>
<Input className="text-black" placeholder="Who are you?" {...field} />
<Input className="text-black" placeholder="Organization" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</Col>
</Row>
<Row>
<Col>
<FormField
control={form.control}
Expand All @@ -125,7 +159,12 @@ export default function StaffingRequest() {
<FormItem>
<FormLabel className="text-lg text-white">Estimated Pilots</FormLabel>
<FormControl>
<Input className="text-black" type="number" {...field} />
<Input
className="text-black"
type="number"
{...field}
onChange={(e) => field.onChange(e.target.value === "" ? undefined : Number(e.target.value))}
/>
</FormControl>
<FormMessage />
</FormItem>
Expand All @@ -139,6 +178,7 @@ export default function StaffingRequest() {
render={({ field }) => (
<FormItem>
<FormLabel className="text-lg text-white">Start</FormLabel>
<br />
<FormControl>
<DatePicker
selected={field.value}
Expand All @@ -162,7 +202,7 @@ export default function StaffingRequest() {
<FormLabel className="text-lg text-white">Duration</FormLabel>
<FormControl>
<Select onValueChange={field.onChange} defaultValue={field.value}>
<SelectTrigger>
<SelectTrigger className="!text-black">
<SelectValue placeholder="Select duration" />
</SelectTrigger>
<SelectContent className="!text-black">
Expand Down
2 changes: 1 addition & 1 deletion models/staffingRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type StaffingRequest = {
id: number;
id?: number;
cid: number;
fullName: string;
email: string;
Expand Down