fini fontionnalité de anomalie, ptet changer styles

This commit is contained in:
tuanvu
2025-12-13 11:26:22 +01:00
parent 68af57a394
commit 730a9882c7
3 changed files with 99 additions and 30 deletions

View File

@@ -1,9 +1,11 @@
import { Chantier } from "@/class/class"; import { Chantier } from "@/class/class";
import { createContext, ReactNode, useContext, useMemo, useState } from "react"; import { createContext, ReactNode, useContext, useMemo, useState } from "react";
import { getChantiers } from "@/services/ressourcesService";
type ChantierContextType = { type ChantierContextType = {
chantier: Chantier | null; chantier: Chantier | null;
setChantier: (p: Chantier | null) => void; setChantier: (p: Chantier | null) => void;
syncChantier: () => Promise<void>;
}; };
const ChantierContext = createContext<ChantierContextType | null>(null); const ChantierContext = createContext<ChantierContextType | null>(null);
@@ -15,7 +17,18 @@ type ChantierProviderProps = {
export const ChantierProvider = ({ children }: ChantierProviderProps) => { export const ChantierProvider = ({ children }: ChantierProviderProps) => {
const [chantier, setChantier] = useState<Chantier | null>(null); const [chantier, setChantier] = useState<Chantier | null>(null);
const value = useMemo(() => ({ chantier, setChantier }), [chantier]); const syncChantier = async () => {
if (!chantier) return;
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 ( return (
<ChantierContext.Provider value={value}> <ChantierContext.Provider value={value}>

View File

@@ -1,9 +1,10 @@
import { Chantier } from '@/class/class'; import { Chantier } from '@/class/class';
import { ThemedView, } from '@/components/theme/themed-view'; import { ThemedView } from '@/components/theme/themed-view';
import React from 'react'; import React, { use, useState } from 'react';
import { Image, StyleProp, StyleSheet, View, ViewStyle } from 'react-native'; import { TouchableOpacity, StyleProp, StyleSheet, View, ViewStyle,Text, TextInput } from 'react-native';
import { ThemedText } from './theme/themed-text'; import { ThemedText } from './theme/themed-text';
import { deleteAnomalie, addAnomalie, getChantiers } from '@/services/ressourcesService';
import { useChantier } from '@/app/ContextChantier';
type Props = { type Props = {
data: { data: {
@@ -12,7 +13,25 @@ type Props = {
style?: StyleProp<ViewStyle>; style?: StyleProp<ViewStyle>;
}; };
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( return(
<View style={style}> <View style={style}>
@@ -23,11 +42,21 @@ export default function Anomaly({data,style , ...otherProps }: Props) {
data.chantier.anomalies.map((anomaly, index) => ( data.chantier.anomalies.map((anomaly, index) => (
<ThemedView key={index} lvl={2} style={styles.anomalyItem}> <ThemedView key={index} lvl={2} style={styles.anomalyItem}>
<ThemedText> {anomaly}</ThemedText> <ThemedText> {anomaly}</ThemedText>
<TouchableOpacity onPress={() => handleDelete(anomaly)} style={styles.deleteButton}>
<Text style={styles.deleteText}></Text>
</TouchableOpacity>
</ThemedView> </ThemedView>
)) ))
) : ( ) : (
<ThemedText style={styles.noAnomaly}>Aucune anomalie</ThemedText> <ThemedText style={styles.noAnomaly}>Aucune anomalie</ThemedText>
) } ) }
{/* Add Anomaly Section */}
<View style={styles.addContainer}>
<TextInput style={styles.input} placeholder="Nouvelle anomalie..." value={newAnomaly} onChangeText={setNewAnomaly} />
<TouchableOpacity style={styles.addButton} onPress={handleAdd}>
<Text style={styles.addButtonText}>Ajouter</Text>
</TouchableOpacity>
</View>
</ThemedView> </ThemedView>
): null} ): null}
</View> </View>
@@ -55,4 +84,41 @@ const styles = StyleSheet.create({
fontStyle: "italic", fontStyle: "italic",
opacity: 0.7, opacity: 0.7,
}, },
}); //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",
}
})

View File

@@ -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 { Chantier, Reservation, Ressources, User } from "../class/class";
import { db } from "../firebase_config"; import { db } from "../firebase_config";
@@ -78,7 +78,7 @@ export async function changeChantierStatus(chantierId: string, newStatus: string
try { try {
const chantierRef = doc(db, "chantier", chantierId); const chantierRef = doc(db, "chantier", chantierId);
await updateDoc(chantierRef, { etat: newStatus }); await updateDoc(chantierRef, { etat: newStatus });
console.log(`Chantier ${chantierId} status updated to ${newStatus}`); console.log("Chantier ${chantierId} status updated to ${newStatus}");
} catch (err) { } catch (err) {
console.error("Error", err); console.error("Error", err);
} }
@@ -98,30 +98,19 @@ export async function addChantier(chantierData: Omit<Chantier, 'id'>): Promise<s
} }
//CHANGE CHANTIER ANOMALIE STATUS //CHANGE CHANTIER ANOMALIE STATUS
export async function changeAnomalieStatus(chantierId: string, anomalie_String: string, newStatus: string): Promise<void> { export async function addAnomalie(chantierId: string, anomalie_String: string): Promise<void> {
try { try {
const chantierRef = doc(db, "chantier", chantierId); const chantierRef = doc(db, "chantier", chantierId);
const chantierSnap = await getDoc(chantierRef); await updateDoc(chantierRef, {
if (chantierSnap.exists()) { anomalies: arrayUnion(anomalie_String)
const chantierData = chantierSnap.data(); });
const anomalies = chantierData.anomalies || []; console.log("Anomalie added");
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");
}
} catch (err) { } 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<void> { export async function deleteAnomalie(chantierId: string, anomalie_String: string): Promise<void> {
try { try {
const chantierRef = doc(db, "chantier", chantierId); const chantierRef = doc(db, "chantier", chantierId);
@@ -129,9 +118,10 @@ export async function deleteAnomalie(chantierId: string, anomalie_String: string
if (chantierSnap.exists()) { if (chantierSnap.exists()) {
const chantierData = chantierSnap.data(); const chantierData = chantierSnap.data();
const anomalies = chantierData.anomalies || []; 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 }); await updateDoc(chantierRef, { anomalies: updatedAnomalies });
console.log(`Anomalie deleted`); console.log("Anomalie deleted");
} else { } else {
console.error("Chantier not found"); console.error("Chantier not found");
} }