Place.tsx 2.39 KB
Newer Older
1
import React, { useEffect, useState } from "react";
Lucas Simon Susin's avatar
Lucas Simon Susin committed
2
3
4
5
6
7
8
9
10
11
import { Container,
	ContainerDown,
	ContainerPlaceName,
	ContainerPlaceAdress,
	ContainerPlacePrice,
	ContainerText,
	HighlitedImage,
	ContainerPlaceText,
	ContainerPlaceSizeCapacity,
	ContainerPlaceHeader } from './styles';
12
13
14
import { TouchableOpacity, Alert } from "react-native";
import { MaterialIcons } from '@expo/vector-icons';
import { colors } from '../../config/Theme';
Douglas Dallavale's avatar
Douglas Dallavale committed
15
import { PlaceProps } from "../../interfaces/PlaceProps/PlaceProps.interface";
Lucas Simon Susin's avatar
Fixes    
Lucas Simon Susin committed
16
import Badge from '../../components/Badge/Badge';
17
import Loading from '../../components/Loading/Loading';
18

19
20
import PlacesService from '../../services/PlacesService';

21
export default function Place ({ spaceId, title, address, capacity, size, price, onPress }: PlaceProps) {
22
	const [imageURL, setImageURL] = useState([] as string[]);
23
	const [favorite, setFavorite] = useState(false);
24
25
26
	const [loading, setLoading] = useState(true);

	async function setUpImages() {
27
28
29
30
31
32
33
34
35
36
37
38
39
		const results = await PlacesService.retrieveThumbnailsItems(spaceId)

		if (results?.items?.length) {
			const [first, ...rest] = results.items;

			const firstUrl = await first.getDownloadURL();

			setImageURL(imageURL => [firstUrl, ...imageURL]);
			setLoading(false);

			const urls: string[] = await Promise.all(rest.map((item, i) => rest[i].getDownloadURL()))
			setImageURL(imageURL => [...imageURL, ...urls]);
		}
40
41
42
43
44
	}

	useEffect(() => {
		setUpImages()
	}, [])
45
46

	return (
47
		<TouchableOpacity onPress={() => onPress(imageURL)}>
48
			<Container>
Lucas Simon Susin's avatar
Lucas Simon Susin committed
49

50
				<ContainerDown>
51
52
					{loading ?
					<Loading /> :
53
54
					<HighlitedImage
						source={{ uri: imageURL[0] }}
55
					/>}
56
				</ContainerDown>
Lucas Simon Susin's avatar
Lucas Simon Susin committed
57

58
				<ContainerText>
Lucas Simon Susin's avatar
Fixes    
Lucas Simon Susin committed
59

60
61
62
63
					<ContainerPlaceHeader>
						<ContainerPlaceName>{title}</ContainerPlaceName>
						<ContainerPlaceAdress>{address}</ContainerPlaceAdress>
					</ContainerPlaceHeader>
Lucas Simon Susin's avatar
Fixes    
Lucas Simon Susin committed
64

65
					<ContainerPlaceSizeCapacity>
Lucas Simon Susin's avatar
Lucas Simon Susin committed
66
67
						<Badge name="people" text="pessoas" number={capacity} size={12} />
						<Badge name="crop" text="m²" number={size} size={12} />
68
					</ContainerPlaceSizeCapacity>
Lucas Simon Susin's avatar
Lucas Simon Susin committed
69

70
					<ContainerPlacePrice>
LucasSusin's avatar
LucasSusin committed
71
						<ContainerPlaceText>{price}</ContainerPlaceText>
72
73
						<TouchableOpacity onPress={() => setFavorite(!favorite)}>
							<MaterialIcons name={favorite ? "favorite" : "favorite-border"} size={36} color={colors.primary} />
74
75
						</TouchableOpacity>
					</ContainerPlacePrice>
Lucas Simon Susin's avatar
Lucas Simon Susin committed
76

77
				</ContainerText>
Lucas Simon Susin's avatar
Lucas Simon Susin committed
78

79
80
81
82
83
84
85
			</Container>
		</TouchableOpacity>

	);
}