clean des composants, dossiers

This commit is contained in:
Rochas
2025-12-11 21:25:58 +01:00
4 changed files with 146 additions and 53 deletions

View File

@@ -13,9 +13,7 @@ Lien du git : gitlab2.istic.univ-rennes1.fr/trochas/mmm-projet
Différentes commandes a effectuer pour lancer le projet: Différentes commandes a effectuer pour lancer le projet:
npm install firebase
npm install react-native-maps
npx expo install react-native-maps@1.9.0 npx expo install react-native-maps@1.9.0
npm install react-native-maps @react-navigation/native @react-navigation/bottom-tabs react-native-safe-area-context react-native-screens npm install react-native-maps @react-navigation/native @react-navigation/bottom-tabs react-native-safe-area-context react-native-screens firebase
npx expo start npx expo start

View File

@@ -4,7 +4,6 @@ import React from 'react';
import { Image, StyleProp, StyleSheet, View, ViewStyle } from 'react-native'; import { Image, StyleProp, StyleSheet, View, ViewStyle } from 'react-native';
import { ThemedText } from './theme/themed-text'; import { ThemedText } from './theme/themed-text';
type Props = { type Props = {
data: { data: {
chantier:Chantier|null; chantier:Chantier|null;
@@ -13,8 +12,6 @@ type Props = {
}; };
export default function ChantierSummary({data,style , ...otherProps }: Props) { export default function ChantierSummary({data,style , ...otherProps }: Props) {
return( return(
<View style={style}> <View style={style}>
{data.chantier ? ( {data.chantier ? (

View File

@@ -1,10 +1,8 @@
import { useChantier } from "@/app/ContextChantier"; import { useChantier } from "@/app/ContextChantier";
import { Chantier } from "@/class/class"; import { Chantier } from "@/class/class";
import { getChantiers } from "@/services/ressourcesService"; import { getChantiers } from "@/services/ressourcesService";
import { useRouter } from "expo-router";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { import {
ActivityIndicator,
Dimensions, Dimensions,
Image, Image,
Pressable, Pressable,
@@ -19,6 +17,7 @@ import { ThemedButton } from "./theme/themed-button";
import { ThemedText } from "./theme/themed-text"; import { ThemedText } from "./theme/themed-text";
import { ThemedTextInput } from "./theme/themed-textinput"; import { ThemedTextInput } from "./theme/themed-textinput";
import { ThemedView } from "./theme/themed-view"; import { ThemedView } from "./theme/themed-view";
import { router } from "expo-router";
const screenHeight = Dimensions.get("window").height; const screenHeight = Dimensions.get("window").height;
const { width, height } = Dimensions.get("window"); const { width, height } = Dimensions.get("window");
@@ -31,28 +30,34 @@ const { width, height } = Dimensions.get("window");
*/ */
export default function SelectChantier() { export default function SelectChantier(props: {
data?: any[];
multiple?: boolean;
selected?: string[] | string | null;
onChange?: (val: any) => void;
placeholder?: string;
}) {
const { data: propData, multiple = false, selected, onChange, placeholder } = props || {};
const { chantier, setChantier } = useChantier(); const { chantier, setChantier } = useChantier();
const [search, setSearch] = useState(""); const [search, setSearch] = useState("");
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const [isLoaded, setIsLoaded] = useState(false); const [chantiers, setChantiers] = useState<any[]>([]);
const [chantiers, setChantiers] = useState<Chantier[]>([]);
const router = useRouter();
const AnimatedThemedView = Animated.createAnimatedComponent(ThemedView);
// cast to any to avoid strict Animated typing issues for custom props like `lvl`/`border` const AnimatedThemedText = Animated.createAnimatedComponent(ThemedText);
const AnimatedThemedView: any = Animated.createAnimatedComponent(ThemedView as any); const AnimatedThemedButton = Animated.createAnimatedComponent(ThemedButton);
const AnimatedThemedText: any = Animated.createAnimatedComponent(ThemedText as any); const AnimatedThemedTextInput =
const AnimatedThemedButton: any = Animated.createAnimatedComponent(ThemedButton as any); Animated.createAnimatedComponent(ThemedTextInput);
const AnimatedThemedTextInput: any = Animated.createAnimatedComponent(ThemedTextInput as any);
async function onPressOpen(){ async function onPressOpen(){
setIsLoaded(false);
setIsOpen(!isOpen); setIsOpen(!isOpen);
if(!isOpen){ if(!isOpen){
const updatedChantiers = await getChantiers(); if (propData && propData.length) {
setIsLoaded(true); setChantiers(propData as any[]);
setChantiers(updatedChantiers) } else {
const updatedChantiers = await getChantiers();
setChantiers(updatedChantiers);
}
} }
} }
@@ -62,6 +67,12 @@ export default function SelectChantier() {
} }
useEffect(() => { useEffect(() => {
// If parent provided data, use it. Otherwise fetch chantiers.
if (propData && propData.length) {
setChantiers(propData as any[]);
return;
}
async function loadChantiers() { async function loadChantiers() {
const list = await getChantiers(); const list = await getChantiers();
setChantiers(list); setChantiers(list);
@@ -70,22 +81,56 @@ export default function SelectChantier() {
loadChantiers(); loadChantiers();
}, []); }, []);
function selectChantier(chantier: Chantier): void { function getId(item: any) {
setChantier(chantier); return (item && (item.id ?? item._id ?? item.uid ?? item.key)) ?? String(item);
}
function getLabel(item: any) {
if (!item) return String(item);
if (item.adresse) return item.adresse;
if (item.nom) return item.nom;
if (item.name && item.last_name) return `${item.last_name} ${item.name}`;
if (item.name) return item.name;
if (item.label) return item.label;
if (typeof item === 'string') return item;
return JSON.stringify(item);
}
function selectChantier(item: any): void {
const id = String(getId(item));
if (multiple) {
// for multiple selection toggle id in selected array
const current = Array.isArray(selected) ? [...selected] : [];
const idx = current.indexOf(id);
if (idx >= 0) current.splice(idx, 1);
else current.push(id);
onChange?.(current);
// keep menu open for multiple selection
return;
}
// single selection
if (onChange) {
onChange(id);
} else {
// fallback behavior for old usage: if selecting a Chantier, update context
setChantier(item as Chantier);
}
setIsOpen(false); setIsOpen(false);
} }
const renderChantier = (chantier: Chantier, index: number) => { const renderChantier = (item: any, index: number) => {
const label = getLabel(item);
const id = String(getId(item));
const isSelected = Array.isArray(selected) ? selected.includes(id) : selected === id;
return ( return (
<Pressable key={index} onPress={() => selectChantier(chantier)}> <Pressable key={index} onPress={() => selectChantier(item)}>
<ThemedView lvl={4} style={styles.chantier}> <ThemedView lvl={4} style={[styles.chantier, isSelected ? { borderWidth: 1 } : {}]}>
<View> <View>
<Image source={{ uri:"https://cdn.discordapp.com/attachments/1425108443571945644/1427207643180826757/raw.png?ex=69392bb2&is=6937da32&hm=dcc09e76d3dca89d2418947b46efbd38673b9dc559027724b2e51d493b173bc9&" /*chantier.urlImg*/ }} style={styles.image} /> <Image source={{ uri: "https://cdn.discordapp.com/attachments/1425108443571945644/1427207643180826757/raw.png?ex=69392bb2&is=6937da32&hm=dcc09e76d3dca89d2418947b46efbd38673b9dc559027724b2e51d493b173bc9&" }} style={styles.image} />
</View> </View>
<View> <View>
<ThemedText>Adresse: {chantier.adresse}</ThemedText> <ThemedText>{label}</ThemedText>
<ThemedText>Chef de chantier: {chantier.chef.last_name}{" "}{chantier.chef.name}</ThemedText>
<ThemedText>État: {chantier.etat}</ThemedText>
</View> </View>
</ThemedView> </ThemedView>
</Pressable> </Pressable>
@@ -98,7 +143,19 @@ export default function SelectChantier() {
<AnimatedThemedView layout={LinearTransition.duration(200)} lvl={2} shadow={true} style={styles.window}> <AnimatedThemedView layout={LinearTransition.duration(200)} lvl={2} shadow={true} style={styles.window}>
<AnimatedThemedButton style={isOpen ? styles.buttonOpen : styles.buttonClose} lvl={isOpen ? 1 : 1} onPress={() => onPressOpen()}> <AnimatedThemedButton style={isOpen ? styles.buttonOpen : styles.buttonClose} lvl={isOpen ? 1 : 1} onPress={() => onPressOpen()}>
<ThemedText style={styles.buttonText}> <ThemedText style={styles.buttonText}>
{isOpen ? "Fermer" : (chantier!=null ? chantier.adresse : "Chantier")} {isOpen
? "Fermer"
: (multiple
? (Array.isArray(selected) && selected.length ? `${selected.length} sélectionnés` : (placeholder ?? "Sélectionner"))
: (selected ? (() => {
// show label of selected single item if provided in propData
if (selected && propData) {
const found = propData.find((it: any) => String(getId(it)) === String(selected));
return found ? getLabel(found) : (chantier ? (chantier.adresse ?? getLabel(chantier)) : (placeholder ?? "Chantier"));
}
return chantier ? (chantier.adresse ?? getLabel(chantier)) : (placeholder ?? "Chantier");
})() : (placeholder ?? "Chantier"))
)}
</ThemedText> </ThemedText>
</AnimatedThemedButton> </AnimatedThemedButton>
{isOpen && ( {isOpen && (
@@ -111,16 +168,14 @@ export default function SelectChantier() {
+ +
</ThemedText> </ThemedText>
</ThemedButton> </ThemedButton>
<View style={styles.list}>
{isLoaded? <ThemedView lvl={2} style={styles.list}>
<ScrollView contentContainerStyle={styles.chantiersList}> <ScrollView contentContainerStyle={styles.chantiersList}>
{chantiers.map((chantier, index) => {chantiers.map((chantier, index) =>
renderChantier(chantier, index) renderChantier(chantier, index)
)} )}
</ScrollView> </ScrollView>
: <ActivityIndicator style={{height:"100%"}} color="#808080" size="large" />} </ThemedView>
</View>
</View> </View>
)} )}
</AnimatedThemedView> </AnimatedThemedView>
@@ -223,8 +278,5 @@ const styles = StyleSheet.create({
buttonAdd:{ buttonAdd:{
borderRadius: 10, borderRadius: 10,
marginBottom: 10, marginBottom: 10,
height: 30,
alignItems: 'center',
justifyContent: 'center',
} }
}); });

View File

@@ -2,6 +2,7 @@ import { addDoc, collection, doc, getDoc, getDocs, Timestamp, updateDoc } from "
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";
///////////////////////////////////USER/////////////////////////////////////
export async function getUsers(): Promise<User[]> { export async function getUsers(): Promise<User[]> {
try { try {
const colRef = collection(db, "user"); const colRef = collection(db, "user");
@@ -18,7 +19,7 @@ export async function getUsers(): Promise<User[]> {
return []; return [];
} }
} }
///////////////////////////////////RESSOURCE////////////////////////////////
export async function getRessources(): Promise<Ressources[]> { export async function getRessources(): Promise<Ressources[]> {
try { try {
const colRef = collection(db, "ressources"); const colRef = collection(db, "ressources");
@@ -35,7 +36,7 @@ export async function getRessources(): Promise<Ressources[]> {
return []; return [];
} }
} }
///////////////////////////////////CHANTIER/////////////////////////////////
export async function getChantiers(): Promise<Chantier[]> { export async function getChantiers(): Promise<Chantier[]> {
const snap = await getDocs(collection(db, "chantier")); const snap = await getDocs(collection(db, "chantier"));
const chantiers: Chantier[] = []; const chantiers: Chantier[] = [];
@@ -72,16 +73,7 @@ export async function getChantiers(): Promise<Chantier[]> {
return chantiers; return chantiers;
} }
function convertReservation(res: any): Reservation { //CHANGE CHANTIER STATUS
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),
};
}
export async function changeChantierStatus(chantierId: string, newStatus: string): Promise<void> { export async function changeChantierStatus(chantierId: string, newStatus: string): Promise<void> {
try { try {
const chantierRef = doc(db, "chantier", chantierId); const chantierRef = doc(db, "chantier", chantierId);
@@ -92,6 +84,7 @@ export async function changeChantierStatus(chantierId: string, newStatus: string
} }
} }
//ADD CHANTIER
export async function addChantier(chantierData: Omit<Chantier, 'id'>): Promise<string | null> { export async function addChantier(chantierData: Omit<Chantier, 'id'>): Promise<string | null> {
try { try {
const colRef = collection(db, "chantier"); const colRef = collection(db, "chantier");
@@ -103,3 +96,56 @@ export async function addChantier(chantierData: Omit<Chantier, 'id'>): Promise<s
return null; return null;
} }
} }
//CHANGE CHANTIER ANOMALIE STATUS
export async function changeAnomalieStatus(chantierId: string, anomalie_String: string, newStatus: 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");
}
} catch (err) {
console.error("Error", err);
}
}
//CHANGE CHANTIER ANOMALIE STATUS
export async function deleteAnomalie(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.filter((anomalie: any) => anomalie.description !== anomalie_String);
await updateDoc(chantierRef, { anomalies: updatedAnomalies });
console.log(`Anomalie deleted`);
} else {
console.error("Chantier not found");
}
} catch (err) {
console.error("Error", err);
}
}
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),
};
}