34
common/resources/client/admin/channels/requests/use-add-to-channel.ts
Executable file
34
common/resources/client/admin/channels/requests/use-add-to-channel.ts
Executable file
@@ -0,0 +1,34 @@
|
||||
import {BackendResponse} from '@common/http/backend-response/backend-response';
|
||||
import {useMutation} from '@tanstack/react-query';
|
||||
import {apiClient, queryClient} from '@common/http/query-client';
|
||||
import {showHttpErrorToast} from '@common/utils/http/show-http-error-toast';
|
||||
import {NormalizedModel} from '@common/datatable/filters/normalized-model';
|
||||
import {channelQueryKey} from '@common/channels/requests/use-channel';
|
||||
|
||||
interface Response extends BackendResponse {}
|
||||
|
||||
interface Payload {
|
||||
channelId: number | string;
|
||||
item: NormalizedModel;
|
||||
}
|
||||
|
||||
export function useAddToChannel() {
|
||||
return useMutation({
|
||||
mutationFn: (payload: Payload) => addToChannel(payload),
|
||||
onSuccess: async (_, payload) => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: channelQueryKey(payload.channelId),
|
||||
});
|
||||
},
|
||||
onError: r => showHttpErrorToast(r),
|
||||
});
|
||||
}
|
||||
|
||||
function addToChannel({channelId, item}: Payload): Promise<Response> {
|
||||
return apiClient
|
||||
.post(`channel/${channelId}/add`, {
|
||||
itemId: item.id,
|
||||
itemType: item.model_type,
|
||||
})
|
||||
.then(r => r.data);
|
||||
}
|
||||
29
common/resources/client/admin/channels/requests/use-addable-content.ts
Executable file
29
common/resources/client/admin/channels/requests/use-addable-content.ts
Executable file
@@ -0,0 +1,29 @@
|
||||
import {keepPreviousData, useQuery} from '@tanstack/react-query';
|
||||
import {apiClient} from '@common/http/query-client';
|
||||
import {BackendResponse} from '@common/http/backend-response/backend-response';
|
||||
import {NormalizedModel} from '@common/datatable/filters/normalized-model';
|
||||
|
||||
export interface SearchResponse extends BackendResponse {
|
||||
results: NormalizedModel[];
|
||||
}
|
||||
|
||||
interface SearchParams {
|
||||
query?: string;
|
||||
limit?: number;
|
||||
modelType: string;
|
||||
}
|
||||
|
||||
export function useAddableContent(params: SearchParams) {
|
||||
return useQuery({
|
||||
queryKey: ['search', params],
|
||||
queryFn: () => search(params),
|
||||
//enabled: !!params.query,
|
||||
placeholderData: params.query ? keepPreviousData : undefined,
|
||||
});
|
||||
}
|
||||
|
||||
function search(params: SearchParams) {
|
||||
return apiClient
|
||||
.get<SearchResponse>(`channel/search-for-addable-content`, {params})
|
||||
.then(response => response.data);
|
||||
}
|
||||
34
common/resources/client/admin/channels/requests/use-apply-channel-preset.ts
Executable file
34
common/resources/client/admin/channels/requests/use-apply-channel-preset.ts
Executable file
@@ -0,0 +1,34 @@
|
||||
import {useMutation} from '@tanstack/react-query';
|
||||
import {useTrans} from '@common/i18n/use-trans';
|
||||
import {toast} from '@common/ui/toast/toast';
|
||||
import {message} from '@common/i18n/message';
|
||||
import {apiClient, queryClient} from '@common/http/query-client';
|
||||
import {DatatableDataQueryKey} from '@common/datatable/requests/paginated-resources';
|
||||
import {BackendResponse} from '@common/http/backend-response/backend-response';
|
||||
import {showHttpErrorToast} from '@common/utils/http/show-http-error-toast';
|
||||
|
||||
interface Response extends BackendResponse {}
|
||||
|
||||
interface Payload {
|
||||
preset: string;
|
||||
}
|
||||
|
||||
export function useApplyChannelPreset() {
|
||||
const {trans} = useTrans();
|
||||
return useMutation({
|
||||
mutationFn: (payload: Payload) => resetChannels(payload),
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: DatatableDataQueryKey('channel'),
|
||||
});
|
||||
toast(trans(message('Channel preset applied')));
|
||||
},
|
||||
onError: err => showHttpErrorToast(err),
|
||||
});
|
||||
}
|
||||
|
||||
function resetChannels(payload: Payload) {
|
||||
return apiClient
|
||||
.post<Response>('channel/apply-preset', payload)
|
||||
.then(r => r.data);
|
||||
}
|
||||
47
common/resources/client/admin/channels/requests/use-create-channel.ts
Executable file
47
common/resources/client/admin/channels/requests/use-create-channel.ts
Executable file
@@ -0,0 +1,47 @@
|
||||
import {useMutation, useQueryClient} from '@tanstack/react-query';
|
||||
import {UseFormReturn} from 'react-hook-form';
|
||||
import {apiClient} from '@common/http/query-client';
|
||||
import {toast} from '@common/ui/toast/toast';
|
||||
import {DatatableDataQueryKey} from '@common/datatable/requests/paginated-resources';
|
||||
import {useTrans} from '@common/i18n/use-trans';
|
||||
import {onFormQueryError} from '@common/errors/on-form-query-error';
|
||||
import {message} from '@common/i18n/message';
|
||||
import {useNavigate} from '@common/utils/hooks/use-navigate';
|
||||
import {PaginationResponse} from '@common/http/backend-response/pagination-response';
|
||||
import {NormalizedModel} from '@common/datatable/filters/normalized-model';
|
||||
import {BackendResponse} from '@common/http/backend-response/backend-response';
|
||||
import {Channel} from '@common/channels/channel';
|
||||
|
||||
const endpoint = 'channel';
|
||||
|
||||
interface Response extends BackendResponse {
|
||||
channel: Channel;
|
||||
}
|
||||
|
||||
export interface CreateChannelPayload
|
||||
extends Omit<Channel, 'content' | 'items'> {
|
||||
content: PaginationResponse<NormalizedModel>;
|
||||
}
|
||||
|
||||
export function useCreateChannel(form: UseFormReturn<CreateChannelPayload>) {
|
||||
const {trans} = useTrans();
|
||||
const navigate = useNavigate();
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (payload: CreateChannelPayload) => createChannel(payload),
|
||||
onSuccess: async response => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: DatatableDataQueryKey(endpoint),
|
||||
});
|
||||
toast(trans(message('Channel created')));
|
||||
navigate(`/admin/channels/${response.channel.id}/edit`, {
|
||||
replace: true,
|
||||
});
|
||||
},
|
||||
onError: err => onFormQueryError(err, form),
|
||||
});
|
||||
}
|
||||
|
||||
function createChannel(payload: CreateChannelPayload) {
|
||||
return apiClient.post<Response>(endpoint, payload).then(r => r.data);
|
||||
}
|
||||
31
common/resources/client/admin/channels/requests/use-remove-from-channel.ts
Executable file
31
common/resources/client/admin/channels/requests/use-remove-from-channel.ts
Executable file
@@ -0,0 +1,31 @@
|
||||
import {useMutation} from '@tanstack/react-query';
|
||||
import {apiClient, queryClient} from '@common/http/query-client';
|
||||
import {showHttpErrorToast} from '@common/utils/http/show-http-error-toast';
|
||||
import {NormalizedModel} from '@common/datatable/filters/normalized-model';
|
||||
import {channelQueryKey} from '@common/channels/requests/use-channel';
|
||||
|
||||
interface Payload {
|
||||
channelId: number | string;
|
||||
item: NormalizedModel;
|
||||
}
|
||||
|
||||
export function useRemoveFromChannel() {
|
||||
return useMutation({
|
||||
mutationFn: (payload: Payload) => removeFromChannel(payload),
|
||||
onSuccess: async (_, payload) => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: channelQueryKey(payload.channelId),
|
||||
});
|
||||
},
|
||||
onError: r => showHttpErrorToast(r),
|
||||
});
|
||||
}
|
||||
|
||||
function removeFromChannel({channelId, item}: Payload) {
|
||||
return apiClient
|
||||
.post(`channel/${channelId}/remove`, {
|
||||
itemId: item.id,
|
||||
itemType: item.model_type,
|
||||
})
|
||||
.then(r => r.data);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import {useMutation} from '@tanstack/react-query';
|
||||
import {apiClient} from '@common/http/query-client';
|
||||
import {BackendResponse} from '@common/http/backend-response/backend-response';
|
||||
import {showHttpErrorToast} from '@common/utils/http/show-http-error-toast';
|
||||
import {NormalizedModel} from '@common/datatable/filters/normalized-model';
|
||||
import {Channel} from '@common/channels/channel';
|
||||
|
||||
interface Response extends BackendResponse {
|
||||
channel: Channel<NormalizedModel>;
|
||||
}
|
||||
|
||||
interface Payload {
|
||||
channelId: number | string;
|
||||
modelType: string;
|
||||
ids: (number | string)[];
|
||||
}
|
||||
|
||||
export function useReorderChannelContent() {
|
||||
return useMutation({
|
||||
mutationFn: (payload: Payload) => reorderContent(payload),
|
||||
onError: err => showHttpErrorToast(err),
|
||||
});
|
||||
}
|
||||
|
||||
function reorderContent({channelId, ids, modelType}: Payload) {
|
||||
return apiClient
|
||||
.post<Response>(`channel/${channelId}/reorder-content`, {
|
||||
modelType,
|
||||
ids,
|
||||
})
|
||||
.then(r => r.data);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import {useMutation} from '@tanstack/react-query';
|
||||
import {useTrans} from '@common/i18n/use-trans';
|
||||
import {toast} from '@common/ui/toast/toast';
|
||||
import {message} from '@common/i18n/message';
|
||||
import {apiClient, queryClient} from '@common/http/query-client';
|
||||
import {BackendResponse} from '@common/http/backend-response/backend-response';
|
||||
import {showHttpErrorToast} from '@common/utils/http/show-http-error-toast';
|
||||
import {NormalizedModel} from '@common/datatable/filters/normalized-model';
|
||||
import {Channel, ChannelConfig} from '@common/channels/channel';
|
||||
import {channelQueryKey} from '@common/channels/requests/use-channel';
|
||||
|
||||
interface Response extends BackendResponse {
|
||||
channel: Channel<NormalizedModel>;
|
||||
}
|
||||
|
||||
interface Payload {
|
||||
channelConfig?: Partial<ChannelConfig>;
|
||||
}
|
||||
|
||||
export function useUpdateChannelContent(channelId: number | string) {
|
||||
const {trans} = useTrans();
|
||||
return useMutation({
|
||||
mutationFn: (payload: Payload) => updateChannel(channelId, payload),
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: channelQueryKey(channelId),
|
||||
});
|
||||
toast(trans(message('Channel content updated')));
|
||||
},
|
||||
onError: err => showHttpErrorToast(err),
|
||||
});
|
||||
}
|
||||
|
||||
function updateChannel(channelId: number | string, payload: Payload) {
|
||||
return apiClient
|
||||
.post<Response>(`channel/${channelId}/update-content`, {
|
||||
...payload,
|
||||
normalizeContent: true,
|
||||
})
|
||||
.then(r => r.data);
|
||||
}
|
||||
45
common/resources/client/admin/channels/requests/use-update-channel.ts
Executable file
45
common/resources/client/admin/channels/requests/use-update-channel.ts
Executable file
@@ -0,0 +1,45 @@
|
||||
import {useMutation} from '@tanstack/react-query';
|
||||
import {useTrans} from '@common/i18n/use-trans';
|
||||
import {useNavigate} from '@common/utils/hooks/use-navigate';
|
||||
import {toast} from '@common/ui/toast/toast';
|
||||
import {message} from '@common/i18n/message';
|
||||
import {apiClient, queryClient} from '@common/http/query-client';
|
||||
import {DatatableDataQueryKey} from '@common/datatable/requests/paginated-resources';
|
||||
import {onFormQueryError} from '@common/errors/on-form-query-error';
|
||||
import {UseFormReturn} from 'react-hook-form';
|
||||
import {BackendResponse} from '@common/http/backend-response/backend-response';
|
||||
import {Channel} from '@common/channels/channel';
|
||||
import {CreateChannelPayload} from '@common/admin/channels/requests/use-create-channel';
|
||||
|
||||
interface Response extends BackendResponse {
|
||||
channel: Channel;
|
||||
}
|
||||
|
||||
export interface UpdateChannelPayload extends CreateChannelPayload {
|
||||
id: number;
|
||||
}
|
||||
|
||||
const Endpoint = (id: number) => `channel/${id}`;
|
||||
|
||||
export function useUpdateChannel(form: UseFormReturn<UpdateChannelPayload>) {
|
||||
const {trans} = useTrans();
|
||||
const navigate = useNavigate();
|
||||
return useMutation({
|
||||
mutationFn: (payload: UpdateChannelPayload) => updateChannel(payload),
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: DatatableDataQueryKey('channel'),
|
||||
});
|
||||
toast(trans(message('Channel updated')));
|
||||
navigate('/admin/channels');
|
||||
},
|
||||
onError: err => onFormQueryError(err, form),
|
||||
});
|
||||
}
|
||||
|
||||
function updateChannel({
|
||||
id,
|
||||
...payload
|
||||
}: UpdateChannelPayload): Promise<Response> {
|
||||
return apiClient.put(Endpoint(id), payload).then(r => r.data);
|
||||
}
|
||||
Reference in New Issue
Block a user