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

45 lines
929 B
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';
type Validation = {
error: string
message: string
}
const ValidationKeys = {
validation: (name: string) => ['group', 'validation', name] as const,
};
function useGroupValidation(name: string = '') {
const api = useApi();
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, {
enabled: !!name,
});
return {
...queryInfo,
data: {
...queryInfo.data,
isValid: !queryInfo.data?.error,
},
};
}
export { useGroupValidation };