import { useState } from "react"; import { TextInput, View, Text, StyleSheet, Dimensions, ScrollView, TouchableOpacity, Checkbox } from "react-native"; import { baseIPAddress, newPlacePath } from "../../../constants/server"; import { SelectInput } from "../../components/form/select"; export default function NewPlace() { const [address, setaddress] = useState(""); const [city, setcity] = useState("Compiègne"); const [comp, setcomp] = useState(""); const [name, setname] = useState(""); const [placesRestantes, setplacesRestantes] = useState(""); const [prix, setprix] = useState(""); const [type, settype] = useState(""); const [equip, setequip] = useState({}); const [isMainCity, setIsMainCity] = useState(true); const [isMainType, setIsMainType] = useState(true); const cityList = ["Compiègne", "Margny", "Venette"] const typeList = ["Maison", "Appartement", "Studio"] const equipmentList = ["wifi", "canap"] { let dic = {} equipmentList.forEach(element => { dic[element] = false }); setequip(dic) } function createPlace() { if (!address || !city || !name || !prix || !type) { /** * If a necessary var is undefined, render a user friendly error */ return } /** * Création de l'object JSON */ let place = { "formatted_address": [address, city].join(", "), "name": name, "place_restant": placesRestantes, "price": prix, "types": type, "equipment" : equip } /** * Appel du back avec fetch * Either render an error or go to the next page */ let ans = fetch(baseIPAddress + newPlacePath, { method: "post", body: JSON.stringify(place) }) } function modifyElement(map, i, newVal) { map[i] = newVal return map } const style = StyleSheet.create({ input: { borderBottomWidth: 1, fontSize: 15, borderBottomColor: "grey", width: Dimensions.get('window').width * (1/2) }, titles: { fontSize: 30, fontWeight: "bold", marginTop: 25, marginBottom: 15 } }) return ( <View style={{marginTop:20, backgroundColor:"white", height:"100%"}} > <ScrollView style={{marginLeft:20, marginBottom: 20}}> <Text style={style.titles}>Adresse</Text> <TextInput style={style.input} onChangeText={setaddress} value={address} placeholder="Adresse"/> <TextInput style={style.input} onChangeText={setcomp} value={comp} placeholder="Complément"/> <SelectInput style={{fontSize:style.input.fontSize, marginTop:10, marginBottom:10}} SetIsMain={setIsMainCity} useOther={true} onChangeText={setcity} value={city} list={cityList}/> {isMainCity ? <TextInput style={style.input} onChangeText={setcity} value={city}/> : null} <Text style={style.titles}>Nom</Text> <TextInput style={style.input} onChangeText={setname} value={name} placeholder="Nom"/> <Text style={style.titles}>Places restantes</Text> <TextInput style={style.input} onChangeText={setplacesRestantes} value={placesRestantes} placeholder="Places restantes"/> <Text style={style.titles}>Prix</Text> <TextInput style={style.input} onChangeText={setprix} value={prix} placeholder="Prix"/> <Text style={style.titles}>Type de logement</Text> <SelectInput style={{fontSize:style.input.fontSize, marginTop:10, marginBottom:10}} SetIsMain={setIsMainType} useOther={true} onChangeText={settype} value={type} list={typeList}/> {isMainType ? <TextInput style={{...style.input, marginBottom:25}} onChangeText={settype} value={type}/> : null} <TouchableOpacity style={{borderRadius:1, backgroundColor:"black", padding:10, width:180}} onPress={createPlace}> <Text style={{fontSize:16, color:"white", fontWeight:"bold", textAlign:"center"}}>Ajouter le logement</Text> </TouchableOpacity> <View style={{flexDirection:"row"}}> {equipmentList.map((item) =>{ return ( <Checkbox value={equip[item]} onValueChange={(val) => setequip(modifyElement(val))} /> ) })} </View> </ScrollView> </View> ) }