auth checkpoint
auth dependecies not instaled in dev container
This commit is contained in:
1 parent
7378d37b36
commit
42002f8e6f
50 files changed
+3680
-636
No files matched your search
@@ -0,0 +1,49 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import * as api from '../lib/api';
|
||||
|
||||
export function useLogin() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({ username, password }: { username: string; password: string }) =>
|
||||
api.login({ username, password }),
|
||||
onSuccess: (data) => {
|
||||
queryClient.setQueryData(['auth', 'user'], data.user);
|
||||
queryClient.setQueryData(['auth', 'token'], data.access_token);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useRegister() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({ username, email, password }: { username: string; email: string; password: string }) =>
|
||||
api.register({ username, email, password }),
|
||||
onSuccess: (data) => {
|
||||
queryClient.setQueryData(['auth', 'user'], data.user);
|
||||
queryClient.setQueryData(['auth', 'token'], data.access_token);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useProfile() {
|
||||
return useQuery({
|
||||
queryKey: ['auth', 'user'],
|
||||
queryFn: api.getProfile,
|
||||
enabled: false, // We'll manually trigger this when needed
|
||||
retry: false,
|
||||
});
|
||||
}
|
||||
|
||||
export function useLogout() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: api.logout,
|
||||
onSuccess: () => {
|
||||
queryClient.removeQueries({ queryKey: ['auth'] });
|
||||
queryClient.clear();
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import * as api from '../lib/api';
|
||||
|
||||
export function useComments(params: api.FindCommentsParams = {}) {
|
||||
return useQuery({
|
||||
queryKey: ['comments', params],
|
||||
queryFn: () => api.fetchComments(params),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateComment() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: api.createComment,
|
||||
onSuccess: (data, variables) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['comments', { articleId: variables.articleId }]
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['comments', { liveBlogId: variables.liveBlogId }]
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateComment() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({ id, dto }: { id: string; dto: api.UpdateCommentDto }) =>
|
||||
api.updateComment(id, dto),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['comments'] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteComment() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: api.deleteComment,
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['comments'] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useReactionCounts(
|
||||
articleId?: string,
|
||||
liveBlogId?: string,
|
||||
commentId?: string
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: ['reactions', 'counts', { articleId, liveBlogId, commentId }],
|
||||
queryFn: () => api.getReactionCounts(articleId, liveBlogId, commentId),
|
||||
enabled: !!articleId || !!liveBlogId || !!commentId,
|
||||
});
|
||||
}
|
||||
|
||||
export function useUserReaction(
|
||||
articleId?: string,
|
||||
liveBlogId?: string,
|
||||
commentId?: string
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: ['reactions', 'user', { articleId, liveBlogId, commentId }],
|
||||
queryFn: () => api.getUserReaction(articleId, liveBlogId, commentId),
|
||||
enabled: !!articleId || !!liveBlogId || !!commentId,
|
||||
retry: false,
|
||||
});
|
||||
}
|
||||
|
||||
export function useAddReaction() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: api.addReaction,
|
||||
onSuccess: (_, variables) => {
|
||||
// Invalidate both counts and user reaction queries
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['reactions', 'counts', {
|
||||
articleId: variables.articleId,
|
||||
liveBlogId: variables.liveBlogId,
|
||||
commentId: variables.commentId
|
||||
}]
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['reactions', 'user', {
|
||||
articleId: variables.articleId,
|
||||
liveBlogId: variables.liveBlogId,
|
||||
commentId: variables.commentId
|
||||
}]
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user