Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Add TodoList.

Thorsten Schaeff0544a2c003b5
diff --git a/examples/todo-expo/App.tsx b/examples/todo-expo/App.tsx
index e250ae24810..40316c14301 100644
--- a/examples/todo-expo/App.tsx
+++ b/examples/todo-expo/App.tsx
@@ -4,7 +4,7 @@ import { StyleSheet, View, Platform } from 'react-native'
import { ThemeProvider, colors, Text } from 'react-native-elements'
import { Styles } from './lib/constants'
import { UserContextProvider, useUser } from './components/UserContext'
-import List from './components/List'
+import List from './components/TodoList'
import Auth from './components/Auth'
const theme = {
diff --git a/examples/todo-expo/components/List.tsx b/examples/todo-expo/components/List.tsx
deleted file mode 100644
index 5464ed23e38..00000000000
--- a/examples/todo-expo/components/List.tsx
+++ /dev/null
@@ -1,11 +0,0 @@
-import React from 'react'
-import { View } from 'react-native'
-import { Text } from 'react-native-elements'
-
-export default function List() {
- return (
- <View>
- <Text>TODO</Text>
- </View>
- )
-}
diff --git a/examples/todo-expo/components/TodoList.tsx b/examples/todo-expo/components/TodoList.tsx
new file mode 100644
index 00000000000..230c17cd6a3
--- /dev/null
+++ b/examples/todo-expo/components/TodoList.tsx
@@ -0,0 +1,126 @@
+import React, { useEffect, useState } from 'react'
+import { View, StyleSheet, ScrollView, SafeAreaView, FlatList } from 'react-native'
+import { Styles } from '../lib/constants'
+import { Text } from 'react-native-elements'
+import { supabase } from '../lib/initSupabase'
+import { useUser } from '../components/UserContext'
+
+import { Button, Input, ListItem, CheckBox } from 'react-native-elements'
+
+/** URL polyfill. Required for Supabase queries to work in React Native. */
+import 'react-native-url-polyfill/auto'
+
+type Todo = {
+ id: number
+ user_id: string
+ task: string
+ is_complete: boolean
+ inserted_at: Date
+}
+
+export default function TodoList() {
+ const { user } = useUser()
+ const [todos, setTodos] = useState<Array<Todo>>([])
+ const [newTaskText, setNewTaskText] = useState<string>('')
+
+ useEffect(() => {
+ fetchTodos()
+ }, [])
+
+ const fetchTodos = async () => {
+ const { data: todos, error } = await supabase
+ .from<Todo>('todos')
+ .select('*')
+ .order('id', { ascending: false })
+ if (error) console.log('error', error)
+ else setTodos(todos!)
+ }
+
+ const addTodo = async (taskText: string) => {
+ const task = taskText.trim()
+ console.log('newtask:', task)
+ if (task.length) {
+ const { data: todo, error } = await supabase
+ .from<Todo>('todos')
+ .insert({ task, user_id: user!.id })
+ .single()
+ if (error) console.log(error.message)
+ else {
+ setTodos([todo!, ...todos])
+ setNewTaskText('')
+ }
+ }
+ }
+
+ const toggleCompleted = async (id: number, is_complete: boolean) => {
+ const { data, error } = await supabase
+ .from<Todo>('todos')
+ .update({ is_complete: !is_complete })
+ .eq('id', id)
+ .single()
+ if (error) console.log(error)
+ else setTodos(todos.map((todo) => (todo.id === id ? data! : todo)))
+ }
+
+ const deleteTodo = async (id: number) => {
+ const { error } = await supabase.from<Todo>('todos').delete().eq('id', id)
+ if (error) console.log('error', error)
+ else setTodos(todos.filter((x) => x.id !== Number(id)))
+ }
+
+ return (
+ <View>
+ <View style={[styles.verticallySpaced, { marginTop: 20 }]}>
+ <Button title="Sign out" onPress={() => supabase.auth.signOut()} />
+ </View>
+ <View style={[styles.verticallySpaced, { marginTop: 20 }]}>
+ <Input
+ label="New todo"
+ leftIcon={{ type: 'font-awesome', name: 'tasks' }}
+ onChangeText={(text) => setNewTaskText(text)}
+ value={newTaskText}
+ />
+ <Button title="Add" onPress={() => addTodo(newTaskText)} />
+ </View>
+ <SafeAreaView style={styles.verticallySpaced}>
+ <FlatList
+ scrollEnabled={true}
+ data={todos}
+ keyExtractor={(item) => `${item.id}`}
+ renderItem={({ item: todo }) => (
+ <ListItem bottomDivider>
+ <ListItem.Content>
+ <View
+ style={[
+ { display: 'flex', flexDirection: 'row', justifyContent: 'space-between' },
+ ]}
+ >
+ <CheckBox
+ checked={todo.is_complete}
+ onPress={() => toggleCompleted(todo.id, todo.is_complete)}
+ />
+ <Text h3 style={{ margin: 'auto' }}>
+ {todo.task}
+ </Text>
+ <Button title="Delete" onPress={() => deleteTodo(todo.id)}></Button>
+ </View>
+ </ListItem.Content>
+ </ListItem>
+ )}
+ />
+ </SafeAreaView>
+ </View>
+ )
+}
+
+const styles = StyleSheet.create({
+ container: {
+ marginTop: 40,
+ padding: Styles.spacing,
+ },
+ verticallySpaced: {
+ paddingTop: 4,
+ paddingBottom: 4,
+ alignSelf: 'stretch',
+ },
+})
diff --git a/examples/todo-expo/package-lock.json b/examples/todo-expo/package-lock.json
index 9cd8fb70076..1d8a8d10a70 100644
--- a/examples/todo-expo/package-lock.json
+++ b/examples/todo-expo/package-lock.json
@@ -2481,6 +2481,15 @@
"node-int64": "^0.4.0"
}
},
+ "buffer": {
+ "version": "5.7.1",
+ "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz",
+ "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==",
+ "requires": {
+ "base64-js": "^1.3.1",
+ "ieee754": "^1.1.13"
+ }
+ },
"buffer-alloc": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/buffer-alloc/-/buffer-alloc-1.2.0.tgz",
@@ -4074,6 +4083,11 @@
"safer-buffer": ">= 2.1.2 < 3"
}
},
+ "ieee754": {
+ "version": "1.2.1",
+ "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz",
+ "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA=="
+ },
"image-size": {
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/image-size/-/image-size-0.6.3.tgz",
@@ -6126,6 +6140,11 @@
"once": "^1.3.1"
}
},
+ "punycode": {
+ "version": "2.1.1",
+ "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz",
+ "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A=="
+ },
"qs": {
"version": "6.9.4",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.9.4.tgz",
@@ -6301,6 +6320,14 @@
"resolved": "https://registry.npmjs.org/react-native-status-bar-height/-/react-native-status-bar-height-2.6.0.tgz",
"integrity": "sha512-z3SGLF0mHT+OlJDq7B7h/jXPjWcdBT3V14Le5L2PjntjjWM3+EJzq2BcXDwV+v67KFNJic5pgA26cCmseYek6w=="
},
+ "react-native-url-polyfill": {
+ "version": "1.2.0",
+ "resolved": "https://registry.npmjs.org/react-native-url-polyfill/-/react-native-url-polyfill-1.2.0.tgz",
+ "integrity": "sha512-hpLZ8RyS3oGVyTOe/HjoqVoCOSkeJvrCoEB3bJsY7t9uh7kpQDV6kgvdlECEafYpxe3RzMrKLVcmWRbPU7CuAw==",
+ "requires": {
+ "whatwg-url-without-unicode": "8.0.0-3"
+ }
+ },
"react-native-web": {
"version": "0.13.18",
"resolved": "https://registry.npmjs.org/react-native-web/-/react-native-web-0.13.18.tgz",
@@ -7574,6 +7601,11 @@
"defaults": "^1.0.3"
}
},
+ "webidl-conversions": {
+ "version": "5.0.0",
+ "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-5.0.0.tgz",
+ "integrity": "sha512-VlZwKPCkYKxQgeSbH5EyngOmRp7Ww7I9rQLERETtf5ofd9pGeswWiOtogpEO850jziPRarreGxn5QIiTqpb2wA=="
+ },
"websocket": {
"version": "1.0.33",
"resolved": "https://registry.npmjs.org/websocket/-/websocket-1.0.33.tgz",
@@ -7607,6 +7639,16 @@
"resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.5.0.tgz",
"integrity": "sha512-jXkLtsR42xhXg7akoDKvKWE40eJeI+2KZqcp2h3NsOrRnDvtWX36KcKl30dy+hxECivdk2BVUHVNrPtoMBUx6A=="
},
+ "whatwg-url-without-unicode": {
+ "version": "8.0.0-3",
+ "resolved": "https://registry.npmjs.org/whatwg-url-without-unicode/-/whatwg-url-without-unicode-8.0.0-3.tgz",
+ "integrity": "sha512-HoKuzZrUlgpz35YO27XgD28uh/WJH4B0+3ttFqRo//lmq+9T/mIOJ6kqmINI9HpUpz1imRC/nR/lxKpJiv0uig==",
+ "requires": {
+ "buffer": "^5.4.3",
+ "punycode": "^2.1.1",
+ "webidl-conversions": "^5.0.0"
+ }
+ },
"which": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz",
diff --git a/examples/todo-expo/package.json b/examples/todo-expo/package.json
index 154d84216da..5159efe024a 100644
--- a/examples/todo-expo/package.json
+++ b/examples/todo-expo/package.json
@@ -17,6 +17,7 @@
"react-dom": "16.13.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-39.0.4.tar.gz",
"react-native-elements": "^3.0.0-alpha.1",
+ "react-native-url-polyfill": "^1.2.0",
"react-native-web": "~0.13.12"
},
"devDependencies": {