Added: beer add page; all beers page #11

Merged
fr merged 1 commits from fr/beer_add into main 2024-05-09 15:34:09 +02:00
4 changed files with 145 additions and 12 deletions

View File

@ -20,7 +20,7 @@ export default function TabLayout() {
backgroundColor: colors.darkSecondary, backgroundColor: colors.darkSecondary,
}, },
tabBarActiveTintColor: colors.gold, tabBarActiveTintColor: colors.gold,
headerShown: false, headerShown: true,
}} }}
sceneContainerStyle={{ backgroundColor: colors.dark }} sceneContainerStyle={{ backgroundColor: colors.dark }}
> >
@ -62,7 +62,10 @@ export default function TabLayout() {
/> />
{/* Hide following routes from bottom bar */} {/* Hide following routes from bottom bar */}
<Tabs.Screen name="beer/add" options={{ href: null }} /> <Tabs.Screen
name="beer/add"
options={{ href: null, title: "Add beer" }}
/>
</Tabs> </Tabs>
</View> </View>
); );

View File

@ -1,10 +1,86 @@
import { View } from "react-native"; import { StyleSheet, TextInput, View } from "react-native";
import Text from "@components/Text"; import { useState } from "react";
import Button from "@components/Button";
import { colors } from "@components/style";
export default function BeerAdd() { export default function BeerAdd() {
const [b_name, setBName] = useState("");
const [b_degree, setBDegree] = useState("");
const [b_packaging, setBPackaging] = useState("");
const [b_brand, setBBrand] = useState("");
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();
}
return ( return (
<View style={{ justifyContent: "center", alignItems: "center", flex: 1 }}> <View style={styles.container}>
<Text>Beer Add</Text> <View style={styles.form}>
<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}
onChangeText={(text) => setBDegree(text)}
placeholderTextColor="#aaaaaa"
/>
<TextInput
style={styles.input}
placeholder="Packaging"
value={b_packaging}
onChangeText={(text) => setBPackaging(text)}
placeholderTextColor="#aaaaaa"
/>
<Button title="Add beer" color={colors.green} onPress={addBeer} />
</View>
</View> </View>
); );
} }
const styles = StyleSheet.create({
container: {
flex: 1,
},
form: {
flex: 1,
alignItems: "center",
paddingTop: "10%",
gap: 15,
},
input: {},
input: {
height: "auto",
width: "60%",
borderColor: "gray",
borderWidth: 1,
borderRadius: 5,
padding: 10,
color: "#fff",
},
});

View File

@ -1,21 +1,74 @@
import { View } from "react-native"; import { StyleSheet, View, FlatList } from "react-native";
import Text from "@components/Text"; import Text from "@components/Text";
import Button from "@components/Button"; import Button from "@components/Button";
import { colors } from "@components/style"; import { colors } from "@components/style";
import { router } from "expo-router"; import { router } from "expo-router";
import { useEffect, useState } from "react";
export default function Tab() { export default function Tab() {
return ( const [data, setData] = useState([]);
<View style={{ justifyContent: "center", alignItems: "center", flex: 1 }}> useEffect(() => {
<Text>Tab BEER</Text> fetchData();
}, []);
async function fetchData() {
try {
const res = await fetch(`${process.env.EXPO_PUBLIC_API_URL}/beer/get`, {
method: "GET",
credentials: "include",
});
const data = await res.json();
setData(data.data);
} catch (err) {
console.error(err);
alert("Something went wrong");
}
}
return (
<View style={styles.container}>
<Button <Button
title="Add Beers" title="Add Beer"
color={colors.gold} color={colors.gold}
onPress={() => { onPress={() => {
router.replace("/beer/add"); router.replace("/beer/add");
}} }}
/> />
<FlatList
data={data}
style={styles.beerList}
keyExtractor={(item) => String(item._id)}
renderItem={({ item }) => (
<View style={styles.item}>
<Text>Name: {item.name}</Text>
<Text>Brand: {item.brand}</Text>
<Text>Degree: {item.degree}</Text>
<Text>Packaging: {item.packaging}</Text>
</View>
)}
/>
</View> </View>
); );
} }
export const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
marginTop: "5%",
},
beerList: {
width: "100%",
paddingHorizontal: "15%",
marginTop: "5%",
},
item: {
borderColor: "gray",
borderWidth: 1,
borderRadius: 10,
padding: 13,
marginBottom: "5%",
},
});

View File

@ -1,7 +1,6 @@
import { Redirect, Stack, Slot } from "expo-router"; import { Redirect, Stack, Slot } from "expo-router";
import { useAuth } from "@context/AuthContext"; import { useAuth } from "@context/AuthContext";
import { View, Text, StyleSheet } from "react-native"; import { View, Text, StyleSheet } from "react-native";
import { StatusBar } from "expo-status-bar";
export default function AppLayout() { export default function AppLayout() {
const { authState } = useAuth(); const { authState } = useAuth();
@ -14,10 +13,12 @@ export default function AppLayout() {
</View> </View>
); );
} }
if (!authState.authenticated) { if (!authState.authenticated) {
console.log("get the fuck out"); console.log("get the fuck out");
return <Redirect href="/login" />; return <Redirect href="/login" />;
} }
return ( return (
<Stack> <Stack>
<Stack.Screen name="(tabs)" options={{ headerShown: false }} /> <Stack.Screen name="(tabs)" options={{ headerShown: false }} />