Skip to content

Commit

Permalink
refactor: comment and replace function in repository service
Browse files Browse the repository at this point in the history
  • Loading branch information
SwiichyCode committed Mar 7, 2024
1 parent 59c32f7 commit 80eba83
Show file tree
Hide file tree
Showing 28 changed files with 371 additions and 629 deletions.
60 changes: 21 additions & 39 deletions src/actions/addcomment.action.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,23 @@
"use server";
import { userAction } from "@/lib/next-safe-action";
import repositoryService from "@/services/repository.service";
import { pusherServer } from "@/lib/pusherServer";
import * as z from "zod";
// "use server";
// import { userAction } from "@/lib/next-safe-action";
// import { postRepositoryComment } from "@/services/repository.service";
// import * as z from "zod";

const schema = z.object({
repositoryId: z.number(),
picture: z.string().optional().nullable(),
name: z.string().optional().nullable(),
username: z.string().optional().nullable(),
content: z.string().min(1, "Comment must be at least 1 character long"),
});
// const schema = z.object({
// repositoryId: z.number(),
// picture: z.string().optional().nullable(),
// name: z.string().optional().nullable(),
// username: z.string().optional().nullable(),
// content: z.string().min(1, "Comment must be at least 1 character long"),
// });

export const addComment = userAction(schema, async (data, ctx) => {
try {
await pusherServer.trigger(`repo-${data.repositoryId}`, "new-comment", {
// content: data.content,
// picture: data.picture,
// name: data.name,
// username: data.username,
// userId: ctx.session.user.id,
content: data.content,
createdBy: {
image: data.picture,
name: data.name,
username: data.username,
},
createdAt: new Date(),
userId: ctx.session.user.id,
});

await repositoryService.addCommentToRepository(
data.repositoryId,
data.content,
ctx.session.user.id,
);
} catch (error) {
if (error instanceof Error) return { error: error.message };
}
});
// export const addComment = userAction(schema, async (data, ctx) => {
// try {
// await postRepositoryComment({
// repositoryId: data.repositoryId,
// content: data.content,
// });
// } catch (error) {
// if (error instanceof Error) return { error: error.message };
// }
// });
22 changes: 0 additions & 22 deletions src/actions/addrepository.action.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/actions/admin/hiderepository.action.ts

This file was deleted.

30 changes: 0 additions & 30 deletions src/actions/getRepositories.action.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/actions/syncrepositories.action.ts

This file was deleted.

