Skip to content

Commit

Permalink
fix: shifted the logic to server side
Browse files Browse the repository at this point in the history
  • Loading branch information
amanbairagi89 committed Jun 16, 2024
1 parent 9df282a commit 35383a0
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 82 deletions.
71 changes: 70 additions & 1 deletion src/app/api/admin/content/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
import db from '@/db';
import axios from 'axios';
import { NextRequest, NextResponse } from 'next/server';

interface DiscordData {
type: string;
thumbnail: string;
title: string;
courseTitle: string;
courseId: number;
currFolderId: number;
mediaId: number;
}

const sendUpdateToDiscord = async (data: DiscordData) => {
const body = {
content: 'Hello @everyone',
tts: false,
color: 'white',
embeds: [
{
title: `New ${data?.type === 'notion' ? 'NOTE' : data?.type?.toUpperCase()}`,
description: `${data?.title} has been added in the ${data?.courseTitle} , [Click here to visit this ${data?.type === 'notion' ? 'note' : data?.type}](https://app.100xdevs.com/courses/${data.courseId}/${data.currFolderId}/${data.mediaId})`,
},
],
};

try {
await axios.post(
process.env.NEXT_PUBLIC_DISCORD_WEBHOOK_URL as string,
body,
);
} catch (error) {
console.error('Failed to send update to Discord:', error);
}
};

export const POST = async (req: NextRequest) => {
const {
type,
Expand All @@ -10,6 +44,9 @@ export const POST = async (req: NextRequest) => {
parentContentId,
metadata,
adminPassword,
courseTitle,
rest,
checked,
}: {
type: 'video' | 'folder' | 'notion';
thumbnail: string;
Expand All @@ -18,6 +55,9 @@ export const POST = async (req: NextRequest) => {
parentContentId: number;
metadata: any;
adminPassword: string;
courseTitle: string;
rest: string[];
checked: boolean;
} = await req.json();

if (adminPassword !== process.env.ADMIN_SECRET) {
Expand Down Expand Up @@ -103,5 +143,34 @@ export const POST = async (req: NextRequest) => {
});
}
}
return NextResponse.json({ id: content.id }, { status: 200 });

if (checked && (type === 'notion' || type === 'video')) {
if (!process.env.NEXT_PUBLIC_DISCORD_WEBHOOK_URL) {
return NextResponse.json(
{ message: 'Environment variable for discord webhook is not set' },
{ status: 500 },
);
}
const data: DiscordData = {
type,
thumbnail:
'https://d2szwvl7yo497w.cloudfront.net/courseThumbnails/video.png',
title,
courseTitle,
courseId,
currFolderId: parseInt(rest[0], 10),
mediaId: content.id,
};
await sendUpdateToDiscord(data);
}

return NextResponse.json(
{
message:
checked && (type === 'notion' || type === 'video')
? 'Content Added and Discord notification has been sent'
: 'Content has been added',
},
{ status: 200 },
);
};
40 changes: 0 additions & 40 deletions src/app/services/DiscordService.tsx

This file was deleted.

47 changes: 11 additions & 36 deletions src/components/admin/AddContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { AddNotionMetadata } from './AddNotionMetadata';
import { Input } from '../ui/input';
import { useRecoilState } from 'recoil';
import { loader } from '@/store/atoms/loader';
import DiscordService from '@/app/services/DiscordService';
import { Checkbox } from '../ui/checkbox';
import { toast } from 'sonner';

export const AddContent = ({
rest,
Expand All @@ -26,22 +26,6 @@ export const AddContent = ({
const [adminPassword, setAdminPassword] = useState('');
const [loading, setLoading] = useRecoilState(loader);

const { sendUpdateToDiscord } = DiscordService();

interface DiscordData {
type: string;
thumbnail: string;
title: string;
courseTitle: string;
courseId: number;
currFolderId: number;
mediaId: number;
}

const postOnDiscord = (data: DiscordData) => {
sendUpdateToDiscord(data);
};

const handleContentSubmit = async () => {
setLoading(true);
const response = await fetch('/api/admin/content', {
Expand All @@ -54,34 +38,25 @@ export const AddContent = ({
parentContentId,
metadata,
adminPassword,
courseTitle,
rest,
checked,
}),
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
});
setLoading(false);
const responseData = await response.json();
console.log(responseData);

const respData: { id: number } = await response.json();

const data = {
type,
thumbnail:
'https://d2szwvl7yo497w.cloudfront.net/courseThumbnails/video.png',
title,
courseTitle,
courseId,
currFolderId: parseInt(rest[0], 10),
mediaId: respData.id,
};

if (checked && response.status === 200) {
if (type === 'notion' || type === 'video') {
postOnDiscord(data);
setLoading(false);
}
if (response.status === 200) {
// handle success if needed
toast.success(responseData.message);
} else {
setLoading(false);
// handle error if needed
toast.error(responseData.message || 'Something went wrong');
}
};

Expand Down
10 changes: 5 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
"incremental": true,
"plugins": [
{
"name": "next",
},
"name": "next"
}
],
"paths": {
"@/*": ["./src/*"],
"@public/*": ["./public/*"],
},
"@public/*": ["./public/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"],
"exclude": ["node_modules"]
}

0 comments on commit 35383a0

Please sign in to comment.