Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved the UI for form & Fixed the Logic #865

Merged
merged 1 commit into from
Aug 14, 2024
Merged
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
138 changes: 78 additions & 60 deletions src/app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,68 +214,86 @@ export default function Courses() {
</Form>
</CardContent>
</Card>
<Card className="mx-auto w-full max-w-6xl overflow-y-auto lg:mt-10">
<Card className="mx-auto w-full max-w-6xl overflow-y-auto lg:my-10">
<CardHeader>
<CardTitle>Allow user another account in cohort 3</CardTitle>
<CardTitle>Discord</CardTitle>
</CardHeader>
<CardContent className="grid gap-4 p-4 pt-0">
<input
type="text"
placeholder="Email"
onChange={(e) => {
setEmail(e.target.value);
}}
></input>
<input
type="text"
placeholder="Admin Password"
onChange={(e) => {
setAdminPassword(e.target.value);
}}
></input>
<button
onClick={() => {
axios.post('/api/admin/discordReset', {
email,
adminPassword,
});
}}
>
Reset
</button>
</CardContent>
</Card>

<Card className="mx-auto w-full max-w-6xl overflow-y-auto lg:mt-10">
<CardHeader>
<CardTitle>Get users discord username in cohort 3</CardTitle>
</CardHeader>
<CardContent className="grid gap-4 p-4 pt-0">
<input
type="text"
placeholder="Email"
onChange={(e) => {
setEmail(e.target.value);
}}
></input>
<input
type="text"
placeholder="Admin Password"
onChange={(e) => {
setAdminPassword(e.target.value);
}}
></input>
<button
onClick={async () => {
const res = await axios.post('/api/admin/discordReset/get', {
email,
adminPassword,
});
alert(JSON.stringify(res.data));
}}
>
Get
</button>
<CardContent className="flex flex-col gap-5 lg:flex-row">
<Card className="mx-auto w-full max-w-3xl overflow-y-auto">
<CardHeader>
<CardTitle>Allow user another account in cohort 3</CardTitle>
</CardHeader>
<CardContent className="grid gap-4 p-4 pt-0">
<Input
type="text"
placeholder="Email"
onChange={(e) => {
setEmail(e.target.value);
}}
></Input>
<Input
type="text"
placeholder="Admin Password"
onChange={(e) => {
setAdminPassword(e.target.value);
}}
></Input>
<Button
onClick={async () => {
try {
const res = await axios.post('/api/admin/discordReset', {
email,
adminPassword,
});
toast(JSON.stringify(res.data.data));
} catch (error) {
toast.error(error.response.data.message);
}
}}
>
Reset
</Button>
</CardContent>
</Card>
<Card className="mx-auto w-full max-w-3xl overflow-y-auto">
<CardHeader>
<CardTitle>Get users discord username in cohort 3</CardTitle>
</CardHeader>
<CardContent className="grid gap-4 p-4 pt-0">
<Input
type="text"
placeholder="Email"
onChange={(e) => {
setEmail(e.target.value);
}}
></Input>
<Input
type="text"
placeholder="Admin Password"
onChange={(e) => {
setAdminPassword(e.target.value);
}}
></Input>
<Button
onClick={async () => {
try {
const res = await axios.post(
'/api/admin/discordReset/get',
{
email,
adminPassword,
},
);
alert(JSON.stringify(res.data));
} catch (error: any) {
toast.error(error.response.data.message);
}
}}
>
Get
</Button>
</CardContent>
</Card>
</CardContent>
</Card>
</div>
Expand Down
72 changes: 43 additions & 29 deletions src/app/api/admin/discordReset/get/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,55 @@ export async function POST(req: NextRequest) {

if (!parseResult.success) {
return NextResponse.json(
{ error: parseResult.error.message },
{ error: parseResult.error.message, message: 'Invalid input data' },
{ status: 400 },
);
}

const { adminPassword, email } = parseResult.data;
try {
const { adminPassword, email } = parseResult.data;

if (adminPassword !== process.env.ADMIN_SECRET) {
return NextResponse.json({}, { status: 401 });
}
if (adminPassword !== process.env.ADMIN_SECRET) {
return NextResponse.json(
{
message: 'Invalid Admin Password',
},
{ status: 401 },
);
}

const user = await db.user.findFirst({
where: {
email,
},
});
const user = await db.user.findFirst({
where: {
email,
},
});

if (!user) {
return NextResponse.json({ msg: 'User not found' }, { status: 404 });
}
if (!user) {
return NextResponse.json({ message: 'User not found' }, { status: 404 });
}

const response = await db.discordConnectBulk.findMany({
where: {
userId: user.id,
},
select: {
discordId: true,
username: true,
},
});

return NextResponse.json(
{ data: response },
{
status: 200,
},
);
const response = await db.discordConnectBulk.findMany({
where: {
userId: user.id,
},
select: {
discordId: true,
username: true,
},
});

return NextResponse.json(
{ data: response },
{
status: 200,
},
);
} catch (error) {
return NextResponse.json(
{
message: 'Internal Server Error',
},
{ status: 500 },
);
}
}
72 changes: 43 additions & 29 deletions src/app/api/admin/discordReset/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,55 @@ export async function POST(req: NextRequest) {

if (!parseResult.success) {
return NextResponse.json(
{ error: parseResult.error.message },
{ error: parseResult.error.message, message: 'Invalid Input data' },
{ status: 400 },
);
}

const { adminPassword, email } = parseResult.data;
try {
const { adminPassword, email } = parseResult.data;

if (adminPassword !== process.env.ADMIN_SECRET) {
return NextResponse.json({}, { status: 401 });
}
if (adminPassword !== process.env.ADMIN_SECRET) {
return NextResponse.json(
{
message: 'Invalid Admin Password',
},
{ status: 401 },
);
}

const user = await db.user.findFirst({
where: {
email,
},
});
const user = await db.user.findFirst({
where: {
email,
},
});

if (!user) {
return NextResponse.json({ msg: 'User not found' }, { status: 404 });
}
if (!user) {
return NextResponse.json({ message: 'User not found' }, { status: 404 });
}

const response = await db.discordConnectBulk.updateMany({
where: {
userId: user.id,
cohortId: '3',
},
data: {
cohortId: Math.random().toString(),
},
});

return NextResponse.json(
{ data: response },
{
status: 200,
},
);
const response = await db.discordConnectBulk.updateMany({
where: {
userId: user.id,
cohortId: '3',
},
data: {
cohortId: Math.random().toString(),
},
});

return NextResponse.json(
{ data: response },
{
status: 200,
},
);
} catch (error) {
return NextResponse.json(
{
message: 'Internal Server Error',
},
{ status: 500 },
);
}
}
3 changes: 0 additions & 3 deletions src/app/error.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
'use client';

import { Button } from '@/components/ui/button';
import { InfoIcon } from 'lucide-react';
import Link from 'next/link';
import { useEffect } from 'react';

export default function ErrorPage({
error,
reset,
}: {
error: Error & { digest?: string };
reset: () => void;
}) {
useEffect(() => {
// Log the error to an error reporting service
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"],
}
Loading