From 84df00230ea75201f27ede0cb1b44756423ab2ce Mon Sep 17 00:00:00 2001 From: tuanvu Date: Tue, 9 Dec 2025 14:22:39 +0100 Subject: [PATCH] separte class and services --- app/(tabs)/gestionnaire_ressource.tsx | 7 +- class/class.tsx | 89 +---------------------- components/selectChantier.tsx | 3 +- services/ressourcesService.ts | 101 +++++++++++++++++++++----- 4 files changed, 91 insertions(+), 109 deletions(-) diff --git a/app/(tabs)/gestionnaire_ressource.tsx b/app/(tabs)/gestionnaire_ressource.tsx index ce31b48..ae8c79d 100644 --- a/app/(tabs)/gestionnaire_ressource.tsx +++ b/app/(tabs)/gestionnaire_ressource.tsx @@ -11,12 +11,13 @@ import { StyleSheet, Text } from "react-native"; -import { getRessources, Ressource } from "../../services/ressourcesService"; +import { Ressources } from "../../class/class"; +import { getRessources } from "../../services/ressourcesService"; export default function GestionnaireRessource() { const { nom, prenom } = useLocalSearchParams(); const [search, setSearch] = useState(""); - const [ressource, setRessources] = useState([]); + const [ressource, setRessources] = useState([]); const [loading, setLoading] = useState(true); const router = useRouter(); @@ -39,7 +40,7 @@ export default function GestionnaireRessource() { r.name.toLowerCase().includes(search.toLowerCase()) ); - const renderRessource = ({ item }: { item: Ressource }) => { + const renderRessource = ({ item }: { item: Ressources }) => { if (!item) return null; return ( diff --git a/class/class.tsx b/class/class.tsx index c9b589f..8f47d75 100644 --- a/class/class.tsx +++ b/class/class.tsx @@ -1,6 +1,3 @@ -import { collection, getDocs, Timestamp } from "firebase/firestore"; -import { db } from "../firebase_config"; - export type Chantier = { id: number; adresse: string; @@ -15,42 +12,6 @@ export type Chantier = { anomalies: string[]; }; -export async function getChantiers(): Promise { - try { - const colRef = collection(db, "chantier"); - const snapshot = await getDocs(colRef); - return snapshot.docs.map((doc) => { - const data = doc.data() as any; - return { - ...data, - chef: { - ...data.chef, - allocation: data.chef?.allocation?.map(convertReservation) || [], - }, - equipe: - data.equipe?.map((u: any) => ({ - ...u, - allocation: u.allocation?.map(convertReservation) || [], - })) || [], - materiel: - data.materiel?.map((m: any) => ({ - ...m, - allocation: m.allocation?.map(convertReservation) || [], - })) || [], - vehicules: - data.vehicules?.map((v: any) => ({ - ...v, - allocation: v.allocation?.map(convertReservation) || [], - })) || [], - anomalies: data.anomalies || [], - } as Chantier; - }); - } catch (err) { - console.error("Firestore Chantiers Error:", err); - return []; - } -} - export type User = { id: string; name: string; @@ -60,23 +21,6 @@ export type User = { qualifications: string; }; -export async function getUsers(): Promise { - try { - const colRef = collection(db, "user"); - const snapshot = await getDocs(colRef); - return snapshot.docs.map((doc) => { - const data = doc.data(); - return { - ...data, - allocation: data.allocation?.map(convertReservation) || [], - } as User; - }); - } catch (err) { - console.error("Firestore Users Error:", err); - return []; - } -} - export type Ressources = { id: number; name: string; @@ -87,39 +31,8 @@ export type Ressources = { allocation: Reservation[]; }; -export async function getRessources(): Promise { - try { - const colRef = collection(db, "ressources"); - const snapshot = await getDocs(colRef); - return snapshot.docs.map((doc) => { - const data = doc.data(); - return { - ...data, - allocation: data.allocation?.map(convertReservation) || [], - } as Ressources; - }); - } catch (err) { - console.error("Firestore Ressources Error:", err); - return []; - } -} - export type Reservation = { id: string; dateChantier: Date; dateFin: Date; -}; - -function convertReservation(res: any): Reservation { - return { - id: res.id, - dateChantier: - res.dateChantier instanceof Timestamp - ? res.dateChantier.toDate() - : new Date(res.dateChantier), - dateFin: - res.dateFin instanceof Timestamp - ? res.dateFin.toDate() - : new Date(res.dateFin), - }; -} +}; \ No newline at end of file diff --git a/components/selectChantier.tsx b/components/selectChantier.tsx index 521f5d4..5a357e8 100644 --- a/components/selectChantier.tsx +++ b/components/selectChantier.tsx @@ -1,5 +1,6 @@ import { useChantier } from "@/app/ContextChantier"; -import { Chantier, getChantiers } from "@/class/class"; +import { Chantier } from "@/class/class"; +import { getChantiers } from "@/services/ressourcesService"; import { useEffect, useState } from "react"; import { Dimensions, diff --git a/services/ressourcesService.ts b/services/ressourcesService.ts index a47406c..fabdd20 100644 --- a/services/ressourcesService.ts +++ b/services/ressourcesService.ts @@ -1,23 +1,90 @@ import { collection, getDocs, doc, updateDoc } from "firebase/firestore"; -import { db } from "../firebase_config"; +import { db } from "../firebase_config"; +import { Timestamp } from "firebase/firestore"; +import { Chantier, User, Ressources, Reservation } from "../class/class"; -export type Ressource = { - id: number; - name: string; - type: string; - Image: string; - quantity: number; - available_quantity: number; -}; - -export async function getRessources(): Promise { +export async function getUsers(): Promise { try { - const ressourcesCol = collection(db, "ressources"); - const snapshot = await getDocs(ressourcesCol); - const list = snapshot.docs.map((doc) => doc.data() as Ressource); - return list; + const colRef = collection(db, "user"); + const snapshot = await getDocs(colRef); + return snapshot.docs.map((doc) => { + const data = doc.data(); + return { + ...data, + allocation: data.allocation?.map(convertReservation) || [], + } as User; + }); } catch (err) { - console.log("Firestore Error:", err); + console.error("Firestore Users Error:", err); return []; } -} \ No newline at end of file +} + + +export async function getRessources(): Promise { + try { + const colRef = collection(db, "ressources"); + const snapshot = await getDocs(colRef); + return snapshot.docs.map((doc) => { + const data = doc.data(); + return { + ...data, + allocation: data.allocation?.map(convertReservation) || [], + } as Ressources; + }); + } catch (err) { + console.error("Firestore Ressources Error:", err); + return []; + } +} + + +export async function getChantiers(): Promise { + try { + const colRef = collection(db, "chantier"); + const snapshot = await getDocs(colRef); + return snapshot.docs.map((doc) => { + const data = doc.data() as any; + return { + ...data, + chef: { + ...data.chef, + allocation: data.chef?.allocation?.map(convertReservation) || [], + }, + equipe: + data.equipe?.map((u: any) => ({ + ...u, + allocation: u.allocation?.map(convertReservation) || [], + })) || [], + materiel: + data.materiel?.map((m: any) => ({ + ...m, + allocation: m.allocation?.map(convertReservation) || [], + })) || [], + vehicules: + data.vehicules?.map((v: any) => ({ + ...v, + allocation: v.allocation?.map(convertReservation) || [], + })) || [], + anomalies: data.anomalies || [], + } as Chantier; + }); + } catch (err) { + console.error("Firestore Chantiers Error:", err); + return []; + } +} + +function convertReservation(res: any): Reservation { + return { + id: res.id, + dateChantier: + res.dateChantier instanceof Timestamp + ? res.dateChantier.toDate() + : new Date(res.dateChantier), + dateFin: + res.dateFin instanceof Timestamp + ? res.dateFin.toDate() + : new Date(res.dateFin), + }; +}