From 6e4242736fd2d61e971b452059fc103a6293cad1 Mon Sep 17 00:00:00 2001 From: tuanvu Date: Sat, 13 Dec 2025 16:40:43 +0100 Subject: [PATCH] addchantier work --- app/(tabs)/addChantier.tsx | 44 ++++++++++++++++++++++++++++------- services/ressourcesService.ts | 1 + 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/app/(tabs)/addChantier.tsx b/app/(tabs)/addChantier.tsx index d31caba..71699ba 100644 --- a/app/(tabs)/addChantier.tsx +++ b/app/(tabs)/addChantier.tsx @@ -2,7 +2,6 @@ import ChantierSummary from '@/components/chantierSummary'; import SelectChantier from '@/components/selectChantier'; import SetStatus from '@/components/setStatus'; - import { ThemedView } from '@/components/theme/themed-view'; import React, { useEffect, useState } from 'react'; import { StyleSheet, ScrollView, Button, TextInput, Text, View, Modal } from 'react-native'; @@ -19,6 +18,8 @@ import DateTimePicker, { DateTimePickerEvent } from '@react-native-community/dat import Constants from 'expo-constants'; //pour connaître la taille de la barre menu de l'OS en haut import SelectChafChantier from '@/components/selectChefChantier'; import SelectMachine from '@/components/selectMachine'; +import { db } from '@/firebase_config'; +import { doc } from 'firebase/firestore'; //Uniquement accessible par le RESPONSSABLE du chantier //Pour créer ou modifier un chantier @@ -60,14 +61,41 @@ export default function AddChantier() { }; async function onConfirm(): Promise { - //TODO - //await changeChantierStatus(chantier.id,tempStatus) - //Il faut changer le UX - //setChantier({...chantier,etat: tempStatus}) - if(isValidChantier()){ - setOpenConfirmation(false); - } + if (!isValidChantier() || !chefChantier) return; + const chantierDate = new Date(date); + chantierDate.setHours(morning ? 0 : 12, 0, 0, 0); + //CREATE NEW TYPE CHANTIER FOR FIRESTORE + const chantierFirestore = { + adresse, + etat: "En cours", + contact, + chef: doc(db, "user", chefChantier.id), + equipe: [], + materiel: materiels + ? [doc(db, "ressources", String(materiels.id))] + : [], + vehicules: engins?.map(e => + doc(db, "ressources", String(e.id)) + ) || [], + anomalies: [], + dateDep: chantierDate, + tempsEst: parseInt(duree) || 1, + latitude: 0, //TODO + longitude: 0, //TODO + }; + const id = await addChantier(chantierFirestore as any); + if (id) { + //console.log("Chantier created with ID:", id); + setChantier({ ...chantierFirestore, + id, + chef: chefChantier, + equipe: [], + materiel: materiels ? [materiels] : [], + vehicules: engins || [], + } as Chantier); + setOpenConfirmation(false); } + } function onCancel(): void { setOpenConfirmation(false); diff --git a/services/ressourcesService.ts b/services/ressourcesService.ts index 0138114..5556674 100644 --- a/services/ressourcesService.ts +++ b/services/ressourcesService.ts @@ -10,6 +10,7 @@ export async function getUsers(): Promise { return snapshot.docs.map((doc) => { const data = doc.data(); return { + id: doc.id, ...data, allocation: data.allocation?.map(convertReservation) || [], } as User;