Skip to content

Commit

Permalink
Correct some react hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Afonso-santos committed Nov 23, 2023
1 parent 1ceaa82 commit 208d594
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 41 deletions.
21 changes: 6 additions & 15 deletions apps/app/components/LectureForm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default function LectureForm({ id }) {
setEvents(response.data);
});
}, []);

useEffect(() => {
if (id !== "new") {
listEvents().then((response) => {
Expand All @@ -59,7 +60,7 @@ export default function LectureForm({ id }) {
promise = Promise.all(events.map((event) => getNinjaEvents(event.id)));
}
return promise;
});
}, [events]);

useEffect(() => {
fetchData().then((responses) => {
Expand All @@ -71,7 +72,7 @@ export default function LectureForm({ id }) {
)
);
});
}, [events, fetchData]);
}, [fetchData]);

const [filteredNinjas, setFilteredNinjas] = useState([]);
const handleEventChange = useCallback(
Expand All @@ -94,7 +95,8 @@ export default function LectureForm({ id }) {
const filtered = mentors.filter((mentor) => {
const lecture = lectures.find(
(lecture) =>
lecture.mentor.id === mentor.id && lecture.event.id === selectedEvent
lecture.mentor.id === mentor.id &&
lecture.event.id === selectedEvent.id
);
return !lecture;
});
Expand All @@ -107,25 +109,14 @@ export default function LectureForm({ id }) {
);
}, [filteredNinjas]);

useEffect(() => {
const filtered = ninjas.filter((ninja) => {
const lecture = lectures.find(
(lecture) =>
lecture.ninja.id === ninja.id && lecture.event.id === selectedEvent
);
return !lecture;
});
setFilteredNinjas(filtered);
}, [ninjas, selectedEvent, lectures, handleEventChange]);

useEffect(() => {
setFilteredMentors(
filteredMentors.sort((a, b) => a.first_name.localeCompare(b.first_name))
);
}, [filteredMentors]);

const onFinish = (values) => {
if (Object.keys(selectedEvent).length != 0) {
if (Object.keys(selectedEvent).length !== 0) {
api
.createLecture(values)
.then(() => {
Expand Down
4 changes: 2 additions & 2 deletions apps/app/components/Signup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ function Signup() {
value
? Promise.resolve()
: Promise.reject(
"É necessário concordar com os termos e condições"
),
"É necessário concordar com os termos e condições"
),
},
]}
>
Expand Down
43 changes: 19 additions & 24 deletions apps/app/pages/admin/lectures/presences/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,34 +112,29 @@ export default function Presences() {
);
});
}, []);
const generateData = () => {
const data: any[] = [];
useEffect(() => {
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,
selectedLectures.forEach((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);
};
useEffect(() => {
if (selectedEvent !== "") {
setSelectedLectures(
lectures.filter((lecture) => lecture.event.id === selectedEvent)
);
}
}, [selectedEvent, lectures]);
setData(data);
};

useEffect(() => {
generateData();
}, [selectedLectures, generateData]);
if (selectedLectures.length > 0) {
generateData();
}
}, [selectedLectures]);

const handleEditButtonClick = () => {
setEditButtonVisible(false);
Expand Down

0 comments on commit 208d594

Please sign in to comment.