Skip to content

Commit 7566d29

Browse files
committed
refactor
1 parent a1134cf commit 7566d29

File tree

3 files changed

+40
-29
lines changed

3 files changed

+40
-29
lines changed

02-pinia-app/db/db.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"picture": "https://via.placeholder.com/150/yellow",
77
"age": 29,
88
"eyeColor": "brown",
9-
"name": "Donovan Bernardo",
9+
"name": "Donovan Fernando",
1010
"gender": "male",
1111
"company": "BIOSPAN",
1212
"email": "donovanbernard@biospan.com",
@@ -136,7 +136,7 @@
136136
"picture": "https://via.placeholder.com/150/white",
137137
"age": 53,
138138
"eyeColor": "blue",
139-
"name": "Lester Gilmore!!!",
139+
"name": "Lester Gilmore",
140140
"gender": "male",
141141
"company": "ZILLANET",
142142
"email": "lestergilmore@zillanet.com",

02-pinia-app/src/clients/composables/useClient.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
1-
import { ref, watch } from 'vue';
2-
import { useQuery } from '@tanstack/vue-query';
1+
import { ref, watch, computed } from 'vue';
2+
import { useQuery, useMutation } from '@tanstack/vue-query';
33
import clientsApi from '@/api/clientsApi';
44
import type { Client } from '@/clients/interfaces/client';
55

6-
const getClient = async (id: number): Promise<Client> => {
6+
const getClient = async ( id: number ): Promise<Client> => {
77
const { data } = await clientsApi.get(`/${ id }`);
88
return data;
99
}
1010

11+
const updateClient = async( client: Client ): Promise<Client> => {
12+
const { data } = await clientsApi.patch<Client>(`/${ client.id }`, client);
13+
return data;
14+
}
15+
1116
const useClient = ( id: number ) => {
1217

1318
const client = ref<Client>();
1419

15-
const { isLoading, data } = useQuery(
20+
const clientMutation = useMutation( updateClient );
21+
22+
const { data, isError, isLoading } = useQuery(
1623
['client', id],
1724
() => getClient(id),
25+
{ retry: false }
1826
);
1927

2028
watch(data, () => {
2129
if ( data.value ) client.value = {...data.value};
2230
}, { immediate: true });
2331

2432
return {
33+
clientMutation,
2534
client,
26-
isLoading
35+
isError,
36+
isLoading,
37+
updateClient: clientMutation.mutate,
38+
isUpdating: computed( () => clientMutation.isLoading.value ),
39+
isUpdatingSuccess: computed( () => clientMutation.isSuccess.value ),
40+
isErrorUpdating: computed( () => clientMutation.isError.value )
2741
}
2842

2943
}

02-pinia-app/src/clients/pages/ClientPage.vue

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,41 @@
11
<script setup lang="ts">
22
import { watch } from 'vue';
3-
import { useRoute } from 'vue-router';
4-
import { useMutation, useQueryClient } from '@tanstack/vue-query';
5-
import clientsApi from '@/api/clientsApi';
3+
import { useRoute, useRouter } from 'vue-router';
64
import LoadingModalComponent from '@/shared/components/LoadingModalComponent.vue';
75
import useClient from '@/clients/composables/useClient';
8-
import type { Client } from '@/clients/interfaces/client';
96
107
const route = useRoute();
11-
const queryClient = useQueryClient();
8+
const router = useRouter();
129
13-
const { client, isLoading } = useClient( +route.params.id );
14-
15-
const updateClient = async( client: Client ):Promise<Client> => {
16-
// await new Promise( resolve => {
17-
// setTimeout( () => resolve( true ), 1500 )
18-
// });
19-
const { data } = await clientsApi.patch<Client>(`/${client.id}`, client);
20-
const queries = queryClient.getQueryCache().findAll(['clients?page='], { exact: false });
21-
queries.forEach( query => query.fetch() );
22-
return data;
23-
}
24-
25-
const clientMutation = useMutation( updateClient );
10+
const {
11+
clientMutation,
12+
client,
13+
isError,
14+
isLoading,
15+
isUpdating,
16+
isUpdatingSuccess,
17+
updateClient
18+
} = useClient( +route.params.id );
2619
2720
watch(clientMutation.isSuccess, () => {
2821
setTimeout(() => {
2922
clientMutation.reset();
3023
}, 2000);
3124
});
3225
26+
watch(isError, () => {
27+
if ( isError.value ) router.replace('/clients');
28+
});
29+
3330
</script>
3431

3532
<template>
36-
<h3 v-if="clientMutation.isLoading.value">Saving...</h3>
37-
<h3 v-if="clientMutation.isSuccess.value">Saved</h3>
33+
<h3 v-if="isUpdating">Saving...</h3>
34+
<h3 v-if="isUpdatingSuccess">Saved</h3>
3835
<LoadingModalComponent v-if="isLoading" />
3936
<div v-if="client">
4037
<h2>{{ client.name }}</h2>
41-
<form @submit.prevent="clientMutation.mutate(client!)">
38+
<form @submit.prevent="updateClient(client!)">
4239
<input
4340
type="text"
4441
placeholder="Name"
@@ -53,7 +50,7 @@ watch(clientMutation.isSuccess, () => {
5350
<br>
5451
<button
5552
type="submit"
56-
:disabled="clientMutation.isLoading.value"
53+
:disabled="isUpdating"
5754
>
5855
Save
5956
</button>

0 commit comments

Comments
 (0)