Dashboard: hooks cleanup
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
@ -1,11 +1,6 @@
|
||||
export { useCreateDomain, type CreateDomainParams } from './useCreateDomain';
|
||||
export { useCreateRelay } from './useCreateRelay';
|
||||
export { useDeleteDomain } from './useDeleteDomain';
|
||||
export { useDeleteRelay } from './useDeleteRelay';
|
||||
export { useDomains } from './useDomains';
|
||||
export { useModerationLog } from './useModerationLog';
|
||||
export { useRelays } from './useRelays';
|
||||
export { useRules } from './useRules';
|
||||
export { useSuggest } from './useSuggest';
|
||||
export { useUpdateDomain } from './useUpdateDomain';
|
||||
export { useVerify } from './useVerify';
|
||||
@ -1,18 +0,0 @@
|
||||
import { Entities } from 'soapbox/entity-store/entities';
|
||||
import { useCreateEntity } from 'soapbox/entity-store/hooks';
|
||||
import { useApi } from 'soapbox/hooks';
|
||||
import { relaySchema } from 'soapbox/schemas';
|
||||
|
||||
const useCreateRelay = () => {
|
||||
const api = useApi();
|
||||
|
||||
const { createEntity, ...rest } = useCreateEntity([Entities.RELAYS], (relayUrl: string) =>
|
||||
api.post('/api/v1/pleroma/admin/relay', { relay_url: relayUrl }), { schema: relaySchema });
|
||||
|
||||
return {
|
||||
createRelay: createEntity,
|
||||
...rest,
|
||||
};
|
||||
};
|
||||
|
||||
export { useCreateRelay };
|
||||
@ -1,19 +0,0 @@
|
||||
import { Entities } from 'soapbox/entity-store/entities';
|
||||
import { useDeleteEntity } from 'soapbox/entity-store/hooks';
|
||||
import { useApi } from 'soapbox/hooks';
|
||||
|
||||
const useDeleteRelay = () => {
|
||||
const api = useApi();
|
||||
|
||||
const { deleteEntity, ...rest } = useDeleteEntity(Entities.RELAYS, (relayUrl: string) =>
|
||||
api.delete('/api/v1/pleroma/admin/relay', {
|
||||
data: { relay_url: relayUrl },
|
||||
}));
|
||||
|
||||
return {
|
||||
mutate: deleteEntity,
|
||||
...rest,
|
||||
};
|
||||
};
|
||||
|
||||
export { useDeleteRelay };
|
||||
@ -1,8 +1,21 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { useApi } from 'soapbox/hooks';
|
||||
import { queryClient } from 'soapbox/queries/client';
|
||||
import { domainSchema, type Domain } from 'soapbox/schemas';
|
||||
|
||||
import type { AxiosResponse } from 'axios';
|
||||
|
||||
interface CreateDomainParams {
|
||||
domain: string;
|
||||
public: boolean;
|
||||
}
|
||||
|
||||
interface UpdateDomainParams {
|
||||
id: string;
|
||||
public: boolean;
|
||||
}
|
||||
|
||||
const useDomains = () => {
|
||||
const api = useApi();
|
||||
|
||||
@ -14,12 +27,56 @@ const useDomains = () => {
|
||||
};
|
||||
|
||||
const result = useQuery<ReadonlyArray<Domain>>({
|
||||
queryKey: ['domains'],
|
||||
queryKey: ['admin', 'domains'],
|
||||
queryFn: getDomains,
|
||||
placeholderData: [],
|
||||
});
|
||||
|
||||
return result;
|
||||
const {
|
||||
mutate: createDomain,
|
||||
isPending: isCreating,
|
||||
} = useMutation({
|
||||
mutationFn: (params: CreateDomainParams) => api.post('/api/v1/pleroma/admin/domains', params),
|
||||
retry: false,
|
||||
onSuccess: ({ data }: AxiosResponse) =>
|
||||
queryClient.setQueryData(['admin', 'domains'], (prevResult: ReadonlyArray<Domain>) =>
|
||||
[...prevResult, domainSchema.parse(data)],
|
||||
),
|
||||
});
|
||||
|
||||
const {
|
||||
mutate: updateDomain,
|
||||
isPending: isUpdating,
|
||||
} = useMutation({
|
||||
mutationFn: ({ id, ...params }: UpdateDomainParams) => api.patch(`/api/v1/pleroma/admin/domains/${id}`, params),
|
||||
retry: false,
|
||||
onSuccess: ({ data }: AxiosResponse) =>
|
||||
queryClient.setQueryData(['admin', 'domains'], (prevResult: ReadonlyArray<Domain>) =>
|
||||
prevResult.map((domain) => domain.id === data.id ? domainSchema.parse(data) : domain),
|
||||
),
|
||||
});
|
||||
|
||||
const {
|
||||
mutate: deleteDomain,
|
||||
isPending: isDeleting,
|
||||
} = useMutation({
|
||||
mutationFn: (id: string) => api.delete(`/api/v1/pleroma/admin/domains/${id}`),
|
||||
retry: false,
|
||||
onSuccess: (_, id) =>
|
||||
queryClient.setQueryData(['admin', 'domains'], (prevResult: ReadonlyArray<Domain>) =>
|
||||
prevResult.filter(({ id: domainId }) => domainId !== id),
|
||||
),
|
||||
});
|
||||
|
||||
return {
|
||||
...result,
|
||||
createDomain,
|
||||
isCreating,
|
||||
updateDomain,
|
||||
isUpdating,
|
||||
deleteDomain,
|
||||
isDeleting,
|
||||
};
|
||||
};
|
||||
|
||||
export { useDomains };
|
||||
|
||||
@ -25,7 +25,7 @@ const useModerationLog = () => {
|
||||
};
|
||||
|
||||
const queryInfo = useInfiniteQuery({
|
||||
queryKey: ['moderation_log'],
|
||||
queryKey: ['admin', 'moderation_log'],
|
||||
queryFn: ({ pageParam }) => getModerationLog(pageParam),
|
||||
initialPageParam: 1,
|
||||
getNextPageParam: (page, allPages) => flattenPages(allPages)!.length >= page.total ? undefined : allPages.length + 1,
|
||||
|
||||
@ -1,8 +1,11 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { useApi } from 'soapbox/hooks';
|
||||
import { queryClient } from 'soapbox/queries/client';
|
||||
import { relaySchema, type Relay } from 'soapbox/schemas';
|
||||
|
||||
import type { AxiosResponse } from 'axios';
|
||||
|
||||
const useRelays = () => {
|
||||
const api = useApi();
|
||||
|
||||
@ -14,12 +17,44 @@ const useRelays = () => {
|
||||
};
|
||||
|
||||
const result = useQuery<ReadonlyArray<Relay>>({
|
||||
queryKey: ['relays'],
|
||||
queryKey: ['admin', 'relays'],
|
||||
queryFn: getRelays,
|
||||
placeholderData: [],
|
||||
});
|
||||
|
||||
return result;
|
||||
const {
|
||||
mutate: followRelay,
|
||||
isPending: isPendingFollow,
|
||||
} = useMutation({
|
||||
mutationFn: (relayUrl: string) => api.post('/api/v1/pleroma/admin/relays', { relay_url: relayUrl }),
|
||||
retry: false,
|
||||
onSuccess: ({ data }: AxiosResponse) =>
|
||||
queryClient.setQueryData(['admin', 'relays'], (prevResult: ReadonlyArray<Relay>) =>
|
||||
[...prevResult, relaySchema.parse(data)],
|
||||
),
|
||||
});
|
||||
|
||||
const {
|
||||
mutate: unfollowRelay,
|
||||
isPending: isPendingUnfollow,
|
||||
} = useMutation({
|
||||
mutationFn: (relayUrl: string) => api.delete('/api/v1/pleroma/admin/relays', {
|
||||
data: { relay_url: relayUrl },
|
||||
}),
|
||||
retry: false,
|
||||
onSuccess: (_, relayUrl) =>
|
||||
queryClient.setQueryData(['admin', 'relays'], (prevResult: ReadonlyArray<Relay>) =>
|
||||
prevResult.filter(({ actor }) => actor !== relayUrl),
|
||||
),
|
||||
});
|
||||
|
||||
return {
|
||||
...result,
|
||||
followRelay,
|
||||
isPendingFollow,
|
||||
unfollowRelay,
|
||||
isPendingUnfollow,
|
||||
};
|
||||
};
|
||||
|
||||
export { useRelays };
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
||||
import { AxiosResponse } from 'axios';
|
||||
|
||||
import { useApi } from 'soapbox/hooks';
|
||||
import { queryClient } from 'soapbox/queries/client';
|
||||
import { adminRuleSchema, type AdminRule } from 'soapbox/schemas';
|
||||
|
||||
import type { AxiosResponse } from 'axios';
|
||||
|
||||
interface CreateRuleParams {
|
||||
priority?: number;
|
||||
text: string;
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
* @module soapbox/api
|
||||
*/
|
||||
|
||||
import axios, { AxiosInstance, AxiosResponse } from 'axios';
|
||||
import axios, { type AxiosInstance, type AxiosResponse } from 'axios';
|
||||
import LinkHeader from 'http-link-header';
|
||||
import { createSelector } from 'reselect';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user