shareUser

This commit is contained in:
2025-02-12 21:15:01 +01:00
parent 0101280404
commit 87da1dd9b7

View File

@@ -31,7 +31,7 @@
<q-card-actions>
<q-btn flat>Editar</q-btn>
<q-btn flat>Compartir</q-btn>
<q-btn flat @click="shareUser(lista.id)">Compartir</q-btn>
<q-space></q-space>
<q-btn class="bg-negative q-mr-md" flat>Borrar</q-btn>
</q-card-actions>
@@ -66,7 +66,6 @@ const getListas = async () => {
.from('listas')
.select(`*, profiles(*),items(is_done)`)
if (error) throw error
console.log('listas', listas)
store.listas = listas
ordenarArray(store.listas)
} catch (error) {
@@ -174,6 +173,77 @@ const unShareUser = async (user_id, lista_id, email) => {
}
}
const getProfileByEmail = async (email) => {
try {
const { data, error } = await supabase
.from('profiles')
.select()
.eq('email', email.trim())
if (error) throw error
return data[0]
} catch (error) {
if (error instanceof Error) {
$q.notify({
type: 'negative',
message: error.message
})
}
}
}
const shareUser = async (id) => {
$q.dialog({
title: 'Escriba',
message: 'Email del usuario',
html: true,
prompt: {
model: '',
isValid: val => val.length > 5, // << here is the magic
type: 'text'
},
ok: {
push: true,
},
cancel: {
push: true,
},
persistent: true
}).onOk(async (email) => {
try {
const profile = await getProfileByEmail(email)
if (profile.email === store.user.email) {
$q.notify({
type: 'negative',
message: 'No puede compartir la lista con usted mismo'
})
return
}
const { error } = await supabase
.from('share')
.insert([
{ user_id: profile.id, lista_id: id },
])
.select()
if (error) throw error
store.listas.filter(l => l.id === id)[0].profiles.push(profile)
} catch (error) {
if (error instanceof Error) {
if (error.message.includes('profile is undefined')) {
$q.notify({
type: 'negative',
message: 'El email no existe en la base de datos'
})
} else {
$q.notify({
type: 'negative',
message: error.message
})
}
}
}
})
}
onMounted(() => {
getListas()
})