This commit is contained in:
Alexis Leboeuf
2025-12-13 22:24:21 +01:00
4 changed files with 44 additions and 11 deletions

View File

@@ -10,10 +10,11 @@ import { FlatList, Image, StyleSheet, Text, View } from "react-native";
import { Ressources } from "../../class/class"; import { Ressources } from "../../class/class";
import { getRessources } from "../../services/ressourcesService"; import { getRessources } from "../../services/ressourcesService";
import SelectChantier from "@/components/selectChantier"; import SelectChantier from "@/components/selectChantier";
import { useRessources } from "../ContextRessource";
export default function GestionnaireRessource() { export default function GestionnaireRessource() {
const [search, setSearch] = useState(""); const [search, setSearch] = useState("");
const [ressource, setRessources] = useState<Ressources[]>([]); const {ressources, setRessources} = useRessources();
const [filterType, setFilterType] = useState("tout"); const [filterType, setFilterType] = useState("tout");
const [showFilterMenu, setShowFilterMenu] = useState(false); const [showFilterMenu, setShowFilterMenu] = useState(false);
const router = useRouter(); const router = useRouter();
@@ -30,7 +31,7 @@ export default function GestionnaireRessource() {
loadData(); loadData();
}, []); }, []);
const filteredData = ressource.filter((r) => { const filteredData = ressources.filter((r) => {
const matchName = r.name.toLowerCase().includes(search.toLowerCase()); const matchName = r.name.toLowerCase().includes(search.toLowerCase());
const matchType = filterType === "tout" || r.type === filterType; const matchType = filterType === "tout" || r.type === filterType;
return matchName && matchType; return matchName && matchType;

View File

@@ -24,7 +24,7 @@ export type User = {
}; };
export type Ressources = { export type Ressources = {
id: number; id: string;
name: string; name: string;
type: string; //"machine","ouvrier" type: string; //"machine","ouvrier"
Image: string; Image: string;

View File

@@ -9,7 +9,7 @@ import { StyleSheet, ScrollView, Button, TextInput, Text, View, Modal } from 're
import { useChantier } from '@/app/ContextChantier'; import { useChantier } from '@/app/ContextChantier';
import { useRessources } from '@/app/ContextRessource'; import { useRessources } from '@/app/ContextRessource';
import { useUser } from '@/app/ContextUser'; import { useUser } from '@/app/ContextUser';
import { getRessources, getUsers, addChantier } from '@/services/ressourcesService'; import { getRessources, getUsers, addChantier , addRessources} from '@/services/ressourcesService';
import { Chantier, Ressources, User } from '@/class/class'; import { Chantier, Ressources, User } from '@/class/class';
import { ThemedText } from '@/components/theme/themed-text'; import { ThemedText } from '@/components/theme/themed-text';
import { ThemedButton } from '@/components/theme/themed-button'; import { ThemedButton } from '@/components/theme/themed-button';
@@ -58,12 +58,32 @@ export default function AddRessource({ressourceType, ...otherProps }: Props) {
} }
async function onConfirm(): Promise<void> { async function onConfirm(): Promise<void> {
//TODO
//await changeChantierStatus(chantier.id,tempStatus)
//Il faut changer le UX
//setChantier({...chantier,etat: tempStatus})
if(isValidRessource()){ if(isValidRessource()){
try{
setLoading(true);
const nouvelleRessource : Ressources = {
id : '',
name: nom,
type : ressourceType,
quantity : parseInt(quantite),
available_quantity : parseInt(quantite),
Image : "",
allocation : [],
};
const id = await addRessources(nouvelleRessource);
if(id){
setRessources([...ressources,{...nouvelleRessource, id}]);
setOpenConfirmation(false); setOpenConfirmation(false);
setNom('');
setQuantite('');
setQuantiteDisponible('');
}
}catch(error){
}finally{
setOpenConfirmation(false);
setLoading(false);
}
} }
} }
@@ -71,7 +91,7 @@ export default function AddRessource({ressourceType, ...otherProps }: Props) {
setOpenConfirmation(false); setOpenConfirmation(false);
} }
function isValidRessource():Boolean{ function isValidRessource():Boolean{
return nom!= "" && quantite != "" && quantiteDisponible != "" return nom!= "" && quantite != ""
} }
const renderValidationScreen = () => { const renderValidationScreen = () => {
@@ -100,7 +120,6 @@ export default function AddRessource({ressourceType, ...otherProps }: Props) {
) )
} }
const renderInut = (name : string, preFill : string, value : string, setValue : ((text:string) => void),numeric:boolean) => { const renderInut = (name : string, preFill : string, value : string, setValue : ((text:string) => void),numeric:boolean) => {
return ( return (
<View style = {styles.inputLine}> <View style = {styles.inputLine}>

View File

@@ -37,6 +37,19 @@ export async function getRessources(): Promise<Ressources[]> {
return []; return [];
} }
} }
//ADD RESSOURCES
export async function addRessources(ressourceData: Omit<Ressources, 'id'>): Promise<string | null> {
try {
const colRef = collection(db, "ressources");
const ressourcesRef = await addDoc(colRef, ressourceData);
console.log(`Ressource ajoutée avec ID: ${ressourcesRef.id}`);
return ressourcesRef.id;
} catch (err) {
console.error("Error adding:", err);
return null;
}
}
///////////////////////////////////CHANTIER///////////////////////////////// ///////////////////////////////////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"));