From 8da743e939991dacd6208096e5eda86d33d46871 Mon Sep 17 00:00:00 2001
From: Sulagna Ghosh <62666287+suzy-g38@users.noreply.github.com>
Date: Tue, 1 Aug 2023 00:35:37 +0530
Subject: [PATCH] feat: dashboard and data fetch for events (#553)
---
.../Dashboard/DashboardEventCard.tsx | 67 ++++++++++++++
src/components/Dashboard/Events.tsx | 92 +++++++++++++++++++
src/lib/types.ts | 10 ++
src/pages/api/events/allevents.ts | 26 ++++++
src/pages/dashboard/index.tsx | 28 +++++-
5 files changed, 222 insertions(+), 1 deletion(-)
create mode 100644 src/components/Dashboard/DashboardEventCard.tsx
create mode 100644 src/components/Dashboard/Events.tsx
create mode 100644 src/pages/api/events/allevents.ts
diff --git a/src/components/Dashboard/DashboardEventCard.tsx b/src/components/Dashboard/DashboardEventCard.tsx
new file mode 100644
index 00000000..735361d4
--- /dev/null
+++ b/src/components/Dashboard/DashboardEventCard.tsx
@@ -0,0 +1,67 @@
+import { DashboardEventType } from '@/lib/types';
+
+import Image from 'next/image';
+
+const DashboardEventCard = ({
+ _id,
+ name,
+ address,
+ image,
+ date,
+ tags,
+ logo,
+}: DashboardEventType) => {
+ return (
+
+
+
+
+
+
+
+ {logo && (
+
+ )}
+
{name}
+
+
+ {new Date(date).toLocaleString('en-IN', {
+ day: 'numeric',
+ month: 'long',
+ year: 'numeric',
+ })}
+
+
+ {!address.isOnline && address.location}
+
+
+
+
+ {tags.map((tag: string, index: number) => (
+ -
+ {tag}
+
+ ))}
+
+
+
+
+
+ );
+};
+
+export default DashboardEventCard;
diff --git a/src/components/Dashboard/Events.tsx b/src/components/Dashboard/Events.tsx
new file mode 100644
index 00000000..1b516f11
--- /dev/null
+++ b/src/components/Dashboard/Events.tsx
@@ -0,0 +1,92 @@
+import { DashboardEventType } from '@/lib/types';
+
+import { Heading } from '@/components';
+import Button from '@/components/Buttons/Button';
+import DashboardEventCard from '@/components/Dashboard/DashboardEventCard';
+
+import 'react-toastify/dist/ReactToastify.css';
+import { useEffect, useState } from 'react';
+import React from 'react';
+import { toast, ToastContainer } from 'react-toastify';
+
+const DashboadEventPage = () => {
+ const [allEventsData, setAllEventsData] = useState([]);
+ const [initialLoading, setInitialLoading] = useState(true);
+
+ const getData = async () => {
+ setInitialLoading(true);
+ try {
+ const res = await fetch(`/api/events/allevents`).then((response) =>
+ response.json()
+ );
+ // console.log(res)
+ if (res.success) {
+ setAllEventsData(res.events);
+ setInitialLoading(false);
+ }
+ } catch (error) {
+ toast.success('Something went wrong. Please Try Again', {
+ position: 'top-right',
+ autoClose: 1000,
+ hideProgressBar: false,
+ closeOnClick: true,
+ pauseOnHover: true,
+ draggable: true,
+ progress: undefined,
+ closeButton: false,
+ });
+ }
+ };
+
+ useEffect(() => {
+ getData();
+ }, []);
+
+ return (
+
+
+ {initialLoading ? (
+ <>loading>
+ ) : (
+
+
+ {' '}
+
+
+
+
+
+ {allEventsData.length > 0 ? (
+
+ {allEventsData.map(
+ (event: DashboardEventType, index: number) => (
+
+ )
+ )}
+
+ ) : (
+
+
+ No events created
+
+
+ )}
+
+
+ )}
+
+ );
+};
+
+export default DashboadEventPage;
diff --git a/src/lib/types.ts b/src/lib/types.ts
index 7f405d5f..d516480e 100644
--- a/src/lib/types.ts
+++ b/src/lib/types.ts
@@ -91,6 +91,16 @@ export type EventDataType = {
logo?: string;
};
+export type DashboardEventType = {
+ _id: string;
+ name: string;
+ address: { isOnline: boolean; location: string };
+ date: Date;
+ tags: string[];
+ link: string;
+ image: string;
+ logo?: string;
+};
export type NewsLetterFormType = {
email: string;
};
diff --git a/src/pages/api/events/allevents.ts b/src/pages/api/events/allevents.ts
new file mode 100644
index 00000000..f6d1fdd9
--- /dev/null
+++ b/src/pages/api/events/allevents.ts
@@ -0,0 +1,26 @@
+import type { NextApiRequest, NextApiResponse } from 'next';
+
+export default async function handler(
+ req: NextApiRequest,
+ res: NextApiResponse
+) {
+ try {
+ const response = await fetch(
+ `${process.env.NEXT_PUBLIC_API_ENDPOINT}/api/events`,
+ {
+ method: 'get',
+ headers: {
+ 'Content-type': 'application/json',
+ },
+ }
+ ).then((resp) => {
+ if (resp.status === 500) {
+ throw 'Some error occurrend';
+ }
+ return resp.json();
+ });
+ res.status(200).json(response);
+ } catch (error) {
+ res.status(500).json({ success: false, error: error });
+ }
+}
diff --git a/src/pages/dashboard/index.tsx b/src/pages/dashboard/index.tsx
index d5754e2e..7f71ce50 100644
--- a/src/pages/dashboard/index.tsx
+++ b/src/pages/dashboard/index.tsx
@@ -1,11 +1,16 @@
import { SettingsContext } from '@/lib/context/settings';
+import DashboadEventPage from '@/components/Dashboard/Events';
+
import Image from 'next/image';
import { useContext } from 'react';
+import { useState } from 'react';
+import React from 'react';
import { ToastContainer } from 'react-toastify';
const Dashboard = () => {
const { theme } = useContext(SettingsContext);
+ const [selectedOption, setSelectedOption] = useState('');
return (