-
Notifications
You must be signed in to change notification settings - Fork 5
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
[App] Add presences list #186
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4a95d14
Starting the presences list
Afonso-santos 52cba17
visual aspect done
Afonso-santos 95a6abf
presences_list call api
Afonso-santos fcb0d1a
Presences list done
Afonso-santos fdacf02
correct some details
Afonso-santos f0a0d86
correct some erros
Afonso-santos 04a746a
request changes done
Afonso-santos d41a304
request changes done
Afonso-santos 675cbab
request changes done
Afonso-santos a2a9371
request changes done
Afonso-santos 0169654
some details
Afonso-santos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,319 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useRouter } from "next/router"; | ||
import { Button, Row, Select, Space, Table, Typography } from "antd"; | ||
import { CloseOutlined, EditOutlined, SaveOutlined } from "@ant-design/icons"; | ||
|
||
import { | ||
getNinjaEvents, | ||
listEvents, | ||
listLectures, | ||
listMentors, | ||
} from "bokkenjs"; | ||
import * as api from "bokkenjs"; | ||
import { notifyError, notifyInfo } from "~/components/Notification"; | ||
import AppLayout from "~/layouts/AppLayout"; | ||
import { set } from "lodash-es"; | ||
const { Title } = Typography; | ||
|
||
type Event = { | ||
id: string; | ||
title: string; | ||
}; | ||
|
||
type Lecture = { | ||
id: string; | ||
event: Event; | ||
attendance: string; | ||
}; | ||
|
||
export default function Presences() { | ||
const [editButtonVisible, setEditButtonVisible] = useState(true); | ||
const [saveButtonVisible, setSaveButtonVisible] = useState(false); | ||
const [cancelButtonVisible, setCancelButtonVisible] = useState(false); | ||
const [data, setData] = useState<any[]>([]); | ||
const [originalData, setOriginalData] = useState<any[]>([]); | ||
const [events, setEvents] = useState<Event[]>([]); | ||
const [selectedEvent, setSelectedEvent] = useState<string>(""); | ||
const [lectures, setLectures] = useState<Lecture[]>([]); | ||
const [selectedLectures, setSelectedLectures] = useState<Lecture[]>([]); | ||
const [locations, setLocations] = useState<any[]>([]); | ||
const router = useRouter(); | ||
|
||
// const onFinish = (values: any, lectureId: string) => { | ||
// api | ||
|
||
// .updateLecture(lectureId, values) | ||
// .then(() => { | ||
// notifyInfo("Os dados da sessão foram atualizados com sucesso", ""); | ||
// }) | ||
// .catch((error) => { | ||
// notifyError( | ||
// "Ocorreu um erro", | ||
// "Não foi possível atualizar os dados da sessão" | ||
// ); | ||
// }); | ||
// }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this if you no longer need it |
||
const onFinish = (values: any, lectureId: string) => { | ||
api | ||
.updateLecture(lectureId, values) | ||
.then(() => { | ||
notifyInfo("Os dados da sessão foram atualizados com sucesso", ""); | ||
|
||
// Update the corresponding lecture in lectures state with the new data | ||
const updatedLectures = lectures.map((lecture) => { | ||
if (lecture.id === lectureId) { | ||
return { | ||
...lecture, | ||
attendance: values.attendance, | ||
}; | ||
} | ||
return lecture; | ||
}); | ||
|
||
setLectures(updatedLectures); | ||
}) | ||
.catch((error) => { | ||
notifyError( | ||
"Ocorreu um erro", | ||
"Não foi possível atualizar os dados da sessão" | ||
); | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
api | ||
.listEvents() | ||
.then((response: any) => setEvents(response.data)) | ||
.catch((error) => { | ||
notifyError( | ||
"Ocorreu um erro", | ||
"Não foi possível atualizar os dados da sessão" | ||
); | ||
}); | ||
}, []); | ||
|
||
useEffect(() => { | ||
api | ||
.listLectures() | ||
.then((response: any) => setLectures(response.data)) | ||
|
||
.catch(() => {}); | ||
|
||
selectedLectures.forEach((lecture) => { | ||
if (lecture.attendance == null) { | ||
lecture.attendance = "both_absent"; | ||
} | ||
data.push({ | ||
presences: `${lecture.attendance}`, | ||
key: lecture.id, | ||
}); | ||
}); | ||
}, []); | ||
|
||
useEffect(() => { | ||
api | ||
.getLocations() | ||
.then((response: any) => setLocations(response.data)) | ||
.catch((error) => { | ||
notifyError( | ||
"Ocorreu um erro", | ||
"Não foi possível atualizar os dados da sessão" | ||
); | ||
}); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (selectedEvent !== "") { | ||
setSelectedLectures( | ||
lectures.filter((lecture) => lecture.event.id === selectedEvent) | ||
); | ||
} | ||
}, [selectedEvent, lectures]); | ||
|
||
useEffect(() => { | ||
generateData(); | ||
}, [selectedLectures]); | ||
|
||
const generateData = () => { | ||
const data: any[] = []; | ||
|
||
selectedLectures.map((lecture: any) => { | ||
if (lecture.attendance == null) { | ||
lecture.attendance = "both_absent"; | ||
} | ||
data.push({ | ||
ninja: `${lecture.ninja.first_name} ${lecture.ninja.last_name}`, | ||
mentor: `${lecture.mentor.first_name} ${lecture.mentor.last_name}`, | ||
presences: `${lecture.attendance}`, | ||
key: lecture.id, | ||
}); | ||
}); | ||
|
||
setData(data); | ||
}; | ||
|
||
const handleEditButtonClick = () => { | ||
setEditButtonVisible(false); | ||
setSaveButtonVisible(true); | ||
setCancelButtonVisible(true); | ||
}; | ||
|
||
const handleSaveButtonClick = () => { | ||
const hasChanges = JSON.stringify(data) !== JSON.stringify(originalData); | ||
|
||
if (hasChanges) { | ||
data.forEach((item) => { | ||
const lectureId = item.key; | ||
const values = { attendance: item.presences }; | ||
|
||
onFinish(values, lectureId); | ||
}); | ||
} | ||
|
||
setOriginalData([...data]); | ||
setSaveButtonVisible(false); | ||
setCancelButtonVisible(false); | ||
setEditButtonVisible(true); | ||
}; | ||
|
||
const handleCancelButtonClick = () => { | ||
setSaveButtonVisible(false); | ||
setCancelButtonVisible(false); | ||
setEditButtonVisible(true); | ||
}; | ||
|
||
const handleComboBoxChange = (key: string) => (value: string) => { | ||
const updatedData = [...data]; | ||
const dataIndex = updatedData.findIndex((item) => item.key === key); | ||
if (dataIndex > -1) { | ||
updatedData[dataIndex].presences = value; | ||
setData(updatedData); | ||
} | ||
}; | ||
|
||
const getPresencesLabel = (value: string) => { | ||
switch (value) { | ||
case "both_absent": | ||
return "Nenhum"; | ||
case "ninja_absent": | ||
return "Ninja Faltou"; | ||
case "mentor_absent": | ||
return "Mentor Faltou"; | ||
case "both_present": | ||
return "Presentes"; | ||
default: | ||
return ""; | ||
} | ||
}; | ||
const columns = [ | ||
{ | ||
title: "Ninja", | ||
dataIndex: "ninja", | ||
width: "33%", | ||
}, | ||
{ | ||
title: "Mentor", | ||
dataIndex: "mentor", | ||
width: "33%", | ||
}, | ||
{ | ||
title: "Presenças", | ||
dataIndex: "presences", | ||
width: "33%", | ||
render: (_: any, record: any) => { | ||
const label = getPresencesLabel(record.presences); | ||
if (editButtonVisible) { | ||
return <span>{label}</span>; | ||
} else { | ||
return ( | ||
<Select | ||
value={record.presences} | ||
onChange={handleComboBoxChange(record.key)} | ||
style={{ width: 160 }} | ||
> | ||
<Select.Option value="both_absent">Nenhum</Select.Option> | ||
<Select.Option value="ninja_absent">Ninja Faltou</Select.Option> | ||
<Select.Option value="mentor_absent">Mentor Faltou</Select.Option> | ||
<Select.Option value="both_present">Presentes</Select.Option> | ||
</Select> | ||
); | ||
} | ||
}, | ||
}, | ||
]; | ||
|
||
return ( | ||
<> | ||
<AppLayout> | ||
<Row justify="space-between"> | ||
<Title level={2}>Presenças</Title> | ||
<Space> | ||
{editButtonVisible && ( | ||
<Button | ||
shape="circle" | ||
type="primary" | ||
size="large" | ||
icon={<EditOutlined />} | ||
onClick={handleEditButtonClick} | ||
disabled={!editButtonVisible} | ||
/> | ||
)} | ||
{saveButtonVisible && ( | ||
<Button | ||
shape="circle" | ||
type="primary" | ||
size="large" | ||
icon={<SaveOutlined />} | ||
onClick={handleSaveButtonClick} | ||
disabled={!saveButtonVisible} | ||
/> | ||
)} | ||
{cancelButtonVisible && ( | ||
<Button | ||
danger | ||
shape="circle" | ||
type="primary" | ||
size="large" | ||
icon={<CloseOutlined />} | ||
onClick={handleCancelButtonClick} | ||
disabled={!cancelButtonVisible} | ||
/> | ||
)} | ||
</Space> | ||
</Row> | ||
<Row justify="space-around" gutter={[50, 50]}> | ||
<Select | ||
showSearch | ||
defaultValue="Selecione um Evento" | ||
style={{ width: 450 }} | ||
onChange={setSelectedEvent} | ||
filterOption={(input, option) => | ||
(option?.label ?? "").toLowerCase().includes(input.toLowerCase()) | ||
} | ||
optionFilterProp="children" | ||
options={events.map((event) => ({ | ||
label: event.title, | ||
value: event.id, | ||
}))} | ||
value={selectedEvent == "" ? undefined : selectedEvent} | ||
> | ||
{events.map((event) => ( | ||
<Select.Option value={event.id}>{event.title}</Select.Option> | ||
))} | ||
</Select> | ||
</Row> | ||
<div style={{ marginBottom: 25 }} /> | ||
<Table | ||
columns={columns} | ||
dataSource={data} | ||
pagination={{ | ||
pageSize: 40, | ||
}} | ||
scroll={{ | ||
y: 490, | ||
}} | ||
/> | ||
</AppLayout> | ||
</> | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't these changes duplicated from #181 ? If so it should be removed as it will be automatically updated when that PR gets merged