Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions app/composables/useUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export function useUsers() {
users.value = (data as Profile[]) || []
}

async function addUser(user: Omit<Profile, 'created_at' | 'user_id'> & { password?: string }) {
async function addUser(
user: Omit<Profile, 'created_at' | 'user_id'> & { password?: string }
) {
loading.value = true
error.value = null

Expand All @@ -40,30 +42,32 @@ export function useUsers() {
await loadUsers()
} catch (err: any) {
console.error(err)
error.value = err.data?.message || err.message || 'Erro ao adicionar usuário.'
error.value =
err.data?.message || err.message || 'Erro ao adicionar usuário.'
throw err
} finally {
loading.value = false
}
}

async function updateUser(user: Partial<Profile> & { user_id: string }) {
async function updateUser(user: any) {
loading.value = true
error.value = null

try {
await $fetch('/api/users', {
method: 'PUT',
body: {
id: user.user_id,
id: user.id || user.user_id,
name: user.name,
role: user.role
}
})
await loadUsers()
} catch (err: any) {
console.error(err)
error.value = err.data?.message || err.message || 'Erro ao atualizar usuário.'
error.value =
err.data?.message || err.message || 'Erro ao atualizar usuário.'
throw err
} finally {
loading.value = false
Expand All @@ -81,7 +85,8 @@ export function useUsers() {
await loadUsers()
} catch (err: any) {
console.error(err)
error.value = err.data?.message || err.message || 'Erro ao excluir usuário.'
error.value =
err.data?.message || err.message || 'Erro ao excluir usuário.'
throw err
} finally {
loading.value = false
Expand Down
4 changes: 3 additions & 1 deletion app/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ const items = [
<UPageBody>
<div class="flex flex-col gap-4">
<h2 class="text-lg font-semibold">Bem-vindo ao Painel</h2>
<p class="text-gray-500">Selecione uma opção no menu lateral para começar.</p>
<p class="text-gray-500">
Selecione uma opção no menu lateral para começar.
</p>
</div>
</UPageBody>
</UDashboardPanelContent>
Expand Down
20 changes: 16 additions & 4 deletions app/pages/settings/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import * as z from 'zod'
import type { FormSubmitEvent } from '@nuxt/ui'

const { loadAuthUser, user } = useAuth()

// const fileRef = ref<HTMLInputElement>()

const profileSchema = z.object({
Expand All @@ -11,9 +13,11 @@ const profileSchema = z.object({

type ProfileSchema = z.output<typeof profileSchema>

await loadAuthUser()

const profile = reactive<Partial<ProfileSchema>>({
name: 'Benjamin Canac',
email: 'ben@nuxtlabs.com'
name: user.value.user_metadata.name,
email: user.value.email
})

const toast = useToast()
Expand Down Expand Up @@ -73,7 +77,10 @@ async function onSubmit(event: FormSubmitEvent<ProfileSchema>) {
required
class="flex max-sm:flex-col justify-between items-start gap-4"
>
<UInput v-model="profile.name" autocomplete="off" />
<UInput
v-model="profile.name"
autocomplete="off"
/>
</UFormField>
<USeparator />
<UFormField
Expand All @@ -83,7 +90,12 @@ async function onSubmit(event: FormSubmitEvent<ProfileSchema>) {
required
class="flex max-sm:flex-col justify-between items-start gap-4"
>
<UInput v-model="profile.email" type="email" autocomplete="off" />
<UInput
v-model="profile.email"
type="email"
:disabled="true"
autocomplete="off"
/>
</UFormField>
<!-- <USeparator />
<UFormField
Expand Down
3 changes: 2 additions & 1 deletion app/pages/settings/users.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const data = computed<User[]>(() =>
}`,
alt: u.name || u.email
},
role: u.role || 'user'
role: u.role || 'user',
created_at: u.created_at
}))
)

Expand Down
12 changes: 11 additions & 1 deletion app/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@ export interface Profile {
created_at: string
}

export type User = Profile
export interface User {
id: string
email: string
name: string
role: UserRole
avatar?: {
src: string
alt: string
}
created_at: string
}
13 changes: 11 additions & 2 deletions server/api/users.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,20 @@ export default defineEventHandler(async (event) => {
if (user.user) {
const { error: updateError } = await client
.from('profiles')
.update({ name, role })
.eq('user_id', user.user.id)
.upsert({
user_id: user.user.id,
name,
role,
email
}, { onConflict: 'user_id' })
.select()

if (updateError) {
console.error('Failed to update profile:', updateError)
throw createError({
statusCode: 500,
statusMessage: 'Failed to create profile record: ' + updateError.message
})
}
}

Expand Down