Skip to content

Commit

Permalink
feat: implement HOC patern for admin components
Browse files Browse the repository at this point in the history
  • Loading branch information
SwiichyCode committed Feb 26, 2024
1 parent a30c551 commit df243b6
Show file tree
Hide file tree
Showing 14 changed files with 404 additions and 18 deletions.
236 changes: 236 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"db:push": "prisma db push",
"db:studio": "prisma studio",
"dev": "next dev",
"node": "node server.js",
"postinstall": "prisma generate",
"lint": "next lint",
"start": "next start",
Expand Down Expand Up @@ -53,6 +54,8 @@
"react-hook-form": "^7.50.1",
"react-intersection-observer": "^9.8.1",
"react-query": "^3.39.3",
"socket.io": "^4.7.4",
"socket.io-client": "^4.7.4",
"tailwind-merge": "^2.2.1",
"tailwindcss-animate": "^1.0.7",
"ts-jest": "^29.1.2",
Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions src/actions/updateuserrole.action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use server";
import { revalidatePath } from "next/cache";
import { adminAction } from "@/lib/next-safe-action";
import adminService from "@/services/admin.service";
import * as z from "zod";

const schema = z.object({
userId: z.string(),
});

export const updateUserRole = adminAction(schema, async (data) => {
try {
await adminService.updateUserRole(data.userId);
} catch (error) {
if (error instanceof Error) return { error: error.message };
}

revalidatePath("/repositories");
});
23 changes: 23 additions & 0 deletions src/app/socketio/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use client";
import React, { useEffect, useState } from "react";
import io from "socket.io-client";

const API_URL = "https://nodejs-production-eb8a.up.railway.app/";
const socket = io(API_URL, { autoConnect: false });

export default function SocketIOPage() {
const [views, setViews] = useState(0);

useEffect(() => {
socket.connect();
socket.on("count", (count: number) => {
setViews(count);
});

return () => {
socket.disconnect();
};
}, []);

return <div>{views}</div>;
}
16 changes: 16 additions & 0 deletions src/components/_HOC/withAdminRole.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { User } from "@/types/prisma.type";

export const withAdminRole = <P extends object>(
Component: React.ComponentType<P>,
) => {
const WithAdminRole = ({
role,
...props
}: P & { role: User["role"] | undefined }) => {
return role === "ADMIN" ? <Component {...(props as P)} /> : null;
};

WithAdminRole.displayName = `WithAdminRole(${Component.displayName ?? Component.name ?? "Component"})`;

return WithAdminRole;
};
Loading

0 comments on commit df243b6

Please sign in to comment.