1- import { ClashesFC } from "@/dto/Clashes" ;
1+ import { ClashesFC , Event , OffCampus , OnCampus } from "@/dto/Clashes" ;
22import React , { useState } from "react" ;
33import Modal from "@mui/material/Modal" ;
44import TableComponent from "@/components/NewTableComponent/Table" ;
55import generateColumns from "@/components/NewTableComponent/ColumnMapping" ;
66import { Button } from "@/components/ui/button" ;
77import { EventModal , OffCampusModal , OnCampusModal } from "./ClashModal" ;
88
9+ type ClashDateFields = {
10+ startDateTime ?: string ;
11+ cstartDateTime ?: string ;
12+ endDateTime ?: string ;
13+ cendDateTime ?: string ;
14+ } ;
15+
16+ type ClashModalType = "event" | "onCampus" | "offCampus" ;
17+ type ClashModalData = Event | OnCampus | OffCampus ;
18+
919const eventColumns = [
1020 {
1121 viewMore : "View" ,
@@ -25,21 +35,37 @@ const eventColumns = [
2535] ;
2636
2737const Clashes = ( { clashes } : { clashes : ClashesFC } ) => {
28- const [ selectedClash , setSelectedClash ] = useState < any > ( null ) ;
29- const [ modalType , setModalType ] = useState < string > ( "" ) ;
38+ const [ selectedClash , setSelectedClash ] = useState < ClashModalData | null > (
39+ null ,
40+ ) ;
41+ const [ modalType , setModalType ] = useState < ClashModalType | "" > ( "" ) ;
42+
43+ const toDisplayDate = ( value ?: string ) => {
44+ if ( ! value ) return "N/A" ;
3045
31- const formatClashes = ( clashesEvent : any , modalType : string ) => {
32- return clashesEvent . map ( ( clash ) => ( {
46+ const parsedDate = new Date ( value ) ;
47+ return Number . isNaN ( parsedDate . getTime ( ) )
48+ ? "N/A"
49+ : parsedDate . toLocaleString ( ) ;
50+ } ;
51+
52+ const formatClashes = < T extends ClashModalData & ClashDateFields > (
53+ clashesEvent : T [ ] ,
54+ currentModalType : ClashModalType ,
55+ ) => {
56+ const safeClashes = Array . isArray ( clashesEvent ) ? clashesEvent : [ ] ;
57+
58+ return safeClashes . map ( ( clash ) => ( {
3359 ...clash ,
34- startDateTime : new Date ( clash . startDateTime ) . toLocaleString ( ) ,
35- cstartDateTime : new Date ( clash . cstartDateTime ) . toLocaleString ( ) ,
36- endDateTime : new Date ( clash . endDateTime ) . toLocaleString ( ) ,
37- cendDateTime : new Date ( clash . cendDateTime ) . toLocaleString ( ) ,
60+ startDateTime : toDisplayDate ( clash . startDateTime ) ,
61+ cstartDateTime : toDisplayDate ( clash . cstartDateTime ) ,
62+ endDateTime : toDisplayDate ( clash . endDateTime ) ,
63+ cendDateTime : toDisplayDate ( clash . cendDateTime ) ,
3864 viewMore : (
3965 < Button
4066 onClick = { ( ) => {
4167 setSelectedClash ( clash ) ;
42- setModalType ( modalType ) ;
68+ setModalType ( currentModalType ) ;
4369 } }
4470 >
4571 View More
@@ -48,10 +74,6 @@ const Clashes = ({ clashes }: { clashes: ClashesFC }) => {
4874 } ) ) ;
4975 } ;
5076
51- const onViewClash = async ( event : any ) => {
52- setSelectedClash ( event ) ;
53- } ;
54-
5577 const onCloseModal = ( ) => {
5678 setSelectedClash ( null ) ;
5779 } ;
@@ -65,18 +87,22 @@ const Clashes = ({ clashes }: { clashes: ClashesFC }) => {
6587 return (
6688 < div className = "bg-white p-4 px-8 rounded-lg border-gray-300 hover:border-blue-200 border-2" >
6789 < Modal
68- open = { selectedClash }
90+ open = { Boolean ( selectedClash ) }
6991 onClose = { onCloseModal }
7092 aria-labelledby = "Event Details"
7193 aria-describedby = "Event Details"
7294 className = "flex justify-center items-center"
7395 >
7496 < >
75- { modalType === "event" && < EventModal event = { selectedClash } /> }
76- { modalType === "onCampus" && (
97+ { modalType === "event" &&
98+ selectedClash &&
99+ "ceventId" in selectedClash && (
100+ < EventModal event = { selectedClash } />
101+ ) }
102+ { modalType === "onCampus" && selectedClash && "baseSalary" in selectedClash && (
77103 < OnCampusModal onCampusEvent = { selectedClash } />
78104 ) }
79- { modalType === "offCampus" && (
105+ { modalType === "offCampus" && selectedClash && "salary" in selectedClash && (
80106 < OffCampusModal offCampusEvent = { selectedClash } />
81107 ) }
82108 </ >
0 commit comments