Skip to content

Commit

Permalink
chore: modified useMutatePost
Browse files Browse the repository at this point in the history
  • Loading branch information
Franqsanz committed Sep 12, 2023
1 parent 26610fa commit 5aee763
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"no-unused-expressions": 0,
"prefer-const": 0,
"func-style": [1, "declaration", { "allowArrowFunctions": false }],
"n/handle-callback-err": 0,
"prettier/prettier": [
2,
{
Expand Down
4 changes: 3 additions & 1 deletion src/components/skeletons/SkeletonDBook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export function SkeletonDetailsBook() {
skeletonHeight='6'
/>
<Skeleton h='40px' rounded='lg' mb='3'></Skeleton>
<SkeletonText mt='1' noOfLines={15} spacing='2' skeletonHeight='3' />
<SkeletonText mt='1' noOfLines={7} spacing='2' skeletonHeight='3' />
<SkeletonText mt='8' noOfLines={5} spacing='2' skeletonHeight='3' />
<SkeletonText mt='8' noOfLines={3} spacing='2' skeletonHeight='3' />
<Skeleton h='300px' rounded='lg' mt='10'></Skeleton>
<Flex direction={{ base: 'column', md: 'row' }} mt='10' gap='3'>
<Skeleton
Expand Down
48 changes: 46 additions & 2 deletions src/hooks/querys.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { useQuery, useMutation, useInfiniteQuery } from '@tanstack/react-query';
import {
useQuery,
useMutation,
useInfiniteQuery,
QueryClient,
} from '@tanstack/react-query';

import {
getAllBooks,
Expand All @@ -11,9 +16,48 @@ import {
postBook,
} from '../services/api';
import { keys } from '../utils/utils';
import { BookType } from '../components/types';

const queryClient = new QueryClient();

function useMutatePost() {
return useMutation({ mutationKey: [keys.postBook], mutationFn: postBook });
return useMutation({
mutationKey: [keys.postBook],
mutationFn: postBook,
// Mutación optimista
onMutate: async (newPost) => {
// Cancelar consultas pendientes para la misma clave de consulta
await queryClient.cancelQueries([keys.postBook]);

// Obtener los datos de la consulta anterior
const previousPost = await queryClient.getQueryData([keys.postBook]);

// Actualizar los datos en caché con el nuevo post
await queryClient.setQueryData(
[keys.postBook],
(oldData?: BookType[] | undefined) => {
if (oldData === null) return [newPost];
// oldData debe ser iterable por eso el (oldData || []).
return [...(oldData || []), newPost];
},
);

return { previousPost }; // <--- Contexto
},
onError: (err, variables, context) => {
console.log(err);
// Revertir los datos en caché si la mutación falla
if (context?.previousPost !== null) {
queryClient.setQueryData([keys.postBook], context?.previousPost);
}
},
onSettled: async () => {
// Invalidar la consulta en caché para que se refresque
await queryClient.invalidateQueries({
queryKey: [keys.postBook],
});
},
});
}

function useAllBooks() {
Expand Down

1 comment on commit 5aee763

@vercel
Copy link

@vercel vercel bot commented on 5aee763 Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

xbu – ./

xbu-franqsanz.vercel.app
xbu.vercel.app
xbu-git-main-franqsanz.vercel.app

Please sign in to comment.