120 lines
3.8 KiB
TypeScript
120 lines
3.8 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { SafeAreaView, Text, TextInput, Button, FlatList, View, StyleSheet } from "react-native";
|
|
import { StatusBar } from "expo-status-bar";
|
|
import type { Session } from "@supabase/supabase-js";
|
|
import { supabase } from "./lib/supabase";
|
|
|
|
interface Note {
|
|
id: number;
|
|
body: string;
|
|
}
|
|
|
|
export default function App() {
|
|
const [session, setSession] = useState<Session | null>(null);
|
|
const [email, setEmail] = useState("");
|
|
const [password, setPassword] = useState("");
|
|
const [error, setError] = useState("");
|
|
const [notes, setNotes] = useState<Note[]>([]);
|
|
const [draft, setDraft] = useState("");
|
|
|
|
useEffect(() => {
|
|
supabase.auth.getSession().then(({ data }) => setSession(data.session));
|
|
const { data: sub } = supabase.auth.onAuthStateChange((_event, nextSession) => {
|
|
setSession(nextSession);
|
|
});
|
|
return () => sub.subscription.unsubscribe();
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (!session) return;
|
|
void loadNotes();
|
|
}, [session]);
|
|
|
|
async function loadNotes(): Promise<void> {
|
|
const { data, error: selectError } = await supabase
|
|
.from("notes")
|
|
.select("id, body")
|
|
.order("id", { ascending: false });
|
|
if (selectError) {
|
|
setError(selectError.message);
|
|
return;
|
|
}
|
|
setNotes(data ?? []);
|
|
}
|
|
|
|
async function handleSignUp(): Promise<void> {
|
|
setError("");
|
|
const { error: signUpError } = await supabase.auth.signUp({ email, password });
|
|
if (signUpError) setError(signUpError.message);
|
|
}
|
|
|
|
async function handleSignIn(): Promise<void> {
|
|
setError("");
|
|
const { error: signInError } = await supabase.auth.signInWithPassword({ email, password });
|
|
if (signInError) setError(signInError.message);
|
|
}
|
|
|
|
async function handleAddNote(): Promise<void> {
|
|
if (!draft.trim() || !session) return;
|
|
const { error: insertError } = await supabase
|
|
.from("notes")
|
|
.insert({ body: draft.trim(), user_id: session.user.id });
|
|
if (insertError) {
|
|
setError(insertError.message);
|
|
return;
|
|
}
|
|
setDraft("");
|
|
await loadNotes();
|
|
}
|
|
|
|
if (!session) {
|
|
return (
|
|
<SafeAreaView style={styles.container}>
|
|
<StatusBar style="auto" />
|
|
<Text style={styles.title}>expo-scribase-starter</Text>
|
|
<TextInput
|
|
style={styles.input}
|
|
placeholder="you@example.com"
|
|
autoCapitalize="none"
|
|
value={email}
|
|
onChangeText={setEmail}
|
|
/>
|
|
<TextInput
|
|
style={styles.input}
|
|
placeholder="password (6+ chars)"
|
|
secureTextEntry
|
|
value={password}
|
|
onChangeText={setPassword}
|
|
/>
|
|
<Button title="Sign up" onPress={handleSignUp} />
|
|
<Button title="Sign in" onPress={handleSignIn} />
|
|
{error ? <Text style={styles.error}>{error}</Text> : null}
|
|
</SafeAreaView>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<SafeAreaView style={styles.container}>
|
|
<StatusBar style="auto" />
|
|
<Text style={styles.title}>Your notes</Text>
|
|
<Text>{session.user.email}</Text>
|
|
<View style={styles.row}>
|
|
<TextInput style={[styles.input, styles.flex1]} placeholder="New note" value={draft} onChangeText={setDraft} />
|
|
<Button title="Add" onPress={handleAddNote} />
|
|
</View>
|
|
{error ? <Text style={styles.error}>{error}</Text> : null}
|
|
<FlatList data={notes} keyExtractor={(n) => String(n.id)} renderItem={({ item }) => <Text>{item.body}</Text>} />
|
|
<Button title="Sign out" onPress={() => void supabase.auth.signOut()} />
|
|
</SafeAreaView>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
container: { flex: 1, padding: 24, gap: 12, justifyContent: "center" },
|
|
title: { fontSize: 22, fontWeight: "600" },
|
|
input: { borderWidth: 1, borderColor: "#ccc", borderRadius: 8, padding: 10 },
|
|
row: { flexDirection: "row", gap: 8, alignItems: "center" },
|
|
flex1: { flex: 1 },
|
|
error: { color: "crimson" },
|
|
});
|