feat: build generic voting api, build useVote hook

This commit is contained in:
MTG2000
2022-05-21 13:36:34 +03:00
parent 5ff83ddc62
commit cdbd10da4e
12 changed files with 243 additions and 220 deletions

View File

@@ -5,3 +5,4 @@ export * from "./useInfiniteQuery";
export * from "./useReachedBottom";
export * from "./useAutoResizableTextArea";
export * from "./useCopyToClipboard";
export * from "./useVote";

View File

@@ -0,0 +1 @@
export * from './useVote'

View File

@@ -0,0 +1,96 @@
import { gql } from '@apollo/client';
import { useState } from 'react';
import { useConfirmVoteMutation, useVoteMutation } from 'src/graphql';
import { Wallet_Service } from 'src/services';
export enum PaymentStatus {
DEFAULT,
FETCHING_PAYMENT_DETAILS,
PAID,
AWAITING_PAYMENT,
PAYMENT_CONFIRMED,
NOT_PAID,
CANCELED
}
export const useVote = ({ onSuccess, onError }: {
onSuccess?: () => void
onError?: (error: any) => void
}) => {
const [paymentStatus, setPaymentStatus] = useState<PaymentStatus>(PaymentStatus.DEFAULT);
const [voteMutaion] = useVoteMutation({
onCompleted: async (votingData) => {
try {
setPaymentStatus(PaymentStatus.AWAITING_PAYMENT);
const webln = await Wallet_Service.getWebln()
const paymentResponse = await webln.sendPayment(votingData.vote.payment_request);
setPaymentStatus(PaymentStatus.PAID);
confirmVote({
variables: {
paymentRequest: votingData.vote.payment_request,
preimage: paymentResponse.preimage
}
})
} catch (error) {
console.log(error);
setPaymentStatus(PaymentStatus.NOT_PAID);
}
},
onError: (error) => {
console.log(error);
alert("Something wrong happened...")
setPaymentStatus(PaymentStatus.NOT_PAID);
onError?.(error)
}
});
const [confirmVote] = useConfirmVoteMutation({
onCompleted: () => {
setPaymentStatus(PaymentStatus.PAYMENT_CONFIRMED);
onSuccess?.();
},
update(cache, { data }) {
try {
const { item_id, item_type, amount_in_sat } = data!.confirmVote;
const { votes_count } = cache.readFragment({
id: `${item_type}:${item_id}`,
fragment: gql`
fragment My${item_type} on ${item_type} {
votes_count
}`
}) ?? {};
cache.writeFragment({
id: `${item_type}:${item_id}`,
fragment: gql`
fragment My${item_type} on ${item_type} {
votes_count
}
`,
data: {
votes_count: votes_count + amount_in_sat
},
})
} catch (error) {
}
},
onError: () => { }
});
const vote = (...params: Parameters<typeof voteMutaion>) => {
setPaymentStatus(PaymentStatus.FETCHING_PAYMENT_DETAILS)
voteMutaion(...params)
}
return {
paymentStatus,
vote
}
}

View File

@@ -0,0 +1,23 @@
mutation Vote($itemType: VOTE_ITEM_TYPE!, $itemId: Int!, $amountInSat: Int!) {
vote(item_type: $itemType, item_id: $itemId, amount_in_sat: $amountInSat) {
id
amount_in_sat
payment_request
payment_hash
paid
item_type
item_id
}
}
mutation ConfirmVote($paymentRequest: String!, $preimage: String!) {
confirmVote(payment_request: $paymentRequest, preimage: $preimage) {
id
amount_in_sat
payment_request
payment_hash
paid
item_type
item_id
}
}