Add admin role to front

This commit is contained in:
bsquillari 2023-12-04 19:57:39 +00:00
parent d8aa2dde19
commit 1ef259a7b4
4 changed files with 11 additions and 9 deletions

View File

@ -8,7 +8,7 @@ import useAuth, { AuthProvider } from "./useAuth";
import { EditFlight } from "./components/CreateFlight/EditFlight"; import { EditFlight } from "./components/CreateFlight/EditFlight";
function Router() { function Router() {
const { user, logout, isAirline } = useAuth(); const { user, logout, isAirline, isAdmin } = useAuth();
return ( return (
<div className="App"> <div className="App">
@ -17,7 +17,7 @@ function Router() {
<Route path="/signup" element={<SignUp />} /> <Route path="/signup" element={<SignUp />} />
<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 ? <LogIn /> : <EditFlight />} /> <Route path="/edit-flight/:id" element={!isAirline && !isAdmin ? <LogIn /> : <EditFlight />} />
<Route path="/" element={!user ? <LogIn /> : <Home />} /> <Route path="/" element={!user ? <LogIn /> : <Home />} />
</Routes> </Routes>
<div className="LogoutButton"> <div className="LogoutButton">

View File

@ -24,12 +24,13 @@ interface CardProps {
subscribed: boolean; subscribed: boolean;
refresh: any; refresh: any;
isAirline: boolean; isAirline: boolean;
isAdmin: boolean;
refreshFlights: any; refreshFlights: any;
} }
const { Text } = Typography; const { Text } = Typography;
export const Card: React.FC<CardProps> = ({ flight, user, subscribed, refresh, refreshFlights, isAirline }) => { export const Card: React.FC<CardProps> = ({ flight, user, subscribed, refresh, refreshFlights, isAirline, isAdmin }) => {
const [modalVisible, setModalVisible] = useState<boolean>(false); const [modalVisible, setModalVisible] = useState<boolean>(false);
const navigate = useNavigate(); const navigate = useNavigate();
@ -81,7 +82,6 @@ export const Card: React.FC<CardProps> = ({ flight, user, subscribed, refresh, r
editFlight("" + flight.id, data, token) editFlight("" + flight.id, data, token)
.then(() => { .then(() => {
console.log("culicagado")
refreshFlights() refreshFlights()
}) })
.catch((error) => { .catch((error) => {
@ -158,7 +158,7 @@ export const Card: React.FC<CardProps> = ({ flight, user, subscribed, refresh, r
<Text>{flight.id}</Text> <Text>{flight.id}</Text>
</Space> </Space>
</div> </div>
{!isAirline ? {!isAirline && !isAdmin ?
( (
!(subscribed) ? !(subscribed) ?
<Button type="primary" onClick={handleSubscribe}> <Button type="primary" onClick={handleSubscribe}>
@ -171,7 +171,7 @@ export const Card: React.FC<CardProps> = ({ flight, user, subscribed, refresh, r
) )
: :
( (
user && flight.user_id == user.id ? (user && flight.user_id == user.id) || isAdmin ?
<> <>
<Button type="primary" onClick={handleEdit}> <Button type="primary" onClick={handleEdit}>
Edit Edit

View File

@ -18,7 +18,7 @@ export const Home: React.FC<Props> = (props) => {
const navigate = useNavigate() const navigate = useNavigate()
const [currentPage, setCurrentPage] = useState(initialPage); const [currentPage, setCurrentPage] = useState(initialPage);
const { loading, isAirline, user, token } = useAuth(); const { loading, isAirline, user, token, isAdmin } = useAuth();
const { subscriptions, loading: subsLoading, fetchData } = useFetchSubscriptions(user, token); const { subscriptions, loading: subsLoading, fetchData } = useFetchSubscriptions(user, token);
@ -58,7 +58,7 @@ export const Home: React.FC<Props> = (props) => {
{(props.flights ? props.flights : flights).map((f) => { {(props.flights ? props.flights : flights).map((f) => {
return <Card key={f.id} flight={f} user={user} return <Card key={f.id} flight={f} user={user}
subscribed={subscriptions.some((i => i.flight_id === f.id))} subscribed={subscriptions.some((i => i.flight_id === f.id))}
refresh={fetchData} refreshFlights={refreshFlights} isAirline={isAirline} />; refresh={fetchData} refreshFlights={refreshFlights} isAirline={isAirline} isAdmin={isAdmin} />;
})} })}
{error ? <div className="Disconnected">{error}</div> : <></>} {error ? <div className="Disconnected">{error}</div> : <></>}
</div> </div>

View File

@ -8,6 +8,7 @@ interface AuthContextType {
user?: User; user?: User;
loading: boolean; loading: boolean;
isAirline: boolean; isAirline: boolean;
isAdmin: boolean;
token?: string; token?: string;
error?: any; error?: any;
login: (credentials: Credentials) => void; login: (credentials: Credentials) => void;
@ -105,13 +106,14 @@ export function AuthProvider({
user, user,
loading, loading,
isAirline, isAirline,
isAdmin,
token, token,
error, error,
login, login,
signUp, signUp,
logout, logout,
}), }),
[user, isAirline, loading, error] [user, isAirline, isAdmin, loading, error]
); );
return ( return (