import SelectChantier from '@/components/selectChantier'; import { useRessources } from '@/app/ContextRessource'; import { Ressources } from '@/class/class'; import { ThemedButton } from '@/components/theme/themed-button'; import { ThemedText } from '@/components/theme/themed-text'; import { ThemedTextInput } from '@/components/theme/themed-textinput'; import { ThemedView } from '@/components/theme/themed-view'; import { addRessources } from '@/services/ressourcesService'; import React, { useState } from 'react'; import { Modal, ScrollView, StyleSheet, View } from 'react-native'; import Constants from 'expo-constants'; //pour connaître la taille de la barre menu de l'OS en haut type Dictionary = { [key: string]: string; }; const exempleNom: Dictionary = { 'Outil': "Boîte à outils", 'Machine': "Bulldozer", 'Ouvrier': "Charpentier" }; const exempleQte: Dictionary = { 'Outil': "12", 'Machine': "1", 'Ouvrier': "12" }; type Props = { ressourceType: 'Outil' | 'Machine' | 'Ouvrier'; }; export default function AddRessource({ressourceType, ...otherProps }: Props) { const { ressources, setRessources } = useRessources(); const [editMode,setEditMode] = useState(false); const [loading, setLoading] = useState(false); const [nom, setNom] = useState(''); const [quantite, setQuantite] = useState(''); const [quantiteDisponible,setQuantiteDisponible] = useState(''); const [openConfirmation,setOpenConfirmation] = useState(false); async function handleAddRessource() { setLoading(true); setOpenConfirmation(true); } async function onConfirm(): Promise { 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); setNom(''); setQuantite(''); setQuantiteDisponible(''); } }catch(error){ }finally{ setOpenConfirmation(false); setLoading(false); } } } function onCancel(): void { setOpenConfirmation(false); } function isValidRessource():Boolean{ return nom!= "" && quantite != "" } const renderValidationScreen = () => { return( Créer la nouvelle ressource {ressourceType} suivante ? : Nom: {nom===''?"NONE":nom} Quantité Total: {quantite===''?"0":quantite} onConfirm()}> Confirmer onCancel()}> Annuler ) } const renderInut = (name : string, preFill : string, value : string, setValue : ((text:string) => void),numeric:boolean) => { return ( {name}: ); }; return ( {editMode && } {editMode? "Edition d'un chantier" :"Ajouter une ressource " + ressourceType} {renderInut("Nom",exempleNom[ressourceType],nom,setNom,false)} {renderInut("Quantité Total",exempleQte[ressourceType],quantite,setQuantite,true)} handleAddRessource()} > Valider {openConfirmation && renderValidationScreen()} ); } const styles = StyleSheet.create({ back:{ height:"100%", width:"100%", }, container: { flex: 1, marginTop: Constants.statusBarHeight, //pour la barre menu du haut }, header: { marginTop:80, alignItems: "center", paddingHorizontal: 20, }, text: { fontSize: 22, fontWeight: "bold", marginBottom: 10, }, inputBack: { width: "100%", borderRadius: 10, backgroundColor: "transparent", }, inputLine:{ width: "100%", //flexDirection: 'row', paddingVertical: 5, //alignItems: "center", }, inputName: { fontSize: 16, }, input: { width: "100%", borderRadius: 10, padding: 10, fontSize: 16, }, overlay:{ backgroundColor:'#00000080', padding:"5%", paddingVertical:"20%", width:"100%", height:"100%", }, overlayView:{ borderRadius: 20, padding: 20, alignItems: "center", width: "100%", gap:5, //backgroundColor:'#ff0000', }, buttonValid:{ //borderWidth: 2, width:'100%', margin: 0, borderRadius: 15, padding: 10, height:60, alignItems: "center", justifyContent: 'center', }, summaryNewChantier:{ width:'100%', borderRadius: 15, padding:10, } });