Skip to content

Commit

Permalink
🐛 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nrmnqdds committed Oct 6, 2024
1 parent 7023e80 commit af5ac13
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 721 deletions.
29 changes: 17 additions & 12 deletions lib/server/ads-scraper.ts → app/api/ads/route.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
"use server";

import got from "got";
import { NextResponse } from "next/server";
import { parse } from "node-html-parser";

export async function GetAds() {
export async function GET() {
try {
const url = "https://souq.iium.edu.my/embeded";

const response = await got(url, {
https: { rejectUnauthorized: false },
followRedirect: false,
});
const response = await fetch(url);

if (!response.ok) {
throw new Error("Failed to fetch data");
}

const root = parse(response.body);
const body = await response.text();

const root = parse(body);
const articles = root.querySelectorAll(
'div[style*="width:100%; clear:both;height:100px"]',
);
Expand Down Expand Up @@ -42,12 +43,16 @@ export async function GetAds() {
});
}

return {
return NextResponse.json({
success: true,
data: structuredData,
};
});
} catch (error) {
console.error("Error fetching data:", error);
throw new Error("Failed to fetch data");
// throw new Error("Failed to fetch data");
return NextResponse.json(
{ error: "Failed to fetch data" },
{ status: 500 },
);
}
}
3 changes: 3 additions & 0 deletions app/api/login/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export async function POST(request: NextRequest) {

const json = await res.json();

// Clear the cookie
cookies().delete("MOD_AUTH_CAS");

cookies().set("MOD_AUTH_CAS", json.data.token, {
path: "/",
});
Expand Down
7 changes: 4 additions & 3 deletions app/api/profile/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { type NextRequest, NextResponse } from "next/server";
import { parse } from "node-html-parser";

export async function GET(request: NextRequest) {
const params = request.nextUrl.searchParams;

const username = params.get("username");
const username = request.nextUrl.searchParams.get("username");

if (!username) {
return NextResponse.json({ error: "Invalid username" }, { status: 400 });
}

console.log("username: ", username);

try {
const response = await fetch(IMALUUM_HOME_PAGE, {
headers: {
Expand Down Expand Up @@ -45,6 +45,7 @@ export async function GET(request: NextRequest) {
}

const imageURL = `https://smartcard.iium.edu.my/packages/card/printing/camera/uploads/original/${username.trim()}.jpeg`;
console.log("imageURL: ", imageURL);
const name =
hiddenTextSelector.textContent?.trim().replace(/\s+/g, " ") ?? "";

Expand Down
5 changes: 0 additions & 5 deletions components/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import { Input } from "@/components/ui/input";
import useProfile from "@/hooks/useProfile";
import useResult from "@/hooks/useResult";
import useSchedule from "@/hooks/useSchedule";
import { ImaluumLogin } from "@/lib/server/auth";
import { zodResolver } from "@hookform/resolvers/zod";
import { useMutation } from "@tanstack/react-query";
import { useRouter } from "next/navigation";
import type { CSSProperties, HTMLAttributes } from "react";
import { useForm } from "react-hook-form";
import toast from "react-hot-toast";
import * as z from "zod";
Expand All @@ -31,8 +29,6 @@ const formSchema = z.object({
}),
});

type Props = {} & HTMLAttributes<HTMLFormElement> & CSSProperties;

const LoginForm = () => {
const { reset: ProfileReset, setProfile } = useProfile();
const { reset: ScheduleReset } = useSchedule();
Expand Down Expand Up @@ -76,7 +72,6 @@ const LoginForm = () => {
imageURL: "",
});
router.replace("/dashboard");
console.log("reroute");
},
onError: (err) => {
console.log(err);
Expand Down
19 changes: 9 additions & 10 deletions components/ProfileDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import useProfile from "@/hooks/useProfile";
import { ImaluumLogout } from "@/lib/server/auth";
import { useQueryClient } from "@tanstack/react-query";
import { useRouter } from "next/navigation";
import toast from "react-hot-toast";
Expand Down Expand Up @@ -55,15 +54,15 @@ export default function ProfileDropdown() {

<DropdownMenuItem
className="cursor-pointer focus:bg-red-600/50"
onClick={async () => {
const res = await ImaluumLogout();
if (res.success) {
localStorage.clear();
queryClient.invalidateQueries();
toast.success("Logged out successfully.");
router.replace("/");
}
}}
// onClick={async () => {
// const res = await ImaluumLogout();
// if (res.success) {
// localStorage.clear();
// queryClient.invalidateQueries();
// toast.success("Logged out successfully.");
// router.replace("/");
// }
// }}
>
Log out
</DropdownMenuItem>
Expand Down
14 changes: 12 additions & 2 deletions components/dashboard/Advertisements.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use client";

import { GetAds } from "@/lib/server/ads-scraper";
import { useQuery } from "@tanstack/react-query";
import Link from "next/link";
import { AiOutlineLink } from "react-icons/ai";
Expand All @@ -11,7 +10,18 @@ const Advertisement = ({ className }: { className: string }) => {
const { data: ads, isLoading: loading } = useQuery({
queryKey: ["ads"],
queryFn: async () => {
const data = await GetAds();
// const data = await GetAds();
// if (data.success) {
// return data.data;
// }

const res = await fetch("/api/ads");
if (!res.ok) {
throw new Error("Failed to fetch data");
}

const data = await res.json();

if (data.success) {
return data.data;
}
Expand Down
5 changes: 0 additions & 5 deletions context/ImaluumProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import LoadingScreen from "@/components/loading-screen";
import useProfile from "@/hooks/useProfile";
import useResult from "@/hooks/useResult";
import useSchedule from "@/hooks/useSchedule";
import { GetUserProfile } from "@/lib/server/profile-scraper";
import { GetResult } from "@/lib/server/result-scraper";
import { GetSchedule } from "@/lib/server/schedule-scraper";
import { useQuery } from "@tanstack/react-query";

const ImaluumProvider = ({ children }: { children: React.ReactNode }) => {
Expand All @@ -17,8 +14,6 @@ const ImaluumProvider = ({ children }: { children: React.ReactNode }) => {
const profileData = useQuery({
queryKey: ["profile"],
queryFn: async () => {
// const data = await GetUserProfile(profile?.matricNo as string);

const res = await fetch(`/api/profile?username=${profile?.matricNo}`);

if (!res.ok) {
Expand Down
113 changes: 0 additions & 113 deletions lib/server/auth.ts

This file was deleted.

55 changes: 0 additions & 55 deletions lib/server/profile-scraper.ts

This file was deleted.

Loading

0 comments on commit af5ac13

Please sign in to comment.