kopia lustrzana https://gitlab.com/soapbox-pub/soapbox
intercept errors during fetching
rodzic
9e492caa9f
commit
772fa930d2
|
@ -1,5 +1,6 @@
|
||||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||||
|
|
||||||
|
import { MastodonResponse } from 'soapbox/api/MastodonResponse.ts';
|
||||||
import { useApi } from 'soapbox/hooks/useApi.ts';
|
import { useApi } from 'soapbox/hooks/useApi.ts';
|
||||||
import { PolicyItem, PolicyResponse, PolicySpec } from 'soapbox/utils/policies.ts';
|
import { PolicyItem, PolicyResponse, PolicySpec } from 'soapbox/utils/policies.ts';
|
||||||
|
|
||||||
|
@ -7,11 +8,23 @@ const useModerationPolicies = () => {
|
||||||
const api = useApi();
|
const api = useApi();
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
const handleResponse = async (response: MastodonResponse, message: string) => {
|
||||||
|
const details = await response.error()
|
||||||
|
.then(v => v?.error || 'Unknown error');
|
||||||
|
if (!response.ok) throw new Error(`${message}: ${details}`);
|
||||||
|
const data = await response.json();
|
||||||
|
// Check if the response contains an error
|
||||||
|
if (data && 'error' in data) throw new Error(data.error);
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
const allPoliciesQuery = useQuery({
|
const allPoliciesQuery = useQuery({
|
||||||
queryKey: ['admin', 'moderation_policies'],
|
queryKey: ['admin', 'moderation_policies'],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const response = await api.get('/api/v1/admin/ditto/policies');
|
return await handleResponse(
|
||||||
return response.json() as Promise<PolicyItem[]>;
|
await api.get('/api/v1/admin/ditto/policies'),
|
||||||
|
'Error fetching policy list',
|
||||||
|
) as Promise<PolicyItem[]>;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -19,26 +32,20 @@ const useModerationPolicies = () => {
|
||||||
const currentPolicyQuery = useQuery({
|
const currentPolicyQuery = useQuery({
|
||||||
queryKey: ['admin', 'current_moderation_policy'],
|
queryKey: ['admin', 'current_moderation_policy'],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const response = await api.get('/api/v1/admin/ditto/policies/current');
|
return await handleResponse(
|
||||||
return response.json() as Promise<PolicyResponse>;
|
await api.get('/api/v1/admin/ditto/policies/current'),
|
||||||
|
'Error fetching current policy',
|
||||||
|
) as Promise<PolicyResponse>;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Update current policy
|
// Update current policy
|
||||||
const updatePolicyMutation = useMutation({
|
const updatePolicyMutation = useMutation({
|
||||||
mutationFn: async (spec: PolicySpec) => {
|
mutationFn: async (spec: PolicySpec) => {
|
||||||
const response = await api.put('/api/v1/admin/ditto/policies/current', spec);
|
return await handleResponse(
|
||||||
|
await api.put('/api/v1/admin/ditto/policies/current', spec),
|
||||||
// Parse the JSON response
|
'Error updating policy',
|
||||||
const data = await response.json();
|
) as Promise<PolicyResponse>;
|
||||||
|
|
||||||
// Check if the response contains an error
|
|
||||||
if (data && 'error' in data) {
|
|
||||||
// Throw the error to be caught by the caller
|
|
||||||
throw new Error(data.error);
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
|
||||||
},
|
},
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
queryClient.invalidateQueries({ queryKey: ['admin', 'current_moderation_policy'] }); // Refetch after update
|
queryClient.invalidateQueries({ queryKey: ['admin', 'current_moderation_policy'] }); // Refetch after update
|
||||||
|
@ -52,6 +59,10 @@ const useModerationPolicies = () => {
|
||||||
isFetched: currentPolicyQuery.isFetched,
|
isFetched: currentPolicyQuery.isFetched,
|
||||||
updatePolicy: updatePolicyMutation.mutate,
|
updatePolicy: updatePolicyMutation.mutate,
|
||||||
isUpdating: updatePolicyMutation.isPending,
|
isUpdating: updatePolicyMutation.isPending,
|
||||||
|
allPoliciesError: allPoliciesQuery.error,
|
||||||
|
storedPoliciesError: currentPolicyQuery.error,
|
||||||
|
allPoliciesIsError: allPoliciesQuery.isError,
|
||||||
|
storedPoliciesIsError: currentPolicyQuery.isError,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue