chore: add project files and setup gitignore
This commit is contained in:
3
reserva-mesa-dashboard/hooks/useAuth.ts
Normal file
3
reserva-mesa-dashboard/hooks/useAuth.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { useAuth } from "@/contexts/AuthContext";
|
||||
|
||||
export { useAuth };
|
||||
94
reserva-mesa-dashboard/hooks/useMesas.ts
Normal file
94
reserva-mesa-dashboard/hooks/useMesas.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import { ref, onValue, off, update, push, remove } from "firebase/database";
|
||||
import { db } from "@/lib/firebase";
|
||||
import { useAuth } from "@/hooks/useAuth";
|
||||
import { Mesa, MesaEstado } from "@/types/mesa";
|
||||
|
||||
export function useMesas() {
|
||||
const { user } = useAuth();
|
||||
const [mesas, setMesas] = useState<Mesa[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
if (!user?.email) return;
|
||||
|
||||
const mesasRef = ref(db, "Mesas");
|
||||
|
||||
const unsubscribe = onValue(mesasRef, (snapshot) => {
|
||||
const data = snapshot.val();
|
||||
const list: Mesa[] = [];
|
||||
|
||||
if (data) {
|
||||
Object.keys(data).forEach((key) => {
|
||||
const item = data[key];
|
||||
if (item.restauranteEmail === user.email) {
|
||||
list.push({
|
||||
id: key,
|
||||
...item
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Sort by table number
|
||||
list.sort((a, b) => a.numero - b.numero);
|
||||
|
||||
setMesas(list);
|
||||
setLoading(false);
|
||||
});
|
||||
|
||||
return () => off(mesasRef, "value", unsubscribe);
|
||||
}, [user?.email]);
|
||||
|
||||
const updateMesaEstado = async (mesaId: string, novoEstado: MesaEstado) => {
|
||||
try {
|
||||
await update(ref(db, `Mesas/${mesaId}`), {
|
||||
estado: novoEstado
|
||||
});
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error("Erro ao atualizar estado da mesa:", error);
|
||||
return { success: false, error };
|
||||
}
|
||||
};
|
||||
|
||||
const addMesa = async (numero: number, capacidade: number) => {
|
||||
if (!user?.email) return { success: false, error: "Utilizador não autenticado" };
|
||||
|
||||
try {
|
||||
const newMesaRef = push(ref(db, "Mesas"));
|
||||
const mesaData: Mesa = {
|
||||
id: newMesaRef.key as string,
|
||||
numero,
|
||||
capacidade,
|
||||
estado: "Livre",
|
||||
restauranteEmail: user.email
|
||||
};
|
||||
await update(newMesaRef, mesaData);
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error("Erro ao adicionar mesa:", error);
|
||||
return { success: false, error };
|
||||
}
|
||||
};
|
||||
|
||||
const deleteMesa = async (mesaId: string) => {
|
||||
try {
|
||||
await remove(ref(db, `Mesas/${mesaId}`));
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error("Erro ao remover mesa:", error);
|
||||
return { success: false, error };
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
mesas,
|
||||
loading,
|
||||
updateMesaEstado,
|
||||
addMesa,
|
||||
deleteMesa
|
||||
};
|
||||
}
|
||||
100
reserva-mesa-dashboard/hooks/useReservas.ts
Normal file
100
reserva-mesa-dashboard/hooks/useReservas.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import { ref, onValue, off, update } from "firebase/database";
|
||||
import { db } from "@/lib/firebase";
|
||||
import { useAuth } from "@/hooks/useAuth";
|
||||
import { Reserva, ReservaEstado } from "@/types/reserva";
|
||||
|
||||
export function useReservas() {
|
||||
const { user } = useAuth();
|
||||
const [reservas, setReservas] = useState<Reserva[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
if (!user?.email) return;
|
||||
|
||||
const reservasRef = ref(db, "reservas");
|
||||
|
||||
const unsubscribe = onValue(reservasRef, (snapshot) => {
|
||||
const data = snapshot.val();
|
||||
const list: Reserva[] = [];
|
||||
|
||||
if (data) {
|
||||
Object.keys(data).forEach((key) => {
|
||||
const item = data[key];
|
||||
if (item.restauranteEmail === user.email) {
|
||||
list.push({
|
||||
id: key,
|
||||
...item
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Sort by date and time (newest first for management)
|
||||
list.sort((a, b) => {
|
||||
const dateA = new Date(`${a.data.replace(/-/g, "/")} ${a.hora}`);
|
||||
const dateB = new Date(`${b.data.replace(/-/g, "/")} ${b.hora}`);
|
||||
return dateB.getTime() - dateA.getTime();
|
||||
});
|
||||
|
||||
setReservas(list);
|
||||
setLoading(false);
|
||||
});
|
||||
|
||||
return () => off(reservasRef, "value", unsubscribe);
|
||||
}, [user?.email]);
|
||||
|
||||
const updateReservaEstado = async (reservaId: string, novoEstado: ReservaEstado) => {
|
||||
try {
|
||||
await update(ref(db, `reservas/${reservaId}`), {
|
||||
estado: novoEstado
|
||||
});
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error("Erro ao atualizar estado da reserva:", error);
|
||||
return { success: false, error };
|
||||
}
|
||||
};
|
||||
|
||||
const confirmarComMesa = async (reservaId: string, mesaId: string, mesaNumero: number) => {
|
||||
try {
|
||||
const updates: any = {};
|
||||
updates[`reservas/${reservaId}/estado`] = `Confirmada (Mesa ${mesaNumero})`;
|
||||
if (mesaId) {
|
||||
updates[`Mesas/${mesaId}/estado`] = "Reservada";
|
||||
}
|
||||
|
||||
await update(ref(db), updates);
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error("Erro ao confirmar reserva com mesa:", error);
|
||||
return { success: false, error };
|
||||
}
|
||||
};
|
||||
|
||||
const concluirReserva = async (reservaId: string, mesaId?: string) => {
|
||||
try {
|
||||
const updates: any = {};
|
||||
updates[`reservas/${reservaId}/estado`] = "Concluída";
|
||||
if (mesaId) {
|
||||
updates[`Mesas/${mesaId}/estado`] = "Livre";
|
||||
}
|
||||
|
||||
await update(ref(db), updates);
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error("Erro ao concluir reserva:", error);
|
||||
return { success: false, error };
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
reservas,
|
||||
loading,
|
||||
updateReservaEstado,
|
||||
confirmarComMesa,
|
||||
concluirReserva
|
||||
};
|
||||
}
|
||||
76
reserva-mesa-dashboard/hooks/useStaff.ts
Normal file
76
reserva-mesa-dashboard/hooks/useStaff.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import { ref, onValue, off, update, push, remove } from "firebase/database";
|
||||
import { db } from "@/lib/firebase";
|
||||
import { useAuth } from "@/hooks/useAuth";
|
||||
import { Staff } from "@/types/staff";
|
||||
|
||||
export function useStaff() {
|
||||
const { user } = useAuth();
|
||||
const [staff, setStaff] = useState<Staff[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
if (!user?.email) return;
|
||||
|
||||
const staffRef = ref(db, "Staff");
|
||||
|
||||
const unsubscribe = onValue(staffRef, (snapshot) => {
|
||||
const data = snapshot.val();
|
||||
const list: Staff[] = [];
|
||||
|
||||
if (data) {
|
||||
Object.keys(data).forEach((key) => {
|
||||
const item = data[key];
|
||||
if (item.restauranteEmail === user.email) {
|
||||
list.push({
|
||||
id: key,
|
||||
...item
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
setStaff(list);
|
||||
setLoading(false);
|
||||
});
|
||||
|
||||
return () => off(staffRef, "value", unsubscribe);
|
||||
}, [user?.email]);
|
||||
|
||||
const addStaff = async (member: Omit<Staff, "id" | "restauranteEmail">) => {
|
||||
if (!user?.email) return { success: false, error: "Utilizador não autenticado" };
|
||||
|
||||
try {
|
||||
const newStaffRef = push(ref(db, "Staff"));
|
||||
const staffData: Staff = {
|
||||
id: newStaffRef.key as string,
|
||||
...member,
|
||||
restauranteEmail: user.email
|
||||
};
|
||||
await update(newStaffRef, staffData);
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error("Erro ao adicionar funcionário:", error);
|
||||
return { success: false, error };
|
||||
}
|
||||
};
|
||||
|
||||
const deleteStaff = async (staffId: string) => {
|
||||
try {
|
||||
await remove(ref(db, `Staff/${staffId}`));
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error("Erro ao remover funcionário:", error);
|
||||
return { success: false, error };
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
staff,
|
||||
loading,
|
||||
addStaff,
|
||||
deleteStaff
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user