pleroma/app/soapbox/api/hooks/groups/useGroupValidation.ts

47 lines
1 KiB
TypeScript
Raw Normal View History

2023-03-29 12:42:20 -07:00
import { useQuery } from '@tanstack/react-query';
import { useApi } from 'soapbox/hooks/useApi';
2023-03-29 12:44:00 -07:00
import { useFeatures } from 'soapbox/hooks/useFeatures';
2023-03-29 12:42:20 -07:00
type Validation = {
error: string
message: string
}
const ValidationKeys = {
validation: (name: string) => ['group', 'validation', name] as const,
};
function useGroupValidation(name: string = '') {
const api = useApi();
2023-03-29 12:44:00 -07:00
const features = useFeatures();
2023-03-29 12:42:20 -07:00
const getValidation = async() => {
const { data } = await api.get<Validation>('/api/v1/groups/validate', {
params: { name },
})
.catch((error) => {
if (error.response.status === 422) {
return { data: error.response.data };
}
throw error;
});
return data;
};
const queryInfo = useQuery<Validation>(ValidationKeys.validation(name), getValidation, {
2023-03-29 12:44:00 -07:00
enabled: features.groupsValidation && !!name,
2023-03-29 12:42:20 -07:00
});
return {
...queryInfo,
data: {
...queryInfo.data,
2023-03-29 12:44:00 -07:00
isValid: !queryInfo.data?.error ?? true,
2023-03-29 12:42:20 -07:00
},
};
}
export { useGroupValidation };