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

File "use-store-vote.ts"

Full Path: /home/markqprx/iniasli.pro/client/votes/requests/use-store-vote.ts
File size: 929 bytes
MIME-type: text/plain
Charset: utf-8

import {useMutation} from '@tanstack/react-query';
import {apiClient} from '@common/http/query-client';
import {BackendResponse} from '@common/http/backend-response/backend-response';
import {VotableModel} from '@common/votes/votable-model';
import {showHttpErrorToast} from '@common/utils/http/show-http-error-toast';

interface Response extends BackendResponse {
  model: VotableModel;
}

interface Payload {
  voteType: 'upvote' | 'downvote';
}

export function useStoreVote(model: VotableModel) {
  return useMutation({
    mutationFn: (payload: Payload) => changeVote(model, payload),
    onSuccess: response => {
      //
    },
    onError: err => showHttpErrorToast(err),
  });
}

function changeVote(model: VotableModel, payload: Payload) {
  return apiClient
    .post<Response>('vote', {
      vote_type: payload.voteType,
      model_id: model.id,
      model_type: model.model_type,
    })
    .then(r => r.data);
}