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 { createContext, ReactNode, useContext, useMemo, useState } from "react";
import { getChantiers } from "@/services/ressourcesService";
type ChantierContextType = {
chantier: Chantier | null;
setChantier: (p: Chantier | null) => void;
syncChantier: () => Promise<void>;
};
const ChantierContext = createContext<ChantierContextType | null>(null);
@@ -14,8 +16,19 @@ type ChantierProviderProps = {
export const ChantierProvider = ({ children }: ChantierProviderProps) => {
const [chantier, setChantier] = useState<Chantier | null>(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 (
<ChantierContext.Provider value={value}>

View File

@@ -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<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(
<View style={style}>
@@ -23,11 +42,21 @@ export default function Anomaly({data,style , ...otherProps }: Props) {
data.chantier.anomalies.map((anomaly, index) => (
<ThemedView key={index} lvl={2} style={styles.anomalyItem}>
<ThemedText> {anomaly}</ThemedText>
<TouchableOpacity onPress={() => handleDelete(anomaly)} style={styles.deleteButton}>
<Text style={styles.deleteText}></Text>
</TouchableOpacity>
</ThemedView>
))
) : (
<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>
): null}
</View>
@@ -55,4 +84,41 @@ const styles = StyleSheet.create({
fontStyle: "italic",
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 { 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<Chantier, 'id'>): Promise<s
}
//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 {
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<void> {
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),
};
}
}