Skip to content

Commit

Permalink
fix url create and delete api
Browse files Browse the repository at this point in the history
  • Loading branch information
vinit717 committed Oct 4, 2024
1 parent 92b796e commit 1c407e3
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 9 deletions.
2 changes: 1 addition & 1 deletion __tests__/components/Dashhboard/UrlListItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('UrlListItem', () => {
fireEvent.click(deleteButtons[0]);

await waitFor(() => {
expect(deleteUrlApi).toHaveBeenCalledWith({ id: url.id, userId: 123 });
expect(deleteUrlApi).toHaveBeenCalledWith({ id: url.id });
});
});

Expand Down
4 changes: 1 addition & 3 deletions src/components/Dashboard/UrlListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { TbTrash } from 'react-icons/tb';
import { useMutation } from 'react-query';

import { TINY_SITE } from '@/constants/url';
import useAuthenticated from '@/hooks/useAuthenticated';
import { queryClient } from '@/pages/_app';
import { deleteUrlApi } from '@/services/api';
import { UrlType } from '@/types/url.types';
Expand Down Expand Up @@ -42,7 +41,6 @@ interface UrlListItemProps {
}

const UrlListItem: React.FC<UrlListItemProps> = ({ url, copyButtonHandler }) => {
const { userData } = useAuthenticated();
const deleteMutation = useMutation({
mutationFn: deleteUrlApi,
onSuccess: () => queryClient.invalidateQueries(['urls']),
Expand All @@ -53,7 +51,7 @@ const UrlListItem: React.FC<UrlListItemProps> = ({ url, copyButtonHandler }) =>
});

const handleCopy = () => copyButtonHandler(`${TINY_SITE}/${url.shortUrl}`);
const handleDelete = () => deleteMutation.mutate({ id: url.id, userId: Number(userData?.data?.id) });
const handleDelete = () => deleteMutation.mutate({ id: url.id });

return (
<div className="rounded-xl bg-white px-2 py-2 sm:pl-4 sm:py-4 flex flex-col overflow-hidden">
Expand Down
7 changes: 2 additions & 5 deletions src/services/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,12 @@ const useShortenUrlMutation = ({ onSuccess, onError }: useShortenUrlMutationArgs
MutationParams
> => {
return useMutation(
async ({ originalUrl, userData }: MutationParams) => {
async ({ originalUrl }: MutationParams) => {
const response = await axios.post(
`${TINY_API_URL}/tinyurl`,
{
OriginalUrl: originalUrl,
Comment: '',
CreatedBy: userData?.data?.userName,
UserId: userData?.data?.id,
} as ShortenUrlRequest,
{
withCredentials: true,
Expand All @@ -95,10 +93,9 @@ const useShortenUrlMutation = ({ onSuccess, onError }: useShortenUrlMutationArgs
}
);
};
const deleteUrlApi = async ({ id, userId }: { id: number; userId: number }) => {
const deleteUrlApi = async ({ id }: { id: number }) => {
const { data } = await axios.delete(`${TINY_API_URL}/urls/${id}`, {
withCredentials: true,
data: { user_id: userId },
});
return data;
};
Expand Down

0 comments on commit 1c407e3

Please sign in to comment.