mirror of
https://github.com/aljazceru/landscape-template.git
synced 2026-01-17 21:34:23 +01:00
feat: build generic voting api, build useVote hook
This commit is contained in:
@@ -5,3 +5,4 @@ export * from "./useInfiniteQuery";
|
||||
export * from "./useReachedBottom";
|
||||
export * from "./useAutoResizableTextArea";
|
||||
export * from "./useCopyToClipboard";
|
||||
export * from "./useVote";
|
||||
|
||||
1
src/utils/hooks/useVote/index.ts
Normal file
1
src/utils/hooks/useVote/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './useVote'
|
||||
96
src/utils/hooks/useVote/useVote.tsx
Normal file
96
src/utils/hooks/useVote/useVote.tsx
Normal 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
|
||||
}
|
||||
}
|
||||
23
src/utils/hooks/useVote/vote.graphql
Normal file
23
src/utils/hooks/useVote/vote.graphql
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user