Skip to content

Commit

Permalink
trying to fix build error issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mokelgit committed Sep 11, 2024
1 parent 0c2431d commit e5324c0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 49 deletions.
98 changes: 51 additions & 47 deletions app/api/notifications/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Notification from "@/components/Notification";
import { IS_DEVELOPMENT, IS_PREVIEW } from "@/lib/helpers";
import moment from "moment";

const notificationTable = "tblA4NwUahsIldb6x";
const baseId = "appZWDvjvDmVnOici";

const CACHE_TTL_SECONDS = 300; // 5 minutes

export type Notification = {
export type NotificationType = {
id: string;
displayPages: string;
body: string;
Expand All @@ -33,65 +33,69 @@ async function fetchData() {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.AIRTABLE_API_KEY}`,
Authorization: `Bearer ${process.env.AIRTABLE_API_KEY || ""}`,
},
next: { revalidate: CACHE_TTL_SECONDS },
});

// as records
const data = (await response.json()).records.map((record: any) => {
return {
id: record.id,
get: (field: string) => record.fields[field],
};
});
const jsonResponse = await response.json();

const records = Array.isArray(jsonResponse?.records)
? jsonResponse.records
: [];

// filter out records that are not enabled or not in the branches to include and map them to the Notification type
const records: Notification[] = data
.filter((record) => {
return (
BranchesToInclude.includes(record.get("Branch") as string) &&
record.get("Status") === "Enabled"
);
})
.map((record) => {
return {
id: record.id,
displayPages: record.get("Display Page") as string,
body: record.get("Body") as string,
desc: record.get("Head") as string,
url: record.get("URL") as string,
icon: record.get("Icon") as string,
backgroundColor: record.get("Color") as string,
textColor: record.get("Text Color") as string,
startTimestamp: moment
return records
.map((record: any) => ({
id: record?.id || "",
displayPages: record?.fields?.["Display Page"] || "",
body: record?.fields?.["Body"] || "",
desc: record?.fields?.["Head"] || "",
url: record?.fields?.["URL"] || "",
icon: record?.fields?.["Icon"] || "",
backgroundColor: record?.fields?.["Color"] || "",
textColor: record?.fields?.["Text Color"] || "",
startTimestamp:
moment
.utc(
`${record.get("Start Date") as string}T${
record.get("Start Time") as string
`${record?.fields?.["Start Date"] || ""}T${
record?.fields?.["Start Time"] || ""
}Z`,
)
.valueOf(),
endTimestamp: moment
.valueOf() || 0,
endTimestamp:
moment
.utc(
`${record.get("End Date") as string}T${
record.get("End Time") as string
`${record?.fields?.["End Date"] || ""}T${
record?.fields?.["End Time"] || ""
}Z`,
)
.valueOf(),
branch: record.get("Branch") as string,
};
});

return records;
.valueOf() || 0,
branch: record?.fields?.["Branch"] || "",
}))
.filter(
(notification: NotificationType) =>
BranchesToInclude.includes(notification.branch) &&
notification.displayPages &&
notification.body &&
notification.startTimestamp &&
notification.endTimestamp,
);
} catch (error) {
console.error("Error fetching data:", error);
throw error;
console.error("Error fetching notifications:", error);
return [];
}
}

export async function GET() {
const result = await fetchData();
return new Response(JSON.stringify(result), {
headers: { "content-type": "application/json" },
});
try {
const result = await fetchData();
return new Response(JSON.stringify(result), {
headers: { "content-type": "application/json" },
});
} catch (error) {
console.error("Error in GET function:", error);
return new Response(JSON.stringify([]), {
headers: { "content-type": "application/json" },
});
}
}
4 changes: 2 additions & 2 deletions components/Notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { BASE_URL } from "@/lib/helpers";
import { useTheme } from "next-themes";
import { track } from "@vercel/analytics";
import useSWR from "swr";
import type { Notification } from "@/app/api/notifications/route";
import type { NotificationType } from "@/app/api/notifications/route";
import { useElementSizeObserver } from "@/hooks/useElementSizeObserver";

const currentDateTime = new Date().getTime();
Expand All @@ -22,7 +22,7 @@ const Notification = () => {
const { theme } = useTheme();
const currentPath = usePathname();
const [seenNotifications, setSeenNotifications] = useLocalStorage<
Notification[]
NotificationType[]
>("seenNotifications", []);

const { data, isLoading, isValidating, error } = useSWR(
Expand Down

0 comments on commit e5324c0

Please sign in to comment.