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(null); const navigate = useNavigate(); const createAirline = async (credentials: Credentials): Promise => { 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("/flights"); } else { setError(createResponse.message); } } catch (error) { setError(error as string); } finally { setIsLoading(false); } }; return { createAirline, isLoading, error }; };