JFIFxxC      C  " }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3Rbr{ gilour

File "update-localization.ts"

Full Path: /home/markqprx/iniasli.pro/client/admin/translations/update-localization.ts
File size: 1.42 KB
MIME-type: text/plain
Charset: utf-8

import {useMutation} from '@tanstack/react-query';
import {UseFormReturn} from 'react-hook-form';
import {toast} from '../../ui/toast/toast';
import {BackendResponse} from '../../http/backend-response/backend-response';
import {apiClient, queryClient} from '../../http/query-client';
import {message} from '../../i18n/message';
import {DatatableDataQueryKey} from '../../datatable/requests/paginated-resources';
import {Localization} from '../../i18n/localization';
import {onFormQueryError} from '../../errors/on-form-query-error';
import {showHttpErrorToast} from '../../utils/http/show-http-error-toast';
import {getLocalWithLinesQueryKey} from './use-locale-with-lines';

interface Response extends BackendResponse {
  localization: Localization;
}

function UpdateLocalization({
  id,
  ...other
}: Partial<Localization>): Promise<Response> {
  return apiClient.put(`localizations/${id}`, other).then(r => r.data);
}

export function useUpdateLocalization(
  form?: UseFormReturn<Partial<Localization>>,
) {
  return useMutation({
    mutationFn: (props: Partial<Localization>) => UpdateLocalization(props),
    onSuccess: () => {
      toast(message('Localization updated'));
      queryClient.invalidateQueries({
        queryKey: DatatableDataQueryKey('localizations'),
      });
      queryClient.invalidateQueries({queryKey: getLocalWithLinesQueryKey()});
    },
    onError: r => (form ? onFormQueryError(r, form) : showHttpErrorToast(r)),
  });
}