Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/app/[lang]/dashboard/agents/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ProfileLayout from "@/components/Dashboard/Profile/ProfileLayout";
import { getServerUserRole } from "@/hooks/api/getUserRole";
import { getServerAgent } from "@/hooks/api/getAgent";
import { getServerUser } from "@/hooks/api/getUser";
import { RouteParams } from "@/types";
import { UserRole } from "need4deed-sdk";
import { cookies } from "next/headers";
Expand All @@ -10,9 +11,17 @@ export default async function DashboardAgentPage({ params }: RouteParams) {

const cookieStore = await cookies();
const cookieHeader = cookieStore.toString();
const userRole = await getServerUserRole(cookieHeader);
const user = await getServerUser(cookieHeader);
const agent = await getServerAgent(cookieHeader, id);
const userRole = user?.role;
const personId = user?.personId;
const matchedPersonId = personId === agent?.representative?.id;

if (!userRole || (userRole !== UserRole.COORDINATOR && userRole !== UserRole.ADMIN)) {
if (
!userRole ||
!personId ||
(userRole !== UserRole.COORDINATOR && userRole !== UserRole.ADMIN && !matchedPersonId)
) {
redirect(`/dashboard/agents`);
}
return <ProfileLayout entityId={id} entityType="agent" />;
Expand Down
11 changes: 10 additions & 1 deletion src/components/Dashboard/Agents/AgentReadOnlyTableRow.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ApiAgentGetList, OptionItem } from "need4deed-sdk";
import { ClickableRow, TableCell } from "@/components/core/common/Table";
import { useTranslation } from "react-i18next";
import { useRouter } from "next/navigation";

interface Props {
agent: ApiAgentGetList;
Expand All @@ -10,10 +12,17 @@ interface Props {
}

export function AgentReadOnlyTableRow({ agent, isLast, districtsList }: Props) {
const { i18n } = useTranslation();
const router = useRouter();
const { id, title, type, district } = agent;
const districtTitle = district?.id ? (districtsList?.find((d) => d.id === district.id)?.title ?? null) : null;

const handleGoToProfile = () => {
if (!id) return;
router.push(`/${i18n.language}/dashboard/agents/${id}`);
};
return (
<ClickableRow $isLast={isLast} $cursor={"auto"} data-testid={`agent-row-${id}`}>
<ClickableRow $isLast={isLast} $cursor={"auto"} data-testid={`agent-row-${id}`} onClick={handleGoToProfile}>
<TableCell data-testid={`agent-title-${id}`}>{title}</TableCell>
<TableCell data-testid={`agent-type-${id}`}>{type}</TableCell>
<TableCell data-testid={`agent-district-${id}`}>{districtTitle || "—"}</TableCell>
Expand Down
27 changes: 27 additions & 0 deletions src/hooks/api/getAgent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { fetchFn } from "@/hooks/api/utils";
import { ApiAgentGet } from "need4deed-sdk";
import { apiPathAgent } from "@/config/constants";

export interface ApiResponse<T> {
message: string;
data: T;
count: number;
}

export const getServerAgent = async (cookieHeader: string, id: string): Promise<ApiAgentGet | null> => {
try {
const urlPath = apiPathAgent.replace("/api/", "");
const response = await fetchFn<ApiResponse<ApiAgentGet>>({
url: `${process.env.URL_API}/${urlPath}/${id}`,
options: {
method: "GET",
headers: { Cookie: cookieHeader },
cache: "no-store",
},
});
return response.data;
} catch (error) {
console.error("Failed to fetch server user role:", error);
return null;
}
};
8 changes: 4 additions & 4 deletions src/hooks/api/getUserRole.ts → src/hooks/api/getUser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fetchFn } from "@/hooks/api/utils";
import { UserRole } from "need4deed-sdk";
import { ApiUserGet } from "need4deed-sdk";
import { apiPathMe } from "@/config/constants";

export interface ApiResponse<T> {
Expand All @@ -8,18 +8,18 @@ export interface ApiResponse<T> {
count: number;
}

export const getServerUserRole = async (cookieHeader: string): Promise<UserRole | null> => {
export const getServerUser = async (cookieHeader: string): Promise<ApiUserGet | null> => {
try {
const urlPath = apiPathMe.replace("/api/", "");
const response = await fetchFn<ApiResponse<{ role: UserRole }>>({
const response = await fetchFn<ApiResponse<ApiUserGet>>({
url: `${process.env.URL_API}/${urlPath}`,
options: {
method: "GET",
headers: { Cookie: cookieHeader },
cache: "no-store",
},
});
return response.data.role;
return response.data;
} catch (error) {
console.error("Failed to fetch server user role:", error);
return null;
Expand Down
Loading