2024-05-12 12:10:32 +02:00
|
|
|
import { StyleSheet, TextInput, View, Image } from "react-native";
|
2024-05-08 23:39:49 +02:00
|
|
|
import { useState } from "react";
|
|
|
|
import Button from "@components/Button";
|
2024-05-12 21:26:44 +02:00
|
|
|
import Text from "@components/Text";
|
2024-05-08 23:39:49 +02:00
|
|
|
import { colors } from "@components/style";
|
2024-05-12 12:10:32 +02:00
|
|
|
import * as ImagePicker from "expo-image-picker";
|
2024-05-12 19:43:29 +02:00
|
|
|
import DropDownPicker from "react-native-dropdown-picker";
|
2024-05-12 21:26:44 +02:00
|
|
|
const DropdownTheme = require("@components/DropdownTheme");
|
2024-05-13 16:52:20 +02:00
|
|
|
import { Platform } from "react-native";
|
2024-05-08 21:18:47 +02:00
|
|
|
|
|
|
|
export default function BeerAdd() {
|
2024-05-08 23:39:49 +02:00
|
|
|
const [b_name, setBName] = useState("");
|
|
|
|
const [b_degree, setBDegree] = useState("");
|
2024-05-12 22:59:15 +02:00
|
|
|
const [b_packaging, setBPackaging] = useState(null);
|
2024-05-08 23:39:49 +02:00
|
|
|
const [b_brand, setBBrand] = useState("");
|
2024-05-12 12:10:32 +02:00
|
|
|
const [image, setImage] = useState(null);
|
|
|
|
|
2024-05-12 19:43:29 +02:00
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
const [items, setItems] = useState([
|
2024-05-12 21:26:44 +02:00
|
|
|
{ label: "Tank beer", value: "tank" },
|
|
|
|
{ label: "Cask beer", value: "cask" },
|
2024-05-12 19:43:29 +02:00
|
|
|
{ label: "Glass bottle", value: "glass" },
|
2024-05-12 21:26:44 +02:00
|
|
|
{ label: "Can", value: "can" },
|
|
|
|
{ label: "PET bottle", value: "pet" },
|
2024-05-12 19:43:29 +02:00
|
|
|
]);
|
|
|
|
|
2024-05-12 21:26:44 +02:00
|
|
|
DropDownPicker.addTheme("DropdownTheme", DropdownTheme);
|
|
|
|
DropDownPicker.setTheme("DropdownTheme");
|
|
|
|
|
2024-05-12 12:10:32 +02:00
|
|
|
ImagePicker.getCameraPermissionsAsync(); //check if the user has granted permission to access the camera
|
|
|
|
const pickImage = async () => {
|
2024-05-12 19:12:11 +02:00
|
|
|
const permissionResult =
|
|
|
|
await ImagePicker.requestMediaLibraryPermissionsAsync();
|
|
|
|
|
|
|
|
if (permissionResult.granted === false) {
|
|
|
|
alert("You've refused to allow this appp to access your photos!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-12 12:10:32 +02:00
|
|
|
// No permissions request is necessary for launching the image library
|
2024-05-12 19:12:11 +02:00
|
|
|
const result = await ImagePicker.launchImageLibraryAsync({
|
|
|
|
mediaTypes: ImagePicker.MediaTypeOptions.Images,
|
2024-05-12 12:10:32 +02:00
|
|
|
allowsEditing: true,
|
2024-05-12 19:12:11 +02:00
|
|
|
aspect: [3, 4],
|
|
|
|
// quality: 1,
|
2024-05-12 12:10:32 +02:00
|
|
|
});
|
|
|
|
|
2024-05-12 19:12:11 +02:00
|
|
|
// Explore the result
|
|
|
|
console.log(result);
|
|
|
|
|
|
|
|
if (!result.canceled) {
|
|
|
|
setImage(result.assets[0].uri);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const openCamera = async () => {
|
|
|
|
// Ask the user for the permission to access the camera
|
|
|
|
const permissionResult = await ImagePicker.requestCameraPermissionsAsync();
|
|
|
|
|
|
|
|
if (permissionResult.granted === false) {
|
|
|
|
alert("You've refused to allow this app to access your camera!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await ImagePicker.launchCameraAsync();
|
|
|
|
|
|
|
|
// Explore the result
|
2024-05-12 12:10:32 +02:00
|
|
|
console.log(result);
|
|
|
|
|
|
|
|
if (!result.canceled) {
|
|
|
|
setImage(result.assets[0].uri);
|
|
|
|
}
|
|
|
|
};
|
2024-05-08 23:39:49 +02:00
|
|
|
|
2024-05-12 22:59:15 +02:00
|
|
|
function validateDegreeInput(text) {
|
|
|
|
let newText = "";
|
|
|
|
let numbers = "0123456789.";
|
|
|
|
|
|
|
|
for (var i = 0; i < text.length; i++) {
|
|
|
|
if (numbers.indexOf(text[i]) > -1) {
|
|
|
|
newText = newText + text[i];
|
|
|
|
setBDegree(newText);
|
|
|
|
} else {
|
|
|
|
// your call back function
|
|
|
|
alert("Please enter numbers only.");
|
|
|
|
setBDegree("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-08 23:39:49 +02:00
|
|
|
async function addBeer() {
|
|
|
|
// TODO: after the request - redirect to /beer/{new_beer_id}?; plus some modal about successful state
|
|
|
|
const req = await fetch(`${process.env.EXPO_PUBLIC_API_URL}/beer/add`, {
|
|
|
|
method: "POST",
|
|
|
|
credentials: "include",
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
body: JSON.stringify({
|
|
|
|
brand: b_brand,
|
|
|
|
name: b_name,
|
|
|
|
degree: b_degree,
|
|
|
|
packaging: b_packaging,
|
|
|
|
photos: null,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
const res = await req.json();
|
2024-05-12 12:10:32 +02:00
|
|
|
|
2024-05-12 17:47:06 +02:00
|
|
|
if (res.code == 201 && res.data._id) {
|
|
|
|
window.location.href = `/beer/${res.data._id}`;
|
2024-05-12 12:10:32 +02:00
|
|
|
} else {
|
|
|
|
alert(
|
|
|
|
"Beer was not added successfully. Please check your data and try again.",
|
|
|
|
);
|
|
|
|
}
|
2024-05-08 23:39:49 +02:00
|
|
|
}
|
|
|
|
|
2024-05-08 21:18:47 +02:00
|
|
|
return (
|
2024-05-08 23:39:49 +02:00
|
|
|
<View style={styles.container}>
|
|
|
|
<View style={styles.form}>
|
2024-05-12 21:26:44 +02:00
|
|
|
<Text style={styles.text}>
|
|
|
|
Spill your thoughts about the beer you just sipped!
|
|
|
|
</Text>
|
2024-05-08 23:39:49 +02:00
|
|
|
<TextInput
|
|
|
|
style={styles.input}
|
|
|
|
placeholder="Name"
|
|
|
|
value={b_name}
|
|
|
|
onChangeText={(text) => setBName(text)}
|
|
|
|
placeholderTextColor="#aaaaaa"
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
style={styles.input}
|
|
|
|
placeholder="Brand"
|
|
|
|
value={b_brand}
|
|
|
|
onChangeText={(text) => setBBrand(text)}
|
|
|
|
placeholderTextColor="#aaaaaa"
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
style={styles.input}
|
|
|
|
placeholder="Degree"
|
|
|
|
value={b_degree}
|
2024-05-12 22:59:15 +02:00
|
|
|
onChangeText={(text) => validateDegreeInput(text)}
|
2024-05-08 23:39:49 +02:00
|
|
|
placeholderTextColor="#aaaaaa"
|
2024-05-12 22:59:15 +02:00
|
|
|
keyboardType="numeric"
|
2024-05-13 16:52:20 +02:00
|
|
|
maxLength={3}
|
2024-05-08 23:39:49 +02:00
|
|
|
/>
|
2024-05-12 22:59:15 +02:00
|
|
|
|
2024-05-12 19:43:29 +02:00
|
|
|
<DropDownPicker
|
|
|
|
open={open}
|
2024-05-12 22:59:15 +02:00
|
|
|
value={b_packaging}
|
2024-05-12 19:43:29 +02:00
|
|
|
items={items}
|
|
|
|
setOpen={setOpen}
|
2024-05-12 22:59:15 +02:00
|
|
|
setValue={setBPackaging}
|
2024-05-12 19:43:29 +02:00
|
|
|
setItems={setItems}
|
|
|
|
placeholder={"What are you drinking from?"}
|
2024-05-12 21:26:44 +02:00
|
|
|
theme="DropdownTheme"
|
2024-05-12 22:59:15 +02:00
|
|
|
//searchable={true} //maybe we can use it later...
|
2024-05-12 19:43:29 +02:00
|
|
|
/>
|
2024-05-12 12:10:32 +02:00
|
|
|
<View style={styles.imageContainer}>
|
|
|
|
<Button
|
2024-05-12 21:26:44 +02:00
|
|
|
title="Open gallery"
|
2024-05-12 12:10:32 +02:00
|
|
|
onPress={pickImage}
|
2024-05-12 21:26:44 +02:00
|
|
|
buttonStyle={styles.imageButton}
|
|
|
|
textStyle={styles.imageTextButton}
|
2024-05-12 12:10:32 +02:00
|
|
|
/>
|
2024-05-12 19:12:11 +02:00
|
|
|
|
2024-05-13 16:52:20 +02:00
|
|
|
{Platform.OS != "web" ? (
|
|
|
|
<Button
|
|
|
|
onPress={openCamera}
|
|
|
|
title={"Open camera"}
|
|
|
|
buttonStyle={styles.imageButton}
|
|
|
|
textStyle={styles.imageTextButton}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
false
|
|
|
|
)}
|
|
|
|
|
|
|
|
{image && <Image source={{ uri: image }} style={styles.image} />}
|
2024-05-12 12:10:32 +02:00
|
|
|
</View>
|
2024-05-12 21:58:18 +02:00
|
|
|
{image && <Image source={{ uri: image }} style={styles.image} />}
|
2024-05-12 21:26:44 +02:00
|
|
|
<Button title="Add beer" color={colors.gold} onPress={addBeer} />
|
2024-05-08 23:39:49 +02:00
|
|
|
</View>
|
2024-05-08 21:18:47 +02:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2024-05-08 23:39:49 +02:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
2024-05-12 12:10:32 +02:00
|
|
|
width: "100%",
|
|
|
|
height: "100%",
|
|
|
|
alignItems: "center",
|
2024-05-12 21:43:05 +02:00
|
|
|
display: "flex",
|
2024-05-08 23:39:49 +02:00
|
|
|
},
|
|
|
|
form: {
|
|
|
|
alignItems: "center",
|
|
|
|
gap: 15,
|
2024-05-12 21:26:44 +02:00
|
|
|
width: "80%",
|
2024-05-08 23:39:49 +02:00
|
|
|
},
|
|
|
|
input: {
|
|
|
|
height: "auto",
|
2024-05-12 12:10:32 +02:00
|
|
|
width: "100%",
|
2024-05-08 23:39:49 +02:00
|
|
|
borderColor: "gray",
|
|
|
|
borderWidth: 1,
|
2024-05-12 12:10:32 +02:00
|
|
|
borderRadius: 10,
|
|
|
|
padding: 13,
|
2024-05-08 23:39:49 +02:00
|
|
|
color: "#fff",
|
|
|
|
},
|
2024-05-12 12:10:32 +02:00
|
|
|
imageContainer: {
|
|
|
|
alignItems: "center",
|
|
|
|
justifyContent: "center",
|
2024-05-12 21:26:44 +02:00
|
|
|
display: "flex",
|
|
|
|
flexDirection: "row",
|
|
|
|
gap: 10,
|
|
|
|
},
|
|
|
|
imageButton: {
|
|
|
|
backgroundColor: colors.dark,
|
|
|
|
borderColor: "gray",
|
|
|
|
borderWidth: 1,
|
|
|
|
borderRadius: 10,
|
|
|
|
},
|
|
|
|
imageTextButton: {
|
|
|
|
color: colors.white,
|
2024-05-12 12:10:32 +02:00
|
|
|
},
|
|
|
|
image: {
|
|
|
|
width: 150,
|
|
|
|
height: 150,
|
|
|
|
},
|
2024-05-12 21:26:44 +02:00
|
|
|
text: {
|
|
|
|
color: colors.white,
|
|
|
|
fontSize: 24,
|
|
|
|
textAlign: "center",
|
|
|
|
paddingBottom: "3%",
|
2024-05-12 21:43:05 +02:00
|
|
|
paddingTop: "10%",
|
2024-05-12 21:26:44 +02:00
|
|
|
},
|
2024-05-08 23:39:49 +02:00
|
|
|
});
|