-
Notifications
You must be signed in to change notification settings - Fork 1
/
page.tsx
166 lines (150 loc) · 5.08 KB
/
page.tsx
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"use client";
import { useParams } from "next/navigation";
import { useGetReviews } from "@/app/hooks/useGetReviews";
import { useRouter } from "next/navigation";
import React, { useEffect, useState } from "react";
import { AddReview } from "@/app/components/review/AddReview";
import { TransactionBlock } from "@mysten/sui.js/transactions";
import { useWalletKit } from "@mysten/wallet-kit";
import { Button, Table } from "flowbite-react";
import { HiOutlineArrowRight, HiOutlinePencilAlt } from "react-icons/hi";
import { SuiMoveObject } from "@mysten/sui.js";
import { Review } from "@/app/types/Review";
import { useSui } from "@/app/hooks/useSui";
import { useAuthentication } from "@/app/hooks/useAuthentication";
interface ReviewType {
id: string;
priority: string;
}
export default function Service() {
const router = useRouter();
const { user } = useAuthentication();
const { id } = useParams();
const { suiClient } = useSui();
const { dataReviews, dataName, dataStars, isLoading, currentAccount } =
useGetReviews(id);
const [reviews, setReviews] = useState([] as ReviewType[]);
const [openModal, setOpenModal] = useState(false);
const { signAndExecuteTransactionBlock } = useWalletKit();
const SUI_CLOCK =
"0x0000000000000000000000000000000000000000000000000000000000000006";
const onDisplayReview = (review: ReviewType) => {
router.push(`/review/${review.id}`);
};
useEffect(() => {
async function getReviews() {
if (isLoading) {
return;
}
console.dir(
`reviews: ${JSON.stringify(dataReviews)} size=${dataReviews.length}`,
);
const reviewsPromises = dataReviews.map(async (item: string) => {
console.log(`review: ${JSON.stringify(item)}`);
let total_score = "0";
await suiClient
.getObject({
id: item,
options: {
showContent: true,
},
})
.then(async (res) => {
total_score = (
(res.data?.content as SuiMoveObject).fields as Review
).total_score.toString();
});
return { id: item, priority: total_score };
});
setReviews(await Promise.all(reviewsPromises));
console.dir(`reviews_post: ${JSON.stringify(reviews)}`);
}
getReviews();
}, [currentAccount, isLoading, dataReviews]);
const createReview = async (
reviewBody: string,
overallRate: string,
): Promise<void> => {
const tx = new TransactionBlock();
// console.log(`serviceId=${id}`);
// console.log(`review_body=${reviewBody}`);
// console.log(`review_len=${reviewBody.length}`);
tx.moveCall({
target: `${process.env.NEXT_PUBLIC_PACKAGE}::service::write_new_review_without_poe`,
arguments: [
tx.object(id),
tx.pure(currentAccount?.address),
tx.pure(reviewBody),
tx.pure(overallRate),
tx.object(SUI_CLOCK),
],
});
tx.setGasBudget(1000000000);
const resp = await signAndExecuteTransactionBlock({
transactionBlock: tx,
options: {
showEffects: true,
showObjectChanges: true,
},
});
console.dir(resp, { depth: null });
window.location.reload();
// router.push(`/service/${id}`);
};
return (
<div className="flex flex-col mx-32 my-10">
<h1>Top Reviews</h1>
<div>Name: {`${dataName}`}</div>
<div>Id: {`${id}`}</div>
<div>Total: {`${reviews.length}`}</div>
<div className="container">
{reviews.length > 0 && (
<Table hoverable className="items-center text-center">
<Table.Head>
<Table.HeadCell>Review ID</Table.HeadCell>
<Table.HeadCell>Score</Table.HeadCell>
<Table.HeadCell>Action</Table.HeadCell>
</Table.Head>
<Table.Body className="divide-y">
{reviews.map((item) => (
<Table.Row
className="bg-white dark:border-gray-700 dark:bg-gray-800"
key={item.id}
>
<Table.Cell>
<div className="overflow-hidden">{item.id}</div>
</Table.Cell>
<Table.Cell>{item.priority}</Table.Cell>
<Table.Cell>
{
<Button
color="gray"
pill
onClick={() => onDisplayReview(item)}
>
Info
<HiOutlineArrowRight className="ml-2 h-5 w-5" />
</Button>
}
</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table>
)}
</div>
{user?.role !== "serviceOwner" && (
<Button color="green" pill onClick={() => setOpenModal(true)}>
Add a new review
<HiOutlinePencilAlt className="ml-2 h-5 w-5" />
</Button>
)}
<AddReview
serviceId={id}
openModal={openModal}
setOpenModal={setOpenModal}
onSubmitReview={createReview}
/>
</div>
);
}