diff --git a/src/pages/IndexPage.vue b/src/pages/IndexPage.vue index fb0c6cc..afff1a5 100644 --- a/src/pages/IndexPage.vue +++ b/src/pages/IndexPage.vue @@ -31,7 +31,7 @@ Editar - Compartir + Compartir Borrar @@ -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() })