addChantier / selectMachine / machineSummary : presque fini

This commit is contained in:
Rochas
2025-12-13 15:35:01 +01:00
parent 1407d3b20f
commit e8e30d541d
4 changed files with 145 additions and 46 deletions

View File

@@ -20,6 +20,10 @@ import Constants from 'expo-constants'; //pour connaître la taille de la barre
import SelectChafChantier from '@/components/selectChefChantier'; import SelectChafChantier from '@/components/selectChefChantier';
import SelectMachine from '@/components/selectMachine'; import SelectMachine from '@/components/selectMachine';
type RessourcesQte = [Ressources, number];
//Uniquement accessible par le RESPONSSABLE du chantier //Uniquement accessible par le RESPONSSABLE du chantier
//Pour créer ou modifier un chantier //Pour créer ou modifier un chantier
export default function AddChantier() { export default function AddChantier() {
@@ -37,8 +41,8 @@ export default function AddChantier() {
const [adresse, setAdresse] = useState(''); const [adresse, setAdresse] = useState('');
const [duree, setDuree] = useState(''); const [duree, setDuree] = useState('');
const [contact, setContact] = useState(''); const [contact, setContact] = useState('');
const [engins, setEngins] = useState<Ressources[]>(); //de type enfin /!\ const [machine, setMachines] = useState<RessourcesQte[]>(); //de type enfin /!\
const [materiels, setMateriels] = useState<Ressources>(); //de type material (outils) /!\ const [materiels, setMateriels] = useState<RessourcesQte[][]>(); //de type material (outils) /!\
const [showDateSelect,setSowDateSelect] = useState(false); const [showDateSelect,setSowDateSelect] = useState(false);
const [openConfirmation,setOpenConfirmation] = useState(false); const [openConfirmation,setOpenConfirmation] = useState(false);
@@ -46,6 +50,9 @@ export default function AddChantier() {
const [userSelect, setUserSelect] = useState<string[]>([]); const [userSelect, setUserSelect] = useState<string[]>([]);
const [ressourcesSelect, setRessourcesSelect] = useState<string[]>([]); const [ressourcesSelect, setRessourcesSelect] = useState<string[]>([]);
async function handleAddChantier() { async function handleAddChantier() {
setLoading(true); setLoading(true);
setOpenConfirmation(true); setOpenConfirmation(true);
@@ -59,23 +66,23 @@ export default function AddChantier() {
} }
}; };
async function onConfirm(): Promise<void> { async function onConfirm(): Promise<void> {
//TODO //TODO
//await changeChantierStatus(chantier.id,tempStatus) //await changeChantierStatus(chantier.id,tempStatus)
//Il faut changer le UX //Il faut changer le UX
//setChantier({...chantier,etat: tempStatus}) //setChantier({...chantier,etat: tempStatus})
if(isValidChantier()){ if(isValidChantier()){
setOpenConfirmation(false); setOpenConfirmation(false);
} }
} }
function onCancel(): void { function onCancel(): void {
setOpenConfirmation(false); setOpenConfirmation(false);
} }
function isValidChantier(): boolean { function isValidChantier(): boolean {
return objet!=="" && duree!=='' && adresse!=='' && contact!=='' && chefChantier!==undefined; //TODO return objet!=="" && duree!=='' && adresse!=='' && contact!=='' && chefChantier!==undefined; //TODO
} }
const renderValidationScreen = () => { const renderValidationScreen = () => {
return( return(
@@ -165,17 +172,21 @@ export default function AddChantier() {
{//renderInut("Date de départ","TOTO : JOUR + Demi journé",date,setDate) {//renderInut("Date de départ","TOTO : JOUR + Demi journé",date,setDate)
} }
{renderInutDate("Date de départ")} {renderInutDate("Date de départ")}
{renderInut("Estimation de la durée (en demi-journées)","14",duree,setDuree,false)} {renderInut("Estimation de la durée (en demi-journées)","14",duree,setDuree,true)}
{renderInut("Adresse","1 Rue de la Coutellerie, Paris",adresse,setAdresse,false)} {renderInut("Adresse","1 Rue de la Coutellerie, Paris",adresse,setAdresse,false)}
{renderInut("Contact client","07 01 02 03 04 05",contact,setContact,true)} {renderInut("Contact client","07 01 02 03 04 05",contact,setContact,true)}
<View style = {styles.inputLine}>
<ThemedText style = {styles.inputName}>Engins:</ThemedText>
<SelectMachine style = {styles.input} sendMachines={setEngins}/>
</View>
<View style = {styles.inputLine}> <View style = {styles.inputLine}>
<ThemedText style = {styles.inputName}>Chef de chantier:</ThemedText> <ThemedText style = {styles.inputName}>Chef de chantier:</ThemedText>
<SelectChafChantier style = {styles.input} sendChefChantier={setChefChantier}/> <SelectChafChantier style = {styles.input} sendChefChantier={setChefChantier}/>
</View> </View>
<View style = {styles.inputLine}>
<ThemedText style = {styles.inputName}>Vehicules et machines:</ThemedText>
<SelectMachine style = {styles.input} sendMachines={setMachines}/>
</View>
<View style = {styles.inputLine}>
<ThemedText style = {styles.inputName}>TODO pareil que vehicule mais pour ouvrier:</ThemedText>
<SelectMachine style = {styles.input} sendMachines={setMachines}/>
</View>

View File

@@ -16,6 +16,7 @@ import { Platform, UIManager } from 'react-native';
import { ChantierProvider } from "./ContextChantier"; import { ChantierProvider } from "./ContextChantier";
import { UserProvider } from "./ContextUser"; import { UserProvider } from "./ContextUser";
import { RessourcesProvider } from "./ContextRessource"; import { RessourcesProvider } from "./ContextRessource";
import LoginScreen from "./login/login";
export const unstable_settings = { export const unstable_settings = {
@@ -60,6 +61,7 @@ export default function RootLayout() {
<ChantierProvider> <ChantierProvider>
<RessourcesProvider> <RessourcesProvider>
<ThemeProvider value={colorScheme === "dark" ? DarkTheme : DefaultTheme}> <ThemeProvider value={colorScheme === "dark" ? DarkTheme : DefaultTheme}>
//faut que le login soit ici
<Stack> <Stack>
<Stack.Screen name="(tabs)" options={{ headerShown: false }} /> <Stack.Screen name="(tabs)" options={{ headerShown: false }} />
<Stack.Screen name="selectChantier" options={{ headerShown: false }}/> <Stack.Screen name="selectChantier" options={{ headerShown: false }}/>

View File

@@ -0,0 +1,68 @@
import { Chantier, Ressources } from '@/class/class';
import { ThemedView, } from '@/components/theme/themed-view';
import React, { useState } from 'react';
import { Image, StyleProp, StyleSheet, View, ViewStyle } from 'react-native';
import { ThemedText } from './theme/themed-text';
import { ThemedButton } from './theme/themed-button';
type RessourcesQte = [Ressources, number];
type Props = {
machine:Ressources;
qte:number;
sendMachine: (machine: RessourcesQte) => void;
style?: StyleProp<ViewStyle>;
};
export default function MachineSummary({machine,qte,style,sendMachine, ...otherProps }: Props) {
const [count,setCount] = useState(qte);
function onPressAdd(machine: Ressources): void {
if(count<machine.quantity){
setCount(count+1);
sendMachine([machine, count+1]);
}
}
function onPressSub(machine: Ressources): void {
if(count>0){
setCount(count-1);
sendMachine([machine, count-1]);
}
}
return(
<View style={style}>
<ThemedView lvl={2} border={3} style={{padding:10,width:"100%",borderRadius:10,flexDirection: 'row',justifyContent: 'space-between',}}>
<View>
<ThemedText>{machine.id}</ThemedText>
<ThemedText>{machine.name}</ThemedText>
<ThemedText>{machine.quantity}</ThemedText>
<ThemedText>{machine.type}</ThemedText>
</View>
<View style={{alignItems:"center"}}>
<ThemedButton style={styles.button} lvl={3} onPress={() => onPressAdd(machine)}>
<ThemedText>+</ThemedText>
</ThemedButton>
<ThemedText>{count}/{machine.quantity}</ThemedText>
<ThemedButton style={styles.button} lvl={3} onPress={() => onPressSub(machine)}>
<ThemedText>-</ThemedText>
</ThemedButton>
</View>
</ThemedView>
</View>
)
}
const styles = StyleSheet.create({
button:{
padding:5,
marginHorizontal:10,
alignItems:"center",
borderRadius: 20,
width: 40,
height: 40,
},
});

View File

@@ -8,18 +8,21 @@ import { ThemedText } from './theme/themed-text';
import { ThemedView } from "./theme/themed-view"; import { ThemedView } from "./theme/themed-view";
import { Ressources, User } from '@/class/class'; import { Ressources, User } from '@/class/class';
import { getRessources } from "@/services/ressourcesService"; import { getRessources } from "@/services/ressourcesService";
import MachineSummary from './machineSummary';
const { width, height } = Dimensions.get("window"); const { width, height } = Dimensions.get("window");
type RessourcesQte = [Ressources, number];
type Props = { type Props = {
sendMachines: (machine: Ressources[]) => void; sendMachines: (machine: RessourcesQte[]) => void;
style?: StyleProp<ViewStyle>; style?: StyleProp<ViewStyle>;
}; };
export default function SelectMachine({style,sendMachines: sendMachines , ...otherProps }: Props) { export default function SelectMachine({style,sendMachines: sendMachines , ...otherProps }: Props) {
const { chantier, setChantier} = useChantier(); const { chantier, setChantier} = useChantier();
const [machines, setMachines] = useState<Ressources[]>([]); const [machines, setMachines] = useState<RessourcesQte[]>([]);
const [tempStatus, setTempStatus] = useState(""); const [tempStatus, setTempStatus] = useState("");
const [isOpen,setIsOpen] = useState(false); const [isOpen,setIsOpen] = useState(false);
const [openConfirmation,setOpenConfirmation] = useState(false); const [openConfirmation,setOpenConfirmation] = useState(false);
@@ -40,34 +43,49 @@ export default function SelectMachine({style,sendMachines: sendMachines , ...oth
}, []); }, []);
useEffect(() => {
sendMachines(machines);
}, [machines])
function onPressOpen(): void { function onPressOpen(): void {
setIsOpen(!isOpen); setIsOpen(!isOpen);
} }
function onPressRessource(machine: Ressources): void{ function getTotalMachine(): number{
const exists = machines.some(item => item.name === machine.name); var total = 0;
if(exists){ machines.forEach(element => {
setMachines(prev => prev.filter(item => item.name !== machine.name)); total += element[1]
} });
else{ return total;
setMachines(prevItem => [...prevItem, machine]);
}
sendMachines(machines);
//setIsOpen(false);
} }
const MachineSummary = ({ item }: { item: Ressources }) => { function addMachine(machine: RessourcesQte): void{
if(machine[1]>0){
setMachines(prev =>
prev.some(i => i[0].name === machine[0].name)
? prev.map(i =>
i[0].name === machine[0].name
? [i[0], machine[1]]
: i
)
: [...prev, machine]
);
}
else{
setMachines(prev => prev.filter(item => item[0].name !== machine[0].name));
}
}
const MachineSummaryItem = ({ item }: { item: Ressources }) => {
if (!item) return null; if (!item) return null;
const exists = machines.some(i => i.name === item.name); const machineQte = machines.find(([r]) => r.name === item.name);
const qte = machineQte? machineQte[1]:0;
return( return(
<View style={{padding:10,width:"100%"}}> <MachineSummary style={{padding:10,width:"100%"}} machine={item} qte={qte} sendMachine={addMachine}></MachineSummary>
<ThemedButton lvl={exists?2:0} border={exists?3:0} style={{padding:10,width:"100%",borderRadius:10}} onPress={() => {onPressRessource(item)}}>
<ThemedText>{item.id}</ThemedText>
<ThemedText>{item.name}</ThemedText>
<ThemedText>{item.quantity}</ThemedText>
<ThemedText>{item.type}</ThemedText>
</ThemedButton>
</View>
) )
} }
@@ -81,7 +99,7 @@ export default function SelectMachine({style,sendMachines: sendMachines , ...oth
<FlatList <FlatList
style={{width:"100%"}} style={{width:"100%"}}
data={listMachines} data={listMachines}
renderItem={MachineSummary} renderItem={MachineSummaryItem}
keyExtractor={(_, index) => index.toString()} keyExtractor={(_, index) => index.toString()}
/> />
@@ -97,7 +115,7 @@ export default function SelectMachine({style,sendMachines: sendMachines , ...oth
return( return(
<ThemedButton style={style} lvl={1} onPress={() => onPressOpen()}> <ThemedButton style={style} lvl={1} onPress={() => onPressOpen()}>
<ThemedText style={styles.centeredText}>{machines?machines.length+" machine(s) sélectionné":"sélectionner des machine(s)"}</ThemedText> <ThemedText style={styles.centeredText}>{machines? getTotalMachine()+" machine(s) sélectionné dont "+ machines.length+" différente":"sélectionner des machine(s)"}</ThemedText>
{isOpen && MachineSearch()} {isOpen && MachineSearch()}
</ThemedButton> </ThemedButton>