|
| 1 | +"use client"; |
| 2 | +import { fetchAllSeasons, fetchOnCampusOffers } from "@/helpers/api"; |
| 3 | +import generateColumns from "@/components/NewTableComponent/ColumnMapping"; |
| 4 | +import { onCampusOfferDTO } from "@/dto/onCampusOfferDTO"; |
| 5 | +import Table from "@/components/NewTableComponent/Table"; |
| 6 | +import { useEffect, useState } from "react"; |
| 7 | +import Loader from "@/components/Loader/loader"; |
| 8 | +import toast from "react-hot-toast"; |
| 9 | +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; |
| 10 | + |
| 11 | +const internHiddenColumns = ["salary.firstYearCTC", "salary.totalCTC", "salary.salaryPeriod"]; |
| 12 | +const placementHiddenColumns = ["salary.stipend", "salary.otherCompensations", "salary.salaryPeriod"]; |
| 13 | + |
| 14 | +const OnCampusOffersPage = () => { |
| 15 | + const columns = generateColumns(onCampusOfferDTO); |
| 16 | + const [seasons, setSeasons] = useState<{ id: string; name: string, type: string }[]>([]); |
| 17 | + const [selectedSeason, setSelectedSeason] = useState<string>(""); |
| 18 | + const [loading, setLoading] = useState(true); |
| 19 | + const [allOffers, setAllOffers] = useState(); |
| 20 | + const [visibleColumns, setVisibleColumns] = useState<any[]>([]); |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + const getData = async () => { |
| 24 | + try { |
| 25 | + const data = await fetchAllSeasons(); |
| 26 | + if (Array.isArray(data)) { |
| 27 | + setSeasons( |
| 28 | + data.map((s: any) => ({ |
| 29 | + id: s.id, |
| 30 | + name: `${s.type} - ${s.year}`, |
| 31 | + type: s.type, |
| 32 | + })), |
| 33 | + ); |
| 34 | + } |
| 35 | + setSelectedSeason(data[0]?.id); |
| 36 | + } catch (error) { |
| 37 | + console.log(error); |
| 38 | + toast.error("Failed to select a season"); |
| 39 | + } |
| 40 | + }; |
| 41 | + getData(); |
| 42 | + }, []); |
| 43 | + |
| 44 | + useEffect(() => { |
| 45 | + if (selectedSeason) { |
| 46 | + try { |
| 47 | + setLoading(true); |
| 48 | + fetchOnCampusOffers(selectedSeason).then((data) => { |
| 49 | + setAllOffers(data); |
| 50 | + }); |
| 51 | + if (seasons.find(season => season.id === selectedSeason)?.type === "INTERN") { |
| 52 | + setVisibleColumns(columns.filter((column: any) => !internHiddenColumns.includes(column?.accessorKey))); |
| 53 | + } else { |
| 54 | + setVisibleColumns(columns.filter((column: any) => !placementHiddenColumns.includes(column?.accessorKey))); |
| 55 | + } |
| 56 | + } catch (error) { |
| 57 | + toast.error("Failed to fetch on campus offers"); |
| 58 | + } finally { |
| 59 | + setLoading(false); |
| 60 | + } |
| 61 | + } |
| 62 | + }, [selectedSeason]); |
| 63 | + |
| 64 | + return ( |
| 65 | + <div className="container mx-auto my-4 md:my-8 px-2 md:px-4"> |
| 66 | + <h1 className="text-2xl md:text-3xl mb-4 md:mb-8 font-bold mx-auto text-center"> |
| 67 | + On Campus Offers |
| 68 | + </h1> |
| 69 | + <div className="flex justify-center"> |
| 70 | + <Select onValueChange={(value) => setSelectedSeason(value)} value={selectedSeason}> |
| 71 | + <SelectTrigger> |
| 72 | + <SelectValue> |
| 73 | + {seasons.find(season => season.id === selectedSeason)?.name || "Select a season"} |
| 74 | + </SelectValue> |
| 75 | + </SelectTrigger> |
| 76 | + <SelectContent> |
| 77 | + {seasons.map((season) => ( |
| 78 | + <SelectItem key={season.id} value={season.id}> |
| 79 | + {season.name} |
| 80 | + </SelectItem> |
| 81 | + ))} |
| 82 | + </SelectContent> |
| 83 | + </Select> |
| 84 | + </div> |
| 85 | + {loading && ( |
| 86 | + <div className="w-full flex justify-center"> |
| 87 | + <Loader /> |
| 88 | + </div> |
| 89 | + )} |
| 90 | + {allOffers && ( |
| 91 | + <div> |
| 92 | + <Table |
| 93 | + data={allOffers} |
| 94 | + columns={visibleColumns} |
| 95 | + type={"on-campus-offers"} |
| 96 | + /> |
| 97 | + </div> |
| 98 | + )} |
| 99 | + </div> |
| 100 | + ); |
| 101 | +}; |
| 102 | + |
| 103 | +export default OnCampusOffersPage; |
0 commit comments