-
Notifications
You must be signed in to change notification settings - Fork 1
/
useServiceReview.ts
77 lines (73 loc) · 2.27 KB
/
useServiceReview.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { useWalletKit } from "@mysten/wallet-kit";
import { toast } from "react-hot-toast";
import { useSui } from "./useSui";
import { TransactionBlock } from "@mysten/sui.js/transactions";
import { SuiMoveObject } from "@mysten/sui.js";
export const useServiceReview = () => {
const { executeSignedTransactionBlock, suiClient } = useSui();
const { signTransactionBlock } = useWalletKit();
const handleDeleteReview = async (delistedId: string, reviewId: string) => {
// todo: get ServiceId from reviewId
let serviceId = "";
await suiClient
.getObject({
id: reviewId,
options: {
showContent: true,
},
})
.then(async (res) => {
// console.log(res);
serviceId = (res.data?.content as SuiMoveObject).fields.service_id;
})
.catch((err) => {
console.log(err);
toast.error("Review delete failed.");
return;
});
const tx = new TransactionBlock();
console.log(`serviceId=${serviceId} reviewId=${reviewId} delistedId=${delistedId}`);
tx.moveCall({
target: `${process.env.NEXT_PUBLIC_PACKAGE}::service::delete_review`,
arguments: [
tx.object(serviceId),
tx.object(reviewId),
tx.object(delistedId),
],
});
tx.setGasBudget(1000000000);
return signTransactionBlock({
transactionBlock: tx,
})
.then(async (signedTx: any) => {
try {
let resp = await executeSignedTransactionBlock({
signedTx,
requestType: "WaitForLocalExecution",
options: {
showEffects: true,
showEvents: true,
},
});
console.log(resp);
if (resp.effects?.status.status === "success") {
console.log("Review deleted");
toast.success("Review deleted");
return;
} else {
console.log("Review delete failed");
toast.error("Review delete failed.");
return;
}
} catch (err) {
console.log("Review delete failed");
console.log(err);
toast.error("Something went wrong");
}
})
.catch((err) => {
console.log("Error while signing tx" + err);
});
};
return { handleDeleteReview };
};