Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
};
23 changes: 23 additions & 0 deletions packages/shared/src/features/interests/hooks/useInterest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useQuery } from '@tanstack/react-query';
import { useAuthContext } from '../../../contexts/AuthContext';
import { generateQueryKey, RequestKey } from '../../../lib/query';
import { getInterest, getInterestFindings } from '../../../graphql/interests';

export const useInterest = (id: string) => {
const { user, isAuthReady } = useAuthContext();
const enabled = isAuthReady && !!user && !!id;

const interestQuery = useQuery({
queryKey: generateQueryKey(RequestKey.Interests, user, id),
queryFn: () => getInterest(id),
enabled,
});

const findingsQuery = useQuery({
queryKey: generateQueryKey(RequestKey.InterestFindings, user, id),
queryFn: () => getInterestFindings(id),
enabled,
});

return { interestQuery, findingsQuery };
};
14 changes: 14 additions & 0 deletions packages/shared/src/features/interests/hooks/useInterestPosts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useQuery } from '@tanstack/react-query';
import { useAuthContext } from '../../../contexts/AuthContext';
import { generateQueryKey, RequestKey } from '../../../lib/query';
import { getInterestPosts } from '../../../graphql/interests';

export const useInterestPosts = (id: string) => {
const { user, isAuthReady } = useAuthContext();

return useQuery({
queryKey: generateQueryKey(RequestKey.InterestFindings, user, id, 'posts'),
queryFn: () => getInterestPosts(id),
enabled: isAuthReady && !!user && !!id,
});
};
15 changes: 15 additions & 0 deletions packages/shared/src/features/interests/hooks/useInterests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useQuery } from '@tanstack/react-query';
import { useAuthContext } from '../../../contexts/AuthContext';
import { generateQueryKey, RequestKey } from '../../../lib/query';
import { getInterests } from '../../../graphql/interests';

export const useInterests = () => {
const { user, isAuthReady } = useAuthContext();

return useQuery({
queryKey: generateQueryKey(RequestKey.Interests, user),
queryFn: getInterests,
enabled: isAuthReady && !!user,
staleTime: 0,
});
};
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 };
};
Loading
Loading