Skip to content

Commit c8f82c6

Browse files
committed
Client Page to useClient composable
1 parent ae6039a commit c8f82c6

File tree

3 files changed

+76
-34
lines changed

3 files changed

+76
-34
lines changed

02-piniaApp/src/clients/composables/useClient.ts

+47-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,48 @@
11
import clientsApi from "@/api/clients-api";
22
import type { Client } from "@/clients/interfaces/client";
3-
import { useQuery } from "@tanstack/vue-query";
4-
import { ref, watch } from "vue";
3+
import { useMutation, useQuery } from "@tanstack/vue-query";
4+
import { computed, ref, watch } from "vue";
55

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

11+
// Actualizando cliente
12+
const updateClient = async (client: Client): Promise<Client> => {
13+
// await new Promise((resolve) => {
14+
// setTimeout(() => resolve(true), 1500);
15+
// });
16+
17+
const { id, ...rest } = client;
18+
19+
const { data } = await clientsApi.patch<Client>(`/clients/${id}`, rest);
20+
21+
/*
22+
const queries = queryClient
23+
.getQueryCache()
24+
.findAll(["clients?page="], { exact: false });
25+
26+
// Basado en la query que se utiliza, se elimina la cache y esto
27+
// realiza la petición de nuevo
28+
queries.forEach((query) => query.fetch());
29+
*/
30+
return data;
31+
};
32+
1133
const useCounter = (id: number) => {
1234
const client = ref<Client>();
1335

14-
const { isLoading, data } = useQuery(["client", id], () => getClient(id), {});
36+
const { isLoading, data, isError } = useQuery(
37+
["client", id],
38+
() => getClient(id),
39+
{
40+
retry: false,
41+
// onError(error: any) {
42+
// console.log(error.message);
43+
// },
44+
}
45+
);
1546

1647
watch(
1748
data,
@@ -27,9 +58,21 @@ const useCounter = (id: number) => {
2758
{ immediate: true }
2859
);
2960

61+
const clientMutation = useMutation(updateClient);
62+
63+
// Eliminar anuncio "Guardado! y "Guardando..."
64+
3065
return {
31-
isLoading,
3266
client,
67+
clientMutation,
68+
isError,
69+
isLoading,
70+
71+
// Methods
72+
updateClient: clientMutation.mutate,
73+
isUpdating: computed(() => clientMutation.isLoading.value),
74+
isErrorUpdating: computed(() => clientMutation.isError.value),
75+
isUpdatingSuccess: computed(() => clientMutation.isSuccess.value),
3376
};
3477
};
3578

02-piniaApp/src/clients/composables/useClients.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ const useClients = () => {
3232

3333
// data es reactiva, entonces al existir un cambio,
3434
// se actualizan los cambios en la store.
35-
watch(data, (clients) => {
36-
if (clients) {
37-
store.setClients(clients);
38-
}
39-
});
35+
watch(
36+
data,
37+
(clients) => {
38+
if (clients) {
39+
store.setClients(clients);
40+
}
41+
},
42+
{ immediate: true }
43+
);
4044

4145
return {
4246
clients,

02-piniaApp/src/clients/pages/ClientPage.vue

+20-25
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,42 @@
11
<script setup lang="ts">
22
import LoadingModal from "@/shared/components/LoadingModal.vue";
33
import useClient from "@/clients/composables/useClient";
4-
import { useRoute } from "vue-router";
5-
import { useMutation } from "@tanstack/vue-query";
6-
import type { Client } from "../interfaces/client";
7-
import clientsApi from "@/api/clients-api";
4+
import { useRoute, useRouter } from "vue-router";
85
import { watch } from "vue";
96
107
const route = useRoute();
11-
12-
const { isLoading, client } = useClient(+route.params.id);
13-
14-
// Actualizando cliente
15-
const updateClient = async (client: Client): Promise<Client> => {
16-
await new Promise((resolve) => {
17-
setTimeout(() => resolve(true), 1500);
18-
});
19-
20-
const { id, ...rest } = client;
21-
22-
const { data } = await clientsApi.patch<Client>(`/clients/${id}`, rest);
23-
return data;
24-
};
25-
26-
const clientMutation = useMutation(updateClient);
27-
28-
// Eliminar anuncio "Guardado! y "Guardando..."
8+
const router = useRouter();
9+
// const queryClient = useQueryClient();
10+
11+
const {
12+
client,
13+
isError,
14+
isLoading,
15+
clientMutation,
16+
updateClient,
17+
isUpdating,
18+
isUpdatingSuccess,
19+
} = useClient(+route.params.id);
2920
3021
watch(clientMutation.isSuccess, () => {
3122
setTimeout(() => {
3223
clientMutation.reset();
3324
}, 2000);
3425
});
26+
27+
watch(isError, () => {
28+
if (isError.value) router.replace("/clients");
29+
});
3530
</script>
3631
<template>
37-
<h3 v-if="clientMutation.isLoading.value">Guardando...</h3>
38-
<h3 v-if="clientMutation.isSuccess.value">Guardado!</h3>
32+
<h3 v-if="isUpdating">Guardando...</h3>
33+
<h3 v-if="isUpdatingSuccess">Guardado!</h3>
3934

4035
<LoadingModal v-if="isLoading" />
4136

4237
<div v-if="client">
4338
<h1>{{ client.name }}</h1>
44-
<form @submit.prevent="clientMutation.mutate(client)">
39+
<form @submit.prevent="updateClient(client)">
4540
<input type="text" placeholder="Nombre" v-model="client.name" />
4641
<br />
4742
<input type="text" placeholder="Dirección" v-model="client.address" />

0 commit comments

Comments
 (0)