Skip to content

Commit

Permalink
fix: fixing the forum date extract logic (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonNotJson authored Oct 7, 2023
1 parent 9d69e68 commit 905f971
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 41 deletions.
14 changes: 6 additions & 8 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ jobs:
if: github.event.release.prerelease == true
strategy:
matrix:
microapps:
[
microapps: [
"WEBHOOK_ROOT_DEV",
"WEBHOOK_SYLLABUS_DEV",
"WEBHOOK_CAMPUS_DEV",
"WEBHOOK_FEEDS_DEV",
# "WEBHOOK_CAMPUS_DEV",
# "WEBHOOK_FEEDS_DEV",
"WEBHOOK_FORUMS_DEV",
]
env:
Expand All @@ -30,12 +29,11 @@ jobs:
if: github.event.release.prerelease != true
strategy:
matrix:
microapps:
[
microapps: [
"WEBHOOK_ROOT_PROD",
"WEBHOOK_SYLLABUS_PROD",
"WEBHOOK_CAMPUS_PROD",
"WEBHOOK_FEEDS_PROD",
# "WEBHOOK_CAMPUS_PROD",
# "WEBHOOK_FEEDS_PROD",
"WEBHOOK_FORUMS_PROD",
]
env:
Expand Down
15 changes: 15 additions & 0 deletions apps/forum/src/utils/getDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,18 @@ export const getCurrentDateInJST = () => {

return `${YYYY}${MM}${DD}${HH}${mm}${SS}`;
};

export const extractDate = (fullDate: string) => {
return fullDate.substring(0, 8) + "000000"; // Extracts the first 8 characters (YYYYMMDD)
};

export const getCurrentDateInUTC = () => {
const date = new Date();
const YYYY = date.getUTCFullYear();
const MM = String(date.getUTCMonth() + 1).padStart(2, "0");
const DD = String(date.getUTCDate()).padStart(2, "0");
const HH = String(date.getUTCHours()).padStart(2, "0");
const mm = String(date.getUTCMinutes()).padStart(2, "0");
const SS = String(date.getUTCSeconds()).padStart(2, "0");
return `${YYYY}${MM}${DD}${HH}${mm}${SS}`;
};
14 changes: 7 additions & 7 deletions apps/forum/src/utils/storeDate.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { getCurrentDateInJST } from "./getDate";
import { getCurrentDateInUTC } from "./getDate";

export const storeDate = () => {
const storedDateInJST = localStorage.getItem("lastCheckedDateJST");
const currentDateInJST = getCurrentDateInJST();
const storedDateInUTC = localStorage.getItem("lastCheckedDateUTC");
const currentDateInUTC = getCurrentDateInUTC();

if (!storedDateInJST) {
if (!storedDateInUTC) {
// If there's no stored date, set the current date to local storage.
localStorage.setItem("lastCheckedDateJST", currentDateInJST);
} else if (storedDateInJST !== currentDateInJST) {
localStorage.setItem("lastCheckedDateUTC", currentDateInUTC);
} else if (storedDateInUTC !== currentDateInUTC) {
// If the stored date and the current date are different, update the stored date.
localStorage.setItem("lastCheckedDateJST", currentDateInJST);
localStorage.setItem("lastCheckedDateUTC", currentDateInUTC);
}
};
22 changes: 10 additions & 12 deletions apps/root/src/components/frame/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ import {
TimetableIconHovered,
} from "@app/components/icons/TimetableIcon"
import { ThemeContext, ThemeProvider } from "@app/utils/theme-context"
import {
getCurrentDateInJST,
getCurrentDateInUTC,
extractDate,
} from "@app/utils/getDate"
import { getCurrentDateInUTC, extractDate } from "@app/utils/getDate"
import { shouldCallApi } from "@app/utils/shouldCallApi"
import { fetchNotificaiton } from "@app/utils/fetchNotification"

Expand Down Expand Up @@ -71,15 +67,17 @@ const Nav = () => {

const fetchNotificationAndUpdateState = async () => {
try {
const storedDateInJST = localStorage.getItem("lastCheckedDateJST")
const currentDateInJST = getCurrentDateInJST()
const storedDateOnly = extractDate(storedDateInJST || "")
const currentDateOnly = extractDate(currentDateInJST)
const storedDateInUTC = localStorage.getItem("lastCheckedDateUTC")
const currentDateInUTC = getCurrentDateInUTC()
const storedDateOnly = extractDate(storedDateInUTC || "")
const currentDateOnly = extractDate(currentDateInUTC)

if (!storedDateOnly) {
localStorage.setItem("lastCheckedDateJST", currentDateInJST)
localStorage.setItem("lastCheckedDateUTC", currentDateInUTC)
} else if (shouldCallApi()) {
const newPostsCount = await fetchNotificaiton(storedDateInJST || "")
const newPostsCount = await fetchNotificaiton(currentDateOnly || "")
// const newPostsCount = ""

const updatedNavItems = navItems.map((item) =>
item.path === "/forum"
? {
Expand All @@ -92,7 +90,7 @@ const Nav = () => {
: item
)
setNavItems(updatedNavItems)
localStorage.setItem("lastCheckedDateJST", currentDateInJST)
localStorage.setItem("lastCheckedDateUTC", currentDateInUTC)
localStorage.setItem(
"lastApiCallTimestamp",
new Date().getTime().toString()
Expand Down
28 changes: 14 additions & 14 deletions apps/root/src/utils/getDate.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
export const getCurrentDateInJST = () => {
const date = new Date()
const jstOffset = 9 * 60 // JST is UTC+9
const localOffset = date.getTimezoneOffset()
date.setMinutes(date.getMinutes() + localOffset + jstOffset)
// export const getCurrentDateInJST = () => {
// const date = new Date()
// const jstOffset = 9 * 60 // JST is UTC+9
// const localOffset = date.getTimezoneOffset()
// date.setMinutes(date.getMinutes() + localOffset + jstOffset)

const YYYY = date.getFullYear()
const MM = String(date.getMonth() + 1).padStart(2, "0") // Months are 0-based
const DD = String(date.getDate()).padStart(2, "0")
const HH = String(date.getHours()).padStart(2, "0")
const mm = String(date.getMinutes()).padStart(2, "0")
const SS = String(date.getSeconds()).padStart(2, "0")
// const YYYY = date.getFullYear()
// const MM = String(date.getMonth() + 1).padStart(2, "0") // Months are 0-based
// const DD = String(date.getDate()).padStart(2, "0")
// const HH = String(date.getHours()).padStart(2, "0")
// const mm = String(date.getMinutes()).padStart(2, "0")
// const SS = String(date.getSeconds()).padStart(2, "0")

return `${YYYY}${MM}${DD}${HH}${mm}${SS}`
}
// return `${YYYY}${MM}${DD}${HH}${mm}${SS}`
// }

export const extractDate = (fullDate: string) => {
return fullDate.substring(0, 8) // Extracts the first 8 characters (YYYYMMDD)
return fullDate.substring(0, 8) + "000000" // Extracts the first 8 characters (YYYYMMDD)
}

export const getCurrentDateInUTC = () => {
Expand Down
1 change: 1 addition & 0 deletions apps/root/src/utils/shouldCallApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const LAST_API_CALL_TIMESTAMP = "lastApiCallTimestamp"
// const ONE_HOUR_IN_MS = 1
const ONE_HOUR_IN_MS = 60 * 60 * 1000

const convertToTimestamp = (datetime: string) => {
Expand Down

0 comments on commit 905f971

Please sign in to comment.