Skip to content
Merged
30 changes: 30 additions & 0 deletions packages/shared/src/features/interests/hooks/useCreateInterest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useAuthContext } from '../../../contexts/AuthContext';
import { useToastNotification } from '../../../hooks/useToastNotification';
import { generateQueryKey, RequestKey } from '../../../lib/query';
import { createInterest } from '../../../graphql/interests';

export const useCreateInterest = ({
onCreated,
}: {
onCreated?: (id: string) => void;
} = {}) => {
const { user } = useAuthContext();
const { displayToast } = useToastNotification();
const queryClient = useQueryClient();

const { isPending, mutateAsync } = useMutation({
mutationFn: (query: string) => createInterest(query),
onSuccess: async (interest) => {
await queryClient.invalidateQueries({
queryKey: generateQueryKey(RequestKey.Interests, user),
});
onCreated?.(interest.id);
},
onError: () => {
displayToast('Failed to create the interest. Please try again.');
},
});

return { isCreating: isPending, createInterest: mutateAsync };
};
30 changes: 30 additions & 0 deletions packages/shared/src/features/interests/hooks/useDeleteInterest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useAuthContext } from '../../../contexts/AuthContext';
import { useToastNotification } from '../../../hooks/useToastNotification';
import { generateQueryKey, RequestKey } from '../../../lib/query';
import { deleteInterest } from '../../../graphql/interests';

export const useDeleteInterest = ({
onDeleted,
}: {
onDeleted?: () => void;
} = {}) => {
const { user } = useAuthContext();
const { displayToast } = useToastNotification();
const queryClient = useQueryClient();

const { isPending, mutateAsync } = useMutation({
mutationFn: (id: string) => deleteInterest(id),
onSuccess: async () => {
await queryClient.invalidateQueries({
queryKey: generateQueryKey(RequestKey.Interests, user),
});
onDeleted?.();
},
onError: () => {
displayToast('Failed to delete the interest. Please try again.');
},
});

return { isDeleting: isPending, deleteInterest: mutateAsync };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useAuthContext } from '../../../contexts/AuthContext';
import { useToastNotification } from '../../../hooks/useToastNotification';
import { generateQueryKey, RequestKey } from '../../../lib/query';
import { sendInterestCommand } from '../../../graphql/interests';

export const useSendInterestCommand = (id: string) => {
const { user } = useAuthContext();
const { displayToast } = useToastNotification();
const queryClient = useQueryClient();

const { isPending, mutateAsync } = useMutation({
mutationFn: (text: string) => sendInterestCommand({ id, text }),
onSuccess: async () => {
displayToast('The agent is working on it ✅');
await queryClient.invalidateQueries({
queryKey: generateQueryKey(RequestKey.InterestFindings, user, id),
});
},
onError: () => {
displayToast('Failed to send the command. Please try again.');
},
});

return { isSending: isPending, sendCommand: mutateAsync };
};
31 changes: 31 additions & 0 deletions packages/shared/src/features/interests/hooks/useUpdateInterest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useAuthContext } from '../../../contexts/AuthContext';
import { useToastNotification } from '../../../hooks/useToastNotification';
import { generateQueryKey, RequestKey } from '../../../lib/query';
import type { UpdateInterestInput } from '../../../graphql/interests';
import { updateInterest } from '../../../graphql/interests';

export const useUpdateInterest = (id: string) => {
const { user } = useAuthContext();
const { displayToast } = useToastNotification();
const queryClient = useQueryClient();

const { isPending, mutateAsync } = useMutation({
mutationFn: (data: UpdateInterestInput) => updateInterest({ id, data }),
onSuccess: async () => {
await Promise.all([
queryClient.invalidateQueries({
queryKey: generateQueryKey(RequestKey.Interests, user, id),
}),
queryClient.invalidateQueries({
queryKey: generateQueryKey(RequestKey.Interests, user),
}),
]);
},
onError: () => {
displayToast('Failed to update the interest. Please try again.');
},
});

return { isUpdating: isPending, updateInterest: mutateAsync };
};
47 changes: 47 additions & 0 deletions packages/shared/src/features/interests/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { queryOptions } from '@tanstack/react-query';
import {
getInterest,
getInterestFindings,
getInterestPosts,
getInterests,
} from '../../graphql/interests';
import { generateQueryKey, RequestKey } from '../../lib/query';
import type { LoggedUser } from '../../lib/user';

export const interestsQueryOptions = (user?: Pick<LoggedUser, 'id'>) =>
queryOptions({
queryKey: generateQueryKey(RequestKey.Interests, user),
queryFn: getInterests,
enabled: !!user?.id,
staleTime: 0,
});

export const interestQueryOptions = (
id: string,
user?: Pick<LoggedUser, 'id'>,
) =>
queryOptions({
queryKey: generateQueryKey(RequestKey.Interests, user, id),
queryFn: () => getInterest(id),
enabled: !!user?.id && !!id,
});

export const interestFindingsQueryOptions = (
id: string,
user?: Pick<LoggedUser, 'id'>,
) =>
queryOptions({
queryKey: generateQueryKey(RequestKey.InterestFindings, user, id),
queryFn: () => getInterestFindings(id),
enabled: !!user?.id && !!id,
});

export const interestPostsQueryOptions = (
id: string,
user?: Pick<LoggedUser, 'id'>,
) =>
queryOptions({
queryKey: generateQueryKey(RequestKey.InterestFindings, user, id, 'posts'),
queryFn: () => getInterestPosts(id),
enabled: !!user?.id && !!id,
});
Loading
Loading