Skip to content

Commit 48af5f6

Browse files
committed
doc: add useUser example
1 parent abe80b9 commit 48af5f6

File tree

3 files changed

+79
-17
lines changed

3 files changed

+79
-17
lines changed

docs/user-guide/components.md

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,14 @@ const handleEdit = (user) => {
181181
editingUser.value = user
182182
}
183183
184-
const handleUserUpdated = () => {
184+
const handleUserUpdated = (updatedUser) => {
185185
editingUser.value = null
186-
userList.value?.refresh()
186+
// Update the user in the list for immediate UI update
187+
userList.value?.updateUser(updatedUser)
188+
}
189+
190+
const handleEdit = (user) => {
191+
editingUser.value = user
187192
}
188193
</script>
189194
@@ -281,17 +286,71 @@ const handleUserUpdated = () => {
281286
| `user` | `{ user: User, index: number }` | Custom user item display |
282287
| `pagination` | `{ pagination, fetchUsers, loading }` | Custom pagination controls |
283288

284-
**Methods**
289+
**Events**
285290

286-
| Method | Description |
287-
|--------|-------------|
288-
| `refresh()` | Manually refresh the user list |
291+
| Event | Payload | Description |
292+
|-------|---------|-------------|
293+
| `editClick` | `User` | Fired when edit button is clicked |
294+
| `delete` | `User` | Fired after successful user deletion |
289295

290296
### NUsersUserCard
291297

292298
Displays individual user information with edit and delete actions. Used internally by `NUsersList` but can be used standalone.
293299
The `edit` and `delete` events are emitted when the edit or delete button is clicked. The `delete` event is calling the API to delete the user, while the `edit` event is just emitting the user object and let the parent component handle the edit action.
294300

301+
### Automatic UI Updates
302+
303+
The `NUsersList` component follows an event-driven pattern for handling user updates. When a user is edited, the component emits events that the parent can handle to update the local state.
304+
305+
**Event Flow:**
306+
1. User clicks edit → `NUsersUserCard` emits `editClick`
307+
2. `NUsersList` forwards the event to parent → Parent receives `editClick`
308+
3. Parent shows edit form → User submits changes
309+
4. Parent updates local state → UI automatically reflects changes
310+
311+
**Example with event-driven updates:**
312+
```vue
313+
<script setup>
314+
import { ref } from 'vue'
315+
import { useUsers } from 'nuxt-users/composables'
316+
317+
const { users, updateUser } = useUsers()
318+
const editingUser = ref(null)
319+
320+
const handleEdit = (user) => {
321+
editingUser.value = user
322+
}
323+
324+
const handleUserUpdated = (userData) => {
325+
editingUser.value = null
326+
// Update the local state using the composable
327+
if (userData.id) {
328+
updateUser(userData)
329+
}
330+
}
331+
</script>
332+
333+
<template>
334+
<div>
335+
<NUsersUserForm
336+
v-if="editingUser"
337+
:user="editingUser"
338+
@submit="handleUserUpdated"
339+
/>
340+
<NUsersList
341+
@edit-click="handleEdit"
342+
/>
343+
</div>
344+
</template>
345+
```
346+
347+
**Benefits of this approach:**
348+
- **Loose coupling** - Components don't need to know about each other's internal methods
349+
- **Event-driven** - Clean separation of concerns
350+
- **Reusable** - Parent can handle events however it wants
351+
- **Testable** - Easy to test event handling
352+
- **Flexible** - Parent can implement any update strategy
353+
295354
#### Basic Usage
296355

297356
```vue

playground/pages/users.vue

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
<script setup lang="ts">
22
import { ref } from 'vue'
3+
import { useUsers } from '../../src/runtime/composables/useUsers'
34
import type { User } from '../../src/types'
45
5-
const usersListRef = ref()
6-
7-
const refreshUserList = () => {
8-
console.log('refreshUserList')
9-
if (usersListRef.value) {
10-
usersListRef.value.refresh()
11-
}
12-
}
6+
const { updateUser } = useUsers()
137
148
const selectedUser = ref<User | null>(null)
159
const handleEditClick = (user: User) => {
1610
selectedUser.value = user
1711
}
12+
13+
const handleUserUpdated = (userData: Partial<User>) => {
14+
console.log('User updated:', userData)
15+
selectedUser.value = null
16+
// Update the user in the local state using the composable
17+
if (userData.id) {
18+
updateUser(userData as User)
19+
}
20+
}
1821
</script>
1922

2023
<template>
2124
<div>
2225
<NUsersUserForm
2326
:user="selectedUser"
24-
@submit="refreshUserList"
27+
@submit="handleUserUpdated"
2528
/>
2629
<NUsersList
27-
ref="usersListRef"
2830
@edit-click="handleEditClick"
29-
@delete="refreshUserList"
31+
@delete="() => {}"
3032
/>
3133
</div>
3234
</template>

src/runtime/components/NUsersUserCard.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const canDelete = computed(() => {
7171
})
7272
7373
const editUser = async (user: User) => {
74+
console.info('The edit event is emitted. Nuxt-users does not automatically calls the API to update the user, you should do it at the upper component.')
7475
emit('editClick', user)
7576
}
7677

0 commit comments

Comments
 (0)