What is Nostr?
Matty-kun /
npub13v6…hy07
2025-02-22 23:13:59

Matty-kun on Nostr: I feel like there's a better way to use React Query here, to consolidate it into one ...

I feel like there's a better way to use React Query here, to consolidate it into one single function rather than three separate ones. Maybe using Promise.all(). But, in my efforts to over-engineer everything, I find myself lacking. There also isn't a whole lot of error handling. It's just kind of one of those things that I want to work but don't care much if my nuts explode.

const { data: users, error } = useQuery<User[]>({
queryKey: ["users"],
queryFn: async () => {
const res = await fetch("/api/users");
if (!res.ok) throw new Error("failed to fetch users");
return res.json();
},
staleTime: 60 * 1000, // 60 seconds
retry: 3,
});
const { data: issue } = useQuery<Issue>({
queryKey: ["issue"],
queryFn: async () => {
const res = await fetch(`/api/issues/${issueId}`);
if (!res.ok) throw new Error("failed to fetch issue details");
return res.json();
},
});
const { data: user, isLoading: userQueryIsLoading } = useQuery<User>({
queryKey: ["user"],
queryFn: async () => {
const res = await fetch(`/api/users/${issue?.assignedToUserId}`);
if (!res.ok)
throw new Error(
`failed to fetch user with id ${issue?.assignedToUserId}`
);
return res.json();
},
});

const updateAssignedUser = async (userId: string) => {
try {
setOpen(true);
await fetch(`/api/issuesd/${issueId}`, {
method: "PATCH",
body: JSON.stringify({
assignedToUserId: userId,
}),
});
} catch (e) {
console.log(e);
}
};
Author Public Key
npub13v68j9479je6h958e84h32xs2gjvq3duuk6pd0w4q95evh4s73wqk2hy07