22 changes: 9 additions & 13 deletions src/app/(app)/repositories/[repositoryId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default async function RepositoryCommentPage({
}: {
params: { repositoryId: number };
}) {
const { repository, comments, session, user } = await useFetchCommentsPage(
const { repository, comments, session } = await useFetchCommentsPage(
Number(params.repositoryId),
);

Expand All @@ -21,31 +21,27 @@ export default async function RepositoryCommentPage({
<div className="space-y-12">
<div className="flex items-center space-x-4">
<ProfileAvatar
pictureUrl={repository.ownerAvatarUrl}
alt={repository.repositoryName}
pictureUrl={repository.data?.ownerAvatarUrl}
alt={repository.data?.repositoryName}
/>
<div className="flex flex-col">
<Link
href={repository.url}
href={repository.data?.url ?? "/"}
className="text-xl font-semibold hover:underline"
target="_blank"
>
{repository?.repositoryName}
{repository.data?.repositoryName}
</Link>
<span className="text-sm">
Published by{" "}
{repository.createdBy.username ?? repository.createdBy.name}
{repository.data?.createdBy.username ??
repository.data?.createdBy.name}
</span>
</div>
</div>

<CommentList comments={comments} />
{session && (
<AddCommentForm
user={user}
repositoryId={Number(params.repositoryId)}
/>
)}
<CommentList comments={comments.data} />
{session && <AddCommentForm repositoryId={Number(params.repositoryId)} />}
</div>
);
}
3 changes: 1 addition & 2 deletions src/app/(app)/repositories/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import { DataSharingAgreementForm } from "@/components/organisms/_forms/dataShar
import { AddRepositoryForm } from "@/components/organisms/_forms/addrepository.form";
import { useQueryParser } from "@/hooks/useQueryParser";
import { QueryParamsProvider } from "@/context/queryParamsContext";
import { ParamsType } from "@/types";

type Props = {
searchParams?: {
query?: string;
language?: string;
params?: ParamsType;
params?: string;
};
};

Expand Down
6 changes: 3 additions & 3 deletions src/app/api/cron/route.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { type NextRequest, NextResponse } from "next/server";
import repositoryService from "@/services/repository.service";
import { env } from "@/env";
import { syncRepositories } from "@/services/repository.service";
import adminService from "@/services/admin.service";
import { env } from "@/env";

export async function GET(request: NextRequest) {
const authHeader = request.headers.get("authorization");
if (authHeader !== `Bearer ${env.CRON_SECRET}`) {
return new Response("Unauthorized", { status: 401 });
}

await repositoryService.syncRepositories();
await syncRepositories();
await adminService.updateCronLastRun();

return NextResponse.json({ ok: "Cron OK" });
Expand Down
8 changes: 0 additions & 8 deletions src/app/socketio/page.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion src/components/molecules/PrefetchLink.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";
import Link from "next/link";
import { useQueryClient } from "@tanstack/react-query";
import { getRepositoriesByFilter } from "@/services/actions/repository.service";
import { getRepositoriesByFilter } from "@/services/repository.service";
import { URL } from "@/constants";

export const PrefetchLink = () => {
Expand Down
1 change: 1 addition & 0 deletions src/components/molecules/SelectParams.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const SelectParams = () => {
const { params, setParams: setParams } = useQueryParams({
key: "params",
});

const handleChange = async (value: string) => {
value === "all" ? await setParams("") : await setParams(value);
};
Expand Down
1 change: 0 additions & 1 deletion src/components/organisms/Comment/CommentCard.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Link from "next/link";
import { ProfileAvatar } from "@/components/molecules/Avatar";
import { calculateCommentCreatedRange } from "@/lib/utils";
import type { Comment } from "@/types/prisma.type";

type Props = {
avatar: string | null;
Expand Down
4 changes: 2 additions & 2 deletions src/components/organisms/Comment/CommentList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { CommentCard } from "@/components/organisms/Comment/CommentCard";
import type { Comment } from "@/types/prisma.type";

type Props = {
comments: Comment[];
comments: Comment[] | undefined;
};

export const CommentList = ({ comments }: Props) => {
return (
<div className=" flex flex-col gap-8">
{comments.map((comment) => (
{comments?.map((comment) => (
<CommentCard
key={comment.id}
avatar={comment.createdBy.image}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { useRepositoriesContext } from "@/context/repositoriesContext";
import { ProfileAvatar } from "@/components/molecules/Avatar";
import { RepositoryCardHideWithRole } from "./RepositoryCardHide";
import type { Repository } from "@/types/prisma.type";
import type { User } from "@/types/prisma.type";

type Props = {
repository: Repository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";
import { X } from "lucide-react";
import { hideRepository } from "@/actions/admin/hiderepository.action";
import { hideRepository } from "@/services/repository.service";
import { withAdminRole } from "@/components/_HOCs/withAdminRole";
import type { Repository } from "@/types/prisma.type";

Expand All @@ -12,7 +12,7 @@ const RepositoryCardHide = ({ repository }: Props) => {
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();

await hideRepository({ id: repository.id });
await hideRepository({ repositoryId: repository.id });
};

return (
Expand Down
4 changes: 2 additions & 2 deletions src/components/organisms/Sidebar/SidebarSyncRepositories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";
import { useTransition } from "react";
import { syncRepositories } from "@/actions/syncrepositories.action";
import { syncRepositories } from "@/services/repository.service";
import { SyncIcon } from "@primer/octicons-react";

export const SidebarSyncRepositories = () => {
Expand All @@ -19,7 +19,7 @@ export const SidebarSyncRepositories = () => {
return (
<form onSubmit={handleSyncRepositories}>
<button
className="hover:bg-subtle-hover flex w-full cursor-pointer items-center space-x-2 rounded-md px-2 py-1 text-sm transition"
className="flex w-full cursor-pointer items-center space-x-2 rounded-md px-2 py-1 text-sm transition hover:bg-subtle-hover"
type="submit"
>
<SyncIcon className="h-4 w-4 text-subtle" />
Expand Down
23 changes: 4 additions & 19 deletions src/components/organisms/_forms/addcomment.form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import { useForm } from "react-hook-form";
import { Form } from "@/components/atoms/form";
import { SubmitButton } from "@/components/molecules/SubmitButton";
import { TextAreaForm } from "@/components/molecules/TextAreaForm";
import { addComment } from "@/actions/addcomment.action";
import { postRepositoryComment } from "@/services/repository.service";
import { formAddCommentSchema } from "./addcomment.schema";
import type { User } from "@/types/prisma.type";
import type * as z from "zod";

type Props = {
user: User | null;
repositoryId: number;
};

export const AddCommentForm = ({ user, repositoryId }: Props) => {
export const AddCommentForm = ({ repositoryId }: Props) => {
const [isPending, startTransition] = useTransition();

const form = useForm<z.infer<typeof formAddCommentSchema>>({
Expand All @@ -28,23 +26,10 @@ export const AddCommentForm = ({ user, repositoryId }: Props) => {

function onSubmit(data: z.infer<typeof formAddCommentSchema>) {
startTransition(async () => {
const payload = {
// repositoryId,
// picture: user?.image,
// name: user?.name,
// username: user?.username,
// content: data.content,
await postRepositoryComment({
repositoryId,
content: data.content,
createdBy: {
image: user?.image,
name: user?.name,
username: user?.username,
},
createdAt: new Date(),
};

await addComment(payload);
});

form.reset();
});
Expand Down
7 changes: 5 additions & 2 deletions src/components/organisms/_forms/addrepository.form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Dialog, DialogContent } from "@/components/atoms/dialog";
import { InputForm } from "@/components/molecules/InputForm";
import { RichTextFieldForm } from "@/components/molecules/RichTextFieldForm";
import { SubmitButton } from "@/components/molecules/SubmitButton";
import { addRepository } from "@/actions/addrepository.action";
import { postRepository } from "@/services/repository.service";
import { useFetchInfiniteRepositories } from "@/hooks/useFetchInfiniteRepositories";
import type * as z from "zod";

Expand All @@ -38,7 +38,10 @@ export const AddRepositoryForm = () => {

function onSubmit(data: z.infer<typeof formRepositorySchema>) {
startTransition(async () => {
const response = await addRepository(data);
const response = await postRepository({
url: data.url,
description: data.description,
});

if (!response.data?.error) {
toast({
Expand Down
Loading

0 comments on commit 80eba83

Please sign in to comment.