2022-11-01 10:22:29 -07:00
|
|
|
import { useMutation, useQuery } from '@tanstack/react-query';
|
|
|
|
|
2022-11-02 12:02:43 -07:00
|
|
|
import { useApi, useFeatures, useOwnAccount } from 'soapbox/hooks';
|
2022-11-01 10:22:29 -07:00
|
|
|
|
|
|
|
import { queryClient } from './client';
|
|
|
|
|
|
|
|
export interface IPolicy {
|
|
|
|
pending_policy_id: string
|
|
|
|
}
|
|
|
|
|
|
|
|
const PolicyKeys = {
|
|
|
|
policy: ['policy'] as const,
|
|
|
|
};
|
|
|
|
|
|
|
|
function usePendingPolicy() {
|
|
|
|
const api = useApi();
|
|
|
|
const account = useOwnAccount();
|
2022-11-02 12:02:43 -07:00
|
|
|
const features = useFeatures();
|
2022-11-01 10:22:29 -07:00
|
|
|
|
|
|
|
const getPolicy = async() => {
|
|
|
|
const { data } = await api.get<IPolicy>('/api/v1/truth/policies/pending');
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
return useQuery(PolicyKeys.policy, getPolicy, {
|
|
|
|
retry: 3,
|
|
|
|
refetchOnWindowFocus: true,
|
|
|
|
staleTime: 60000, // 1 minute
|
|
|
|
cacheTime: Infinity,
|
2022-11-02 12:02:43 -07:00
|
|
|
enabled: !!account && features.truthPolicies,
|
2022-11-01 10:22:29 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function useAcceptPolicy() {
|
|
|
|
const api = useApi();
|
|
|
|
|
|
|
|
return useMutation((
|
|
|
|
{ policy_id }: { policy_id: string },
|
|
|
|
) => api.patch(`/api/v1/truth/policies/${policy_id}/accept`), {
|
|
|
|
onSuccess() {
|
|
|
|
queryClient.setQueryData(PolicyKeys.policy, {});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export { usePendingPolicy, useAcceptPolicy, PolicyKeys };
|