mirror of
https://github.com/aljazceru/landscape-template.git
synced 2026-01-09 17:34:24 +01:00
feat: implement custom modals for no-webln error on mobile and web
Fixes #91
This commit is contained in:
@@ -41,81 +41,93 @@ export const useVote = (params: Params) => {
|
||||
|
||||
if (!itemId || !itemType) return;
|
||||
|
||||
setPaymentStatus(PaymentStatus.FETCHING_PAYMENT_DETAILS)
|
||||
voteMutaion({
|
||||
variables: {
|
||||
itemId,
|
||||
itemType,
|
||||
amountInSat: amount
|
||||
},
|
||||
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);
|
||||
|
||||
//Confirm Voting payment
|
||||
confirmVote({
|
||||
variables: {
|
||||
paymentRequest: votingData.vote.payment_request,
|
||||
preimage: paymentResponse.preimage
|
||||
},
|
||||
onCompleted: () => {
|
||||
setPaymentStatus(PaymentStatus.PAYMENT_CONFIRMED);
|
||||
onSuccess?.(votingData.vote.amount_in_sat);
|
||||
onSetteled?.()
|
||||
},
|
||||
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?.(error)
|
||||
}
|
||||
},
|
||||
|
||||
onError: (error) => {
|
||||
setPaymentStatus(PaymentStatus.NETWORK_ERROR);
|
||||
onError?.(error);
|
||||
onSetteled?.();
|
||||
alert("A network error happened while confirming the payment...")
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
setPaymentStatus(PaymentStatus.CANCELED);
|
||||
onError?.(error);
|
||||
onSetteled?.();
|
||||
alert("Payment rejected by user")
|
||||
Wallet_Service.getWebln()
|
||||
.then(webln => {
|
||||
|
||||
if (!webln) {
|
||||
onError?.(new Error('No WebLN Detetcted'))
|
||||
onSetteled?.()
|
||||
return
|
||||
}
|
||||
|
||||
},
|
||||
onError: (error) => {
|
||||
console.log(error);
|
||||
setPaymentStatus(PaymentStatus.NETWORK_ERROR);
|
||||
onError?.(error);
|
||||
onSetteled?.();
|
||||
alert("A network error happened...")
|
||||
}
|
||||
})
|
||||
setPaymentStatus(PaymentStatus.FETCHING_PAYMENT_DETAILS)
|
||||
voteMutaion({
|
||||
variables: {
|
||||
itemId,
|
||||
itemType,
|
||||
amountInSat: amount
|
||||
},
|
||||
onCompleted: async (votingData) => {
|
||||
try {
|
||||
setPaymentStatus(PaymentStatus.AWAITING_PAYMENT);
|
||||
const paymentResponse = await webln.sendPayment(votingData.vote.payment_request);
|
||||
setPaymentStatus(PaymentStatus.PAID);
|
||||
|
||||
//Confirm Voting payment
|
||||
confirmVote({
|
||||
variables: {
|
||||
paymentRequest: votingData.vote.payment_request,
|
||||
preimage: paymentResponse.preimage
|
||||
},
|
||||
onCompleted: () => {
|
||||
setPaymentStatus(PaymentStatus.PAYMENT_CONFIRMED);
|
||||
onSuccess?.(votingData.vote.amount_in_sat);
|
||||
onSetteled?.()
|
||||
},
|
||||
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?.(error)
|
||||
}
|
||||
},
|
||||
|
||||
onError: (error) => {
|
||||
setPaymentStatus(PaymentStatus.NETWORK_ERROR);
|
||||
onError?.(error);
|
||||
onSetteled?.();
|
||||
alert("A network error happened while confirming the payment...")
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
setPaymentStatus(PaymentStatus.CANCELED);
|
||||
onError?.(error);
|
||||
onSetteled?.();
|
||||
alert("Payment rejected by user")
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
onError: (error) => {
|
||||
console.log(error);
|
||||
setPaymentStatus(PaymentStatus.NETWORK_ERROR);
|
||||
onError?.(error);
|
||||
onSetteled?.();
|
||||
alert("A network error happened...")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
}, [confirmVote, voteMutaion, params.itemId, params.itemType, params.onError, params.onSetteled, params.onSuccess]);
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user