katoikia-app/mobile-ui/components/AreaComun.js

179 lines
4.8 KiB
JavaScript
Raw Permalink Normal View History

2022-08-23 04:20:07 +00:00
import React, {useContext, useEffect, useState} from "react";
2022-08-17 22:47:48 +00:00
import {
Box,
Heading,
VStack,
2022-08-23 04:50:38 +00:00
FormControl,
2022-08-17 22:47:48 +00:00
Button,
2022-08-23 04:20:07 +00:00
Center,
2022-08-23 04:50:38 +00:00
Select, CheckIcon, ScrollView
2022-08-17 22:47:48 +00:00
} from "native-base";
2022-08-23 04:20:07 +00:00
import { UserContext } from "../context/UserContext";
import { API } from "../environment/api";
2022-08-23 04:50:38 +00:00
import {TimePicker} from 'react-native-simple-time-picker';
import { View, StyleSheet } from "react-native";
import { number } from "prop-types";
2022-08-24 02:35:11 +00:00
import DateTimePicker from '@react-native-community/datetimepicker';
2022-08-17 22:47:48 +00:00
export default function AreaComun({navigation}){
2022-08-23 04:20:07 +00:00
const { user } = useContext(UserContext)
const [service, setService] = useState("");
const [areas, setAreas] = useState([])
const [isRequesting, setIsRequesting] = useState(false);
2022-08-24 17:15:27 +00:00
const [time, setTime] = useState(new Date())
2022-09-01 04:14:08 +00:00
const idComunidad = user.community_id
const date = new Date();
2022-09-01 04:14:08 +00:00
const [mode, setMode] = useState('time');
2022-08-23 04:50:38 +00:00
2022-09-01 05:41:36 +00:00
const [startDate, setStartDate] = useState();
const [startTime, setStartTime] = useState()
2022-08-23 04:20:07 +00:00
useEffect(() => {
const onRequestReservasData = async () => {
setIsRequesting(true);
2022-08-23 06:21:25 +00:00
2022-08-23 04:20:07 +00:00
try {
2022-09-01 04:14:08 +00:00
const jsonResponse = await fetch(`${API.BASE_URL}/commonArea/findByCommunity/` + `${idComunidad}`, {
2022-08-23 04:20:07 +00:00
method: "GET",
headers: {
'Content-Type': 'application/json'
}
})
const response = await jsonResponse.json();
2022-09-01 04:14:08 +00:00
// console.log(response.message);
2022-08-23 04:20:07 +00:00
setAreas(response.message);
} catch (error) {
console.log("ERROR:" + error);
}
setIsRequesting(false)
}
onRequestReservasData()
}, [user])
2022-08-23 06:21:25 +00:00
const postReserva = async() => {
2022-09-01 05:41:36 +00:00
// console.log(startDate.split('"')[1]);
// console.log(startTime.split('.')[0]);
2022-09-01 05:41:36 +00:00
const data = {
2022-08-23 06:21:25 +00:00
2022-09-01 05:41:36 +00:00
"time": startTime.split('.')[0],
"date": startDate.split('"')[1],
"user_id" : user._id,
"common_area_id": service._id,
"common_area_name": service.name,
"community_id": service.community_id
2022-08-23 06:21:25 +00:00
2022-09-01 05:41:36 +00:00
}
console.log(data);
try {
const jsonDataResponse = await fetch(`${API.BASE_URL}/reservation/createReservation`, {
cache: 'no-cache',
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
}
})
const response = await jsonDataResponse.json();
console.log(response.message);
2022-08-23 06:21:25 +00:00
2022-09-01 05:41:36 +00:00
} catch (error) {
console.log("ERROR:" + error);
}
2022-08-23 06:21:25 +00:00
}
2022-08-23 04:20:07 +00:00
2022-08-24 02:35:11 +00:00
const onChangeStart = (event, selectedDate) => {
2022-09-01 05:41:36 +00:00
// console.log(selectedDate);
const dateString = JSON.stringify(selectedDate).toString().split("T")
setStartDate(dateString[0])
setStartTime(dateString[1])
console.log(dateString);
// console.log(Date.toString(selectedDate));
2022-09-01 04:14:08 +00:00
const currentDate = selectedDate || time;
2022-08-24 02:35:11 +00:00
setTime(currentDate);
};
2022-08-23 04:20:07 +00:00
2022-08-17 22:47:48 +00:00
return (
<Center>
<Box safeArea p="2" w="90%" maxW="290" py="8">
<Heading size="lg" color="coolGray.800" _dark={{
color: "warmGray.50"
}} fontWeight="semibold">
Katoikia
</Heading>
<Heading mt="1" color="coolGray.600" _dark={{
color: "warmGray.200"
}} fontWeight="medium" size="xs">
Reserve su área común
</Heading>
2022-08-23 04:50:38 +00:00
<ScrollView showsVerticalScrollIndicator={false}>
2022-08-17 22:47:48 +00:00
<VStack space={3} mt="5">
2022-08-23 04:20:07 +00:00
<FormControl isRequired>
<FormControl.Label>Área común</FormControl.Label>
<Select selectedValue={service} minWidth="200" accessibilityLabel="Choose Service" placeholder="Elija su área común" _selectedItem={{
bg: "teal.600",
endIcon: <CheckIcon size="5" />
}} mt={1} onValueChange={itemValue => setService(itemValue)}>
{areas.map(item => (
2022-08-24 00:43:50 +00:00
<Select.Item key={item._id} label={item.name} value={item} />
2022-08-23 04:20:07 +00:00
))}
</Select>
</FormControl>
<FormControl isRequired>
2022-08-17 22:47:48 +00:00
<FormControl.Label>Hora de inicio</FormControl.Label>
2022-08-23 06:21:25 +00:00
<View >
2022-09-01 05:41:36 +00:00
<DateTimePicker minimumDate={date} mode="datetime" is24Hour value={time} onChange={onChangeStart}/>
2022-08-23 04:50:38 +00:00
</View>
2022-08-17 22:47:48 +00:00
</FormControl>
2022-08-23 04:20:07 +00:00
2022-08-17 22:47:48 +00:00
2022-08-24 02:35:11 +00:00
<Button mt="10" backgroundColor="tertiary.600" onPress={()=> postReserva()}>
2022-08-17 22:47:48 +00:00
Reservar
</Button>
2022-09-01 05:01:28 +00:00
<Button mt="3" colorScheme="error" onPress={() => navigation.navigate('Mis Reservas')}>
2022-08-17 22:47:48 +00:00
Cancelar
</Button>
</VStack>
2022-08-23 04:50:38 +00:00
</ScrollView>
2022-08-17 22:47:48 +00:00
</Box>
</Center>
)
2022-08-23 04:50:38 +00:00
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 10,
justifyContent: 'center',
alignItems: 'center',
height: 40
}
});