From 8604e4324f56bd5ff746a793b4cbbf23d8fdb637 Mon Sep 17 00:00:00 2001 From: huongthaoh <121412317+huongthaoh@users.noreply.github.com> Date: Sun, 3 Mar 2024 16:36:08 -0600 Subject: [PATCH 1/4] create program and multiselect event --- components/Forms/CreateProgramPopupWindow.tsx | 118 ++++++++++++++---- .../table/event-table/EventTableImpl.tsx | 23 +++- components/table/event-table/TableHeader.tsx | 30 +++-- .../table/program-table/ProgramTableImpl.tsx | 2 +- pages/api/program/index.ts | 17 ++- styles/components/createProgram.styles.tsx | 90 +++++++++++++ styles/components/event.styles.tsx | 13 +- styles/table.styles.tsx | 17 ++- 8 files changed, 261 insertions(+), 49 deletions(-) create mode 100644 styles/components/createProgram.styles.tsx diff --git a/components/Forms/CreateProgramPopupWindow.tsx b/components/Forms/CreateProgramPopupWindow.tsx index 10f29b5..6144509 100644 --- a/components/Forms/CreateProgramPopupWindow.tsx +++ b/components/Forms/CreateProgramPopupWindow.tsx @@ -1,52 +1,116 @@ -import React from 'react'; +import React, { useState } from 'react'; import PopupWindow from '@/components/PopupWindow'; import { - CreateEventContainer, CreateEventForm, FormLabel, FormHeader, InputFlex, FormInput, LargeFormInput, -} from '@/styles/components/event.styles'; -import { LongFormInput } from '@/styles/components/editEventPopupWindowForm.styles'; + LongFormInput, + CreateProgramContainer, +} from '@/styles/components/createProgram.styles'; import { SubmitButton, ButtonCenter, } from '@/styles/components/windowFlow.styles'; import { useForm } from 'react-hook-form'; +import { VolunteerProgramData } from 'bookem-shared/src/types/database'; +import { fetcher } from '@/utils/utils'; +import useSWR from 'swr'; +import { Modal, message } from 'antd'; const CreateProgramPopupWindow = ({ setShowPopup, }: { setShowPopup: React.Dispatch>; }) => { - const { handleSubmit } = useForm({}); - const onSubmit = (data: any) => {}; + const { + data: allPrograms, + isLoading, + error, + mutate, // Used to refetch the data + } = useSWR('/api/program/', fetcher, { + onSuccess: data => { + console.log(allPrograms); + }, + }); + // ANTD message + const [messageApi, contextHolder] = message.useMessage(); + const [programName, setProgramName] = useState(''); + const [programDescription, setProgramDescription] = useState(''); + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + + // check if the program name already exists + if (allPrograms?.some(p => p.name === programName)) { + messageApi.open({ + type: 'warning', + content: 'Program name already exists.', + }); + } else { + const response = await fetch('/api/program/', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + name: programName, + description: programDescription, + }), + }); + if (response.ok) { + const resObj = await response.json(); + console.log('OK'); + messageApi.open({ + type: 'success', + content: 'Program created successfully', + }); + mutate(); + } else { + messageApi.open({ + type: 'error', + content: 'Failed to create program', + }); + } + } + }; + return ( - setShowPopup(false)}> - - - Create Program - Program Name - - - + <> + {contextHolder} + setShowPopup(false)}> + + + Create Program + Program Name + + setProgramName(e.target.value)} + maxLength={50} + /> + - Program Description - + Program Description + setProgramDescription(e.target.value)} + maxLength={250} + /> - - Create - - - - + + Create + + + + + ); }; diff --git a/components/table/event-table/EventTableImpl.tsx b/components/table/event-table/EventTableImpl.tsx index e11494b..a78e509 100644 --- a/components/table/event-table/EventTableImpl.tsx +++ b/components/table/event-table/EventTableImpl.tsx @@ -1,5 +1,5 @@ import React, { useState, useEffect } from 'react'; -import { Table, TableProps, Tag, Input, Popover } from 'antd'; +import { Table, TableProps, Tag, Input, Popover, Button } from 'antd'; import type { ColumnType, ColumnsType, @@ -69,6 +69,19 @@ const EventTableImpl = ({ useEffect(() => { setFilteredDataByTags(dataForTable); }, [dataForTable, setFilteredDataByTags]); + const [selectedRowKeys, setSelectedRowKeys] = useState([]); + + const onSelectChange = newSelectedRowKeys => { + setSelectedRowKeys(newSelectedRowKeys); + }; + const rowSelection = { + selectedRowKeys, + onChange: onSelectChange, + }; + // const hasSelected = selectedRowKeys.length > 0; + const handleAddEvent = () => { + console.log(selectedRowKeys); + }; // check for errors and loading if (error) return
Failed to load event table
; @@ -193,15 +206,21 @@ const EventTableImpl = ({ <> {showPopup && } {showPopupTag && } + {/*