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

React Query Migration #1656

Merged
merged 19 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 3 additions & 9 deletions src/components/admin/services/calendar/Calendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,16 @@ const Calendar = () => {
<>
{event && <Modal event={event} setEvent={setEvent} />}
<ReactBigCalendar
startAccessor="startDate"
endAccessor="endDate"
date={date}
view={view}
className="py-4"
step={15}
events={
tag === "all"
? events
: events.filter(
({ description }) =>
description
.split("\n")[0]
.split("#")
.filter((item) => item !== "")[0]
.trim()
.toLowerCase() === tag,
)
: events.filter((event) => event.category === tag)
}
localizer={mLocalizer}
defaultView="month"
Expand Down
2 changes: 1 addition & 1 deletion src/components/admin/services/calendar/Event.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Event = ({ event, view }) => {
<p className="inline text-xs" data-cy="event">
{view === "month" && (
<>
{new Date(event.start).toLocaleTimeString(navigator.language, {
{new Date(event.startDate).toLocaleTimeString(navigator.language, {
hour: "2-digit",
minute: "2-digit",
})}
Expand Down
4 changes: 2 additions & 2 deletions src/components/admin/services/calendar/Modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ const CalendarModal = ({ event, setEvent }) => {
<div className="border-hackathon-darkgray rounded-b-xl border-x-2 border-b-2 bg-white px-3 py-2">
<div className="flex items-center justify-between">
<div>
{event.start.toLocaleString("default", {
{event.startDate.toLocaleString("default", {
month: "long",
weekday: "long",
day: "2-digit",
year: "numeric",
})}
<br />
{event.start.toLocaleString("default", {
{event.startDate.toLocaleString("default", {
hour: "numeric",
minute: "2-digit",
})}
Expand Down
38 changes: 23 additions & 15 deletions src/components/admin/services/calendar/actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LABELS } from "@/data/admin/Calendar";
import { LABELS, label } from "@/data/admin/Calendar";
import { api } from "@/utils/api";

import { Event } from "@/types/calendar";
const min = new Date(
new Date().getTime() - 20 * 7 * 24 * 60 * 60 * 1000,
).toISOString();
Expand All @@ -20,20 +20,28 @@ export const getEvents = async () => {
url: `https://www.googleapis.com/calendar/v3/calendars/${process.env.NEXT_PUBLIC_GOOGLE_CALENDAR_LEADS}/events?key=${process.env.NEXT_PUBLIC_GOOGLE_CALENDAR_API_KEY}&singleEvents=true&orderBy=startTime&timeMin=${min}&timeMax=${max}`,
});

console.log(leadsResponse, hackathonResponse);

const items = [...hackathonResponse.items, leadsResponse.items];

items.forEach((item: any) => {
item.start = new Date(item.start.dateTime);
item.end = new Date(item.end.dateTime);
const [category, assignee] = item.description
.split("\n")[0]
.split("#")
.map((item: any) => item.trim())
.filter((item: any) => item !== "");
const items = [...hackathonResponse.items, leadsResponse.items][0];
items.forEach((item: Event) => {
item.startDate = new Date(item.start.dateTime);
item.endDate = new Date(item.end.dateTime);
let category: string = "other";
let assignee: string = "";
if (item.description) {
[category, assignee] = item.description
.split("\n")[0]
.split("#")
.map((item: string) => item.trim())
.filter((item: string) => item !== "");
} else {
item.description = "N/A";
}
if (category in LABELS) {
item.color = LABELS[category as keyof label].background;
} else {
category = "other";
item.color = "!bg-hackathon-tags-gray-text";
}
item.category = category;
item.color = LABELS[item.category].background;
item.assignee = assignee;
item.hidden = false;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { api } from "@/utils/api";

import { Event } from "@/types/calendar";
export const getEvents = async () => {
const { items } = await api({
method: "GET",
url: `https://www.googleapis.com/calendar/v3/calendars/${process.env.NEXT_PUBLIC_GOOGLE_CALENDAR}/events?key=${process.env.NEXT_PUBLIC_GOOGLE_CALENDAR_API_KEY}&singleEvents=true&orderBy=startTime`,
});

return items.map((event: any) => ({
return items.map((event: Event) => ({
id: event.id,
name: event.summary,
hidden: false,
Expand Down
95 changes: 82 additions & 13 deletions src/components/admin/services/statistics/actions.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,65 @@
import { api } from "@/utils/api";
import { authenticate } from "@/utils/auth";
import { db } from "@/utils/firebase";
import { collection, doc, getDoc, getDocs } from "firebase/firestore";

type Status = {
pending: number;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it a record with enums

accepted: number;
rejected: number;
};

type Sizes = {
XS?: number;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

u can make this a record with enums

S?: number;
M?: number;
L?: number;
XL?: number;
XXL?: number;
};

interface Stats {
teams: Status | Sizes;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

record with enums

participants: Status | Sizes;
volunteers: Status | Sizes;
judges: Status | Sizes;
mentors: Status | Sizes;
committees: Status | Sizes;
sponsors: Status | Sizes;
panels: Status | Sizes;
admins: Status | Sizes;
}

type Event = {
attendance: number;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

record

name: string;
};

export const getStats = async () => {
const [statistics, events] = await Promise.all([
getDoc(doc(db, "statistics", "statistics")),
getDocs(collection(db, "events")),
]);

const {
teams,
participants,
teams,
volunteers,
judges,
mentors,
committees,
sponsors,
panels,
admins,
} = statistics.data();
} = statistics.data() as Stats;

const attendees = {};
const attendees: Record<string, number> = {};

events.forEach((doc) => {
const { name, attendance } = doc.data();
const { name, attendance } = doc.data() as Event;
attendees[name] = attendance;
});

const users = {
admins,
participants,
teams,
judges,
Expand All @@ -36,23 +68,60 @@ export const getStats = async () => {
committees,
sponsors,
panels,
admins,
};

const sizeData = ["XS", "S", "M", "L", "XL", "XXL"];
const statusData = ["1", "0", "-1"];

const size = {};
const status = {};
const defaultShirts: Sizes = {
XS: 0,
S: 0,
M: 0,
L: 0,
XL: 0,
XXL: 0,
};

const size = {
teams: { ...defaultShirts },
participants: { ...defaultShirts },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cant u just say teams: defaultShirts

volunteers: { ...defaultShirts },
judges: { ...defaultShirts },
mentors: { ...defaultShirts },
committees: { ...defaultShirts },
sponsors: { ...defaultShirts },
panels: { ...defaultShirts },
admins: { ...defaultShirts },
};

const defaultRoles: Status = {
pending: 0,
accepted: 0,
rejected: 0,
};

const status = {
teams: { ...defaultRoles },
participants: { ...defaultRoles },
volunteers: { ...defaultRoles },
judges: { ...defaultRoles },
mentors: { ...defaultRoles },
committees: { ...defaultRoles },
sponsors: { ...defaultRoles },
panels: { ...defaultRoles },
admins: { ...defaultRoles },
};

Object.entries(users).forEach(([group, entries]) => {
size[group] = Object.fromEntries(
const role = group as keyof Stats;

size[role] = Object.fromEntries(
Object.entries(entries).filter(([key]) => sizeData.includes(key)),
);
) as Sizes;

status[group] = Object.fromEntries(
status[role] = Object.fromEntries(
Object.entries(entries).filter(([key]) => statusData.includes(key)),
);
) as Status;
});

return { users: { status, size }, events: attendees };
Expand Down
8 changes: 7 additions & 1 deletion src/data/admin/Calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ interface types {
type: string;
}

interface label {
export interface label {
other: types;
directors: types;
marketing: types;
operations: types;
Expand Down Expand Up @@ -61,6 +62,11 @@ export const LABELS: label = {
background: "!bg-hackathon-tags-red-text",
type: "leads",
},
other: {
color: "gray",
background: "!bg-hackathon-tags-gray-text",
type: "leads",
},
workshop: {
color: "grayblue",
background: "!bg-hackathon-tags-grayblue-text",
Expand Down
19 changes: 19 additions & 0 deletions src/types/calendar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export type Event = {
assignee?: string;
category?: string;
color?: string;
description: string;
end: {
dateTime: string;
timeZone: string;
};
hidden: boolean;
id: string;
start: {
dateTime: string;
timeZone: string;
};
startDate?: Date;
endDate?: Date;
summary: string;
};
Loading