catch error, change body to data

This commit is contained in:
Duncan Healy committed 2020-11-05 16:02:04 +00:00
1 parent 0148176bad
commit c870fdd252
7 files changed
+58 -37

No files matched your search

+1
View File
@@ -47,6 +47,7 @@ const logInSubmitted = (event) => {
.then((response) => {
document.querySelector('#access-token').value = response.data.access_token
document.querySelector('#refresh-token').value = response.data.refresh_token
// response.error ?
alert("Logged in as " + response.user.email)
})
.catch((err) => {
+14 -9
View File
@@ -12,10 +12,14 @@ function MyApp({ Component, pageProps }) {
const authUser = auth.currentUser()
if (!authUser) return
const { body: user } = await supabase.from('users')
const { data: user, error } = await supabase
.from('users')
.match({ id: authUser.id })
.select('*')
.single()
if (error) {
throw new Error(error)
}
setUser(user)
} catch (error) {
console.log(error)
@@ -26,15 +30,16 @@ function MyApp({ Component, pageProps }) {
function onSignOut() {
try {
const user = auth.currentUser();
user.signOut()
.then(response => {
console.log("User logged out")
const user = auth.currentUser()
user
.signOut()
.then((response) => {
console.log('User logged out')
window.location.reload()
})
.catch(error => {
console.log("Failed to logout user: %o", error)
});
.catch((error) => {
console.log('Failed to logout user: %o', error)
})
} catch (error) {
console.log(error)
}
@@ -44,7 +49,7 @@ function MyApp({ Component, pageProps }) {
<UserContext.Provider
value={{
user: user,
signOut: onSignOut
signOut: onSignOut,
}}
>
<Component {...pageProps} />
@@ -7,7 +7,7 @@ export default async function (req, res) {
const authBody = await auth.signUp({email, password})
supabase.setAccessToken(authBody.data.access_token)
const { body: user } = await supabase.from('users').insert([{ id: authBody.user.id, username: email, role }]).single()
const { data: user } = await supabase.from('users').insert([{ id: authBody.user.id, username: email, role }]).single()
return res.status(200).json({ ...user, refresh_token: authBody.refresh_token })
} catch (error) {
+12 -12
View File
@@ -94,9 +94,9 @@ export const useStore = (props) => {
*/
export const fetchChannels = async (setState) => {
try {
let { body } = await supabase.from('channels').select('*')
if (setState) setState(body)
return body
let { data } = await supabase.from('channels').select('*')
if (setState) setState(data)
return data
} catch (error) {
console.log('error', error)
}
@@ -109,8 +109,8 @@ export const fetchChannels = async (setState) => {
*/
export const fetchUser = async (userId, setState) => {
try {
let { body } = await supabase.from('users').eq('id', userId).select(`*`)
let user = body[0]
let { data } = await supabase.from('users').eq('id', userId).select(`*`)
let user = data[0]
if (setState) setState(user)
return user
} catch (error) {
@@ -125,13 +125,13 @@ export const fetchUser = async (userId, setState) => {
*/
export const fetchMessages = async (channelId, setState) => {
try {
let { body } = await supabase
let { data } = await supabase
.from('messages')
.eq('channel_id', channelId)
.select(`*, author:user_id(*)`)
.order('inserted_at', true)
if (setState) setState(body)
return body
if (setState) setState(data)
return data
} catch (error) {
console.log('error', error)
}
@@ -143,8 +143,8 @@ export const fetchMessages = async (channelId, setState) => {
*/
export const addChannel = async (slug) => {
try {
let { body } = await supabase.from('channels').insert([{ slug }])
return body
let { data } = await supabase.from('channels').insert([{ slug }])
return data
} catch (error) {
console.log('error', error)
}
@@ -158,8 +158,8 @@ export const addChannel = async (slug) => {
*/
export const addMessage = async (message, channel_id, user_id) => {
try {
let { body } = await supabase.from('messages').insert([{ message, channel_id, user_id }])
return body
let { data } = await supabase.from('messages').insert([{ message, channel_id, user_id }])
return data
} catch (error) {
console.log('error', error)
}
+3 -3
View File
@@ -19,9 +19,9 @@ export default class SupabaseSlackClone extends App {
signIn = async (id, username) => {
try {
let { body } = await supabase.from('users').match({ username }).select('id, username')
const existing = body[0]
const { body: user } = existing?.id
let { data,error } = await supabase.from('users').match({ username }).select('id, username')
const existing = data[0]
const { data: user } = existing?.id
? await supabase.from('users').update({ id, username }).match({ id }).single()
: await supabase.from('users').insert([{ id, username }]).single()
+5 -2
View File
@@ -71,12 +71,15 @@ const Todo = ({ todo, onDelete }) => {
const toggle = async () => {
try {
const { body } = await supabase
const { data,error } = await supabase
.from('todos')
.update({ is_complete: !isCompleted })
.eq('id', todo.id)
.single()
setIsCompleted(body.is_complete)
if(error){
throw new Error(error)
}
setIsCompleted(data.is_complete)
} catch (error) {
console.log('error', error)
}
+22 -10
View File
@@ -34,10 +34,10 @@ export const useStore = (props) => {
}
})
if (update) {
setTasks(t => [...tasks]) // update
setTasks((t) => [...tasks]) // update
} else {
tasks.unshift(newTask)
setTasks(t => [...tasks]) // new
setTasks((t) => [...tasks]) // new
}
}
}
@@ -61,8 +61,11 @@ export const useStore = (props) => {
export const addTask = async (task_text, list_id) => {
try {
let { body } = await supabase.from('tasks').insert([{ task_text, list_id }])
return body
let { data, error } = await supabase.from('tasks').insert([{ task_text, list_id }])
if (error) {
throw new Error(error)
}
return data
} catch (error) {
console.log('error', error)
}
@@ -70,8 +73,11 @@ export const addTask = async (task_text, list_id) => {
export const updateTask = async (task_id, values) => {
try {
let { body } = await supabase.from('tasks').eq('id', task_id).update(values)
return body
let { data, error } = await supabase.from('tasks').eq('id', task_id).update(values)
if (error) {
throw new Error(error)
}
return data
} catch (error) {
console.log('error', error)
}
@@ -79,8 +85,11 @@ export const updateTask = async (task_id, values) => {
export const createList = async (uuid) => {
try {
let { body } = await supabase.from('lists').insert([{ uuid }])
return body[0]
let { data, error } = await supabase.from('lists').insert([{ uuid }])
if (error) {
throw new Error(error)
}
return data[0]
} catch (error) {
console.log('error', error)
}
@@ -88,12 +97,15 @@ export const createList = async (uuid) => {
export const fetchList = async (list_uuid) => {
try {
let { body } = await supabase
let { data, error } = await supabase
.from('lists')
.eq('uuid', list_uuid)
.select(`*, tasks(*)`)
.single()
return body
if (error) {
throw new Error(error)
}
return data
} catch (error) {
console.log('error', error)
}