(null);
useEffect(() => {
- const parseUrl = () => {
- const pathSegments = router.asPath.split('/');
- const tokenIndex = pathSegments.indexOf('reputation-score') + 1;
-
- if (tokenIndex > 0 && tokenIndex + 1 < pathSegments.length) {
- setTokenId(pathSegments[tokenIndex]);
- setAddress(pathSegments[tokenIndex + 1]);
+ const fetchReputationScore = async (tokenId: string, address: string) => {
+ setLoading(true);
+ setError(null);
+
+ try {
+ const score = await retrieveReputationScore({ tokenId, address });
+
+ setReputationScore(score.reputationScore ?? 0);
+ setCommunityName(score.communityName);
+ } catch (err) {
+ console.error('Error fetching reputation score:', err);
+ setError('Failed to fetch reputation score.');
+ } finally {
+ setLoading(false);
}
};
- parseUrl();
- }, [router.asPath]);
+ const params = new URLSearchParams(window.location.search);
+ const tokenId = params.get('tokenId');
+ const address = params.get('address');
- useEffect(() => {
- if (tokenId && address) {
- const fetchReputationScore = async () => {
- setLoading(true);
- try {
- const score = await retrieveReputationScore({
- tokenId,
- address,
- });
-
- setReputationScore(score.reputationScore ?? 0);
- setCommunityName(score.communityName);
- } catch (error) {
- console.error('Error fetching reputation score:', error);
- } finally {
- setLoading(false);
- }
- };
-
- fetchReputationScore();
+ if (!tokenId || !address) {
+ setError('Invalid URL format. Missing tokenId or address.');
+ setLoading(false);
+ return;
}
- }, [tokenId, address]);
+
+ fetchReputationScore(tokenId, address);
+ }, [retrieveReputationScore]);
const gaugeOptions = {
chart: {
@@ -102,7 +95,7 @@ const ScorePage = () => {
series: [
{
name: 'Score',
- data: [reputationScore ?? 0],
+ data: [reputationScore ? parseFloat(reputationScore.toFixed(1)) : 0],
tooltip: {
valueSuffix: ' /100',
},
@@ -126,6 +119,17 @@ const ScorePage = () => {
return ;
}
+ if (error) {
+ return (
+
+ );
+ }
+
return (
<>
@@ -173,4 +177,4 @@ const ScorePage = () => {
);
};
-export default withRoles(ScorePage, []);
+export default ScorePage;
diff --git a/src/utils/withRoles.tsx b/src/utils/withRoles.tsx
index 84c1b6e8..87eb12d3 100644
--- a/src/utils/withRoles.tsx
+++ b/src/utils/withRoles.tsx
@@ -1,3 +1,5 @@
+'use client';
+
import React, {
ComponentType,
FunctionComponent,
@@ -6,6 +8,8 @@ import React, {
} from 'react';
import { useRouter } from 'next/router';
+import SimpleBackdrop from '@/components/global/LoadingBackdrop';
+
import useAppStore from '../store/useStore';
interface WithRolesProps {
@@ -21,7 +25,7 @@ interface ComponentWithLayout extends FunctionComponent
{
export function withRoles
(
Component: ComponentWithLayout
,
- requiredPermissions: string[]
+ requiredPermissions: string[] = []
): ComponentWithLayout
{
const WithRolesWrapper: ComponentWithLayout
= (props) => {
const userPermissions = useAppStore(
@@ -31,6 +35,11 @@ export function withRoles
(
const router = useRouter();
useEffect(() => {
+ if (requiredPermissions.length === 0) {
+ setIsPermissionLoaded(true);
+ return;
+ }
+
const hasPermission = requiredPermissions.some((permission) =>
userPermissions.includes(permission)
);