diff --git a/app/ContextChantier.tsx b/app/ContextChantier.tsx index 7adfdc3..5b21f58 100644 --- a/app/ContextChantier.tsx +++ b/app/ContextChantier.tsx @@ -1,9 +1,11 @@ import { Chantier } from "@/class/class"; import { createContext, ReactNode, useContext, useMemo, useState } from "react"; +import { getChantiers } from "@/services/ressourcesService"; type ChantierContextType = { chantier: Chantier | null; setChantier: (p: Chantier | null) => void; + syncChantier: () => Promise; }; const ChantierContext = createContext(null); @@ -14,8 +16,19 @@ type ChantierProviderProps = { export const ChantierProvider = ({ children }: ChantierProviderProps) => { const [chantier, setChantier] = useState(null); + + const syncChantier = async () => { + if (!chantier) return; - const value = useMemo(() => ({ chantier, setChantier }), [chantier]); + const all = await getChantiers(); + const updated = all.find(c => c.id === chantier.id); + + if (updated) { + setChantier(updated); + } + }; + + const value = useMemo(() => ({ chantier, setChantier,syncChantier }), [chantier]); return ( diff --git a/components/anomaly.tsx b/components/anomaly.tsx index 9f5955f..3defbfb 100644 --- a/components/anomaly.tsx +++ b/components/anomaly.tsx @@ -1,9 +1,10 @@ import { Chantier } from '@/class/class'; -import { ThemedView, } from '@/components/theme/themed-view'; -import React from 'react'; -import { Image, StyleProp, StyleSheet, View, ViewStyle } from 'react-native'; +import { ThemedView } from '@/components/theme/themed-view'; +import React, { use, useState } from 'react'; +import { TouchableOpacity, StyleProp, StyleSheet, View, ViewStyle,Text, TextInput } from 'react-native'; import { ThemedText } from './theme/themed-text'; - +import { deleteAnomalie, addAnomalie, getChantiers } from '@/services/ressourcesService'; +import { useChantier } from '@/app/ContextChantier'; type Props = { data: { @@ -12,7 +13,25 @@ type Props = { style?: StyleProp; }; -export default function Anomaly({data,style , ...otherProps }: Props) { +export default function Anomaly({data,style}: Props) { + + const{syncChantier }= useChantier(); + + const handleDelete = async (anomaly: string) => { + await deleteAnomalie(data.chantier!.id, anomaly); + data.chantier!.anomalies = data.chantier!.anomalies.filter(a => a !== anomaly); + await syncChantier(); + }; + + const handleAdd = async () => { + if (newAnomaly.trim().length === 0) return; + await addAnomalie(data.chantier!.id, newAnomaly.trim()); + data.chantier!.anomalies.push(newAnomaly.trim()); + setNewAnomaly(""); + await syncChantier(); + }; + + const [newAnomaly, setNewAnomaly] = useState(""); return( @@ -23,11 +42,21 @@ export default function Anomaly({data,style , ...otherProps }: Props) { data.chantier.anomalies.map((anomaly, index) => ( • {anomaly} + handleDelete(anomaly)} style={styles.deleteButton}> + + )) ) : ( Aucune anomalie ) } + {/* Add Anomaly Section */} + + + + Ajouter + + ): null} @@ -55,4 +84,41 @@ const styles = StyleSheet.create({ fontStyle: "italic", opacity: 0.7, }, -}); \ No newline at end of file + //delete button styles + deleteButton: { + backgroundColor: "red", + width: 24, + height: 24, + borderRadius: 12, + alignItems: "center", + justifyContent: "center", + }, + deleteText: { + color: "white", + fontWeight: "bold", + fontSize: 14, + lineHeight: 14, + }, + //add anomaly styles + addContainer: { + marginTop: 10, + flexDirection: "row", + alignItems: "center" + }, + input: { + flex: 1, + backgroundColor: "white", + padding: 8, + borderRadius: 8, + marginRight: 8, + }, + addButton: { + paddingVertical: 8, + paddingHorizontal: 12, + borderRadius: 8, + }, + addButtonText: { + color: "white", + fontWeight: "bold", + } +}) \ No newline at end of file diff --git a/services/ressourcesService.ts b/services/ressourcesService.ts index 2a32e8d..0138114 100644 --- a/services/ressourcesService.ts +++ b/services/ressourcesService.ts @@ -1,4 +1,4 @@ -import { addDoc, collection, doc, getDoc, getDocs, Timestamp, updateDoc } from "firebase/firestore"; +import { addDoc, arrayUnion, collection, doc, getDoc, getDocs, Timestamp, updateDoc } from "firebase/firestore"; import { Chantier, Reservation, Ressources, User } from "../class/class"; import { db } from "../firebase_config"; @@ -78,7 +78,7 @@ export async function changeChantierStatus(chantierId: string, newStatus: string try { const chantierRef = doc(db, "chantier", chantierId); await updateDoc(chantierRef, { etat: newStatus }); - console.log(`Chantier ${chantierId} status updated to ${newStatus}`); + console.log("Chantier ${chantierId} status updated to ${newStatus}"); } catch (err) { console.error("Error", err); } @@ -98,30 +98,19 @@ export async function addChantier(chantierData: Omit): Promise { +export async function addAnomalie(chantierId: string, anomalie_String: string): Promise { try { const chantierRef = doc(db, "chantier", chantierId); - const chantierSnap = await getDoc(chantierRef); - if (chantierSnap.exists()) { - const chantierData = chantierSnap.data(); - const anomalies = chantierData.anomalies || []; - const updatedAnomalies = anomalies.map((anomalie: any) => { - if (anomalie.description === anomalie_String) { - return { ...anomalie, status: newStatus }; - } - return anomalie; - }); - await updateDoc(chantierRef, { anomalies: updatedAnomalies }); - console.log(`Anomalie status updated to ${newStatus}`); - } else { - console.error("Chantier not found"); - } + await updateDoc(chantierRef, { + anomalies: arrayUnion(anomalie_String) + }); + console.log("Anomalie added"); } catch (err) { - console.error("Error", err); + console.error("Error adding anomalie:", err); } } -//CHANGE CHANTIER ANOMALIE STATUS +//DELETE CHANTIER ANOMALIE STATUS export async function deleteAnomalie(chantierId: string, anomalie_String: string): Promise { try { const chantierRef = doc(db, "chantier", chantierId); @@ -129,9 +118,10 @@ export async function deleteAnomalie(chantierId: string, anomalie_String: string if (chantierSnap.exists()) { const chantierData = chantierSnap.data(); const anomalies = chantierData.anomalies || []; - const updatedAnomalies = anomalies.filter((anomalie: any) => anomalie.description !== anomalie_String); + //Filtage + const updatedAnomalies = anomalies.filter((anomaly: string) => anomaly !== anomalie_String); await updateDoc(chantierRef, { anomalies: updatedAnomalies }); - console.log(`Anomalie deleted`); + console.log("Anomalie deleted"); } else { console.error("Chantier not found"); } @@ -148,4 +138,4 @@ function convertReservation(res: any): Reservation { dateFin: res.dateFin instanceof Timestamp ? res.dateFin.toDate() : new Date(res.dateFin), }; -} +} \ No newline at end of file