Add create airline flow to browser

This commit is contained in:
bsquillari 2023-12-04 20:54:09 +00:00
parent 00f016f229
commit 9b3039bead
5 changed files with 110 additions and 1 deletions

View File

@ -24,7 +24,7 @@ instance.interceptors.response.use(
json["count"] = response.headers["x-count"] json["count"] = response.headers["x-count"]
console.log(json) console.log(json)
return json return json
} else if (response.status == 204) { } else if (response.status === 204) {
return response; return response;
} }
return JSON.parse(response.data); return JSON.parse(response.data);
@ -41,6 +41,15 @@ export const createUser = (
return instance.post("users", credentials); return instance.post("users", credentials);
}; };
export const createAirline = (
credentials: Credentials,
token: string
): Promise<{ id?: string; message: string }> => {
return instance.post("users/airline", credentials, {
headers: { Authorization: `Bearer ${token}` },
});
};
export const fetchUserById = (id: number, token: string): Promise<User & { message?: string }> => { export const fetchUserById = (id: number, token: string): Promise<User & { message?: string }> => {
return instance.get("users/" + id, { return instance.get("users/" + id, {
headers: { Authorization: `Bearer ${token}` }, headers: { Authorization: `Bearer ${token}` },

View File

@ -1,6 +1,7 @@
import { LogIn } from "./components/LogIn/LogIn"; import { LogIn } from "./components/LogIn/LogIn";
import { Navigate, Route, RouteProps, Routes } from "react-router"; import { Navigate, Route, RouteProps, Routes } from "react-router";
import { SignUp } from "./components/SignUp/SignUp"; import { SignUp } from "./components/SignUp/SignUp";
import { CreateAirline } from "./components/SignUp/CreateAirline";
import { Home } from "./components/Home/Home"; import { Home } from "./components/Home/Home";
import { CreateFlight } from "./components/CreateFlight/CreateFlight"; import { CreateFlight } from "./components/CreateFlight/CreateFlight";
import { Button } from "antd"; import { Button } from "antd";
@ -15,6 +16,7 @@ function Router() {
<Routes> <Routes>
<Route path="/login" element={<LogIn />} /> <Route path="/login" element={<LogIn />} />
<Route path="/signup" element={<SignUp />} /> <Route path="/signup" element={<SignUp />} />
<Route path="/create-airline" element={!isAdmin ? <LogIn /> : <CreateAirline />} />
<Route path="/home" element={!user ? <LogIn /> : <Home />} /> <Route path="/home" element={!user ? <LogIn /> : <Home />} />
<Route path="/create-flight" element={!isAirline ? <LogIn /> : <CreateFlight />} /> <Route path="/create-flight" element={!isAirline ? <LogIn /> : <CreateFlight />} />
<Route path="/edit-flight/:id" element={!isAirline && !isAdmin ? <LogIn /> : <EditFlight />} /> <Route path="/edit-flight/:id" element={!isAirline && !isAdmin ? <LogIn /> : <EditFlight />} />

View File

@ -53,6 +53,7 @@ export const Home: React.FC<Props> = (props) => {
return ( return (
<div className="Box"> <div className="Box">
{isAirline ? <button onClick={() => { navigate("/create-flight") }}>Create flight</button> : <></>} {isAirline ? <button onClick={() => { navigate("/create-flight") }}>Create flight</button> : <></>}
{isAdmin ? <button onClick={() => { navigate("/create-airline") }}>Create airline user</button> : <></>}
<h2>Flights</h2> <h2>Flights</h2>
<div className="Items"> <div className="Items">
{(props.flights ? props.flights : flights).map((f) => { {(props.flights ? props.flights : flights).map((f) => {

View File

@ -0,0 +1,58 @@
import React, { useState } from "react";
import { Button, Input } from "antd";
import { useCreateAirline } from "../../hooks/useCreateAirline";
export const CreateAirline = () => {
const [username, setUsername] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [repeatPassword, setRepeatPassword] = useState("");
const { createAirline, isLoading, error } = useCreateAirline();
return (
<div className="Box Small">
<div className="Section">
<div className="Section">
<Input
type="email"
placeholder="Email"
onChange={(ev) => setEmail(ev.target.value)}
/>
<Input
placeholder="Username"
onChange={(ev) => setUsername(ev.target.value)}
/>
<Input.Password
placeholder="Password"
onChange={(ev) => setPassword(ev.target.value)}
/>
<Input.Password
placeholder="Repeat password"
onChange={(ev) => setRepeatPassword(ev.target.value)}
/>
<Button
style={{ width: "100%" }}
onClick={async () =>
await createAirline({ email, password, username })
}
loading={isLoading}
disabled={
email === "" ||
username === "" ||
password === "" ||
password !== repeatPassword
}
>
Create Airline
</Button>
{error ? (
<div className="Disconnected">{error}</div>
) : (
<></>
)}
</div>
</div>
</div>
);
};

View File

@ -0,0 +1,39 @@
import { useState } from "react";
import { Credentials } from "../Types";
import { createAirline as createAirlineAPI } from "../Api";
import useAuth from "../useAuth";
import { useNavigate } from "react-router";
export const useCreateAirline = () => {
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const navigate = useNavigate();
const createAirline = async (credentials: Credentials): Promise<void> => {
try {
setIsLoading(true);
setError(null);
const token = localStorage.getItem("token");
if (!token) {
setError("No token!");
setIsLoading(false);
return;
}
const createResponse = await createAirlineAPI(credentials, token);
if (createResponse.id) {
navigate("/home");
} else {
setError(createResponse.message);
}
} catch (error) {
setError(error as string);
} finally {
setIsLoading(false);
}
};
return { createAirline, isLoading, error };
};