Update conditional rendering of SelectedDates

This commit is contained in:
rui hildt 2020-06-02 15:42:14 +02:00
parent 7ae26e39c4
commit 0ca7e66a4b

View File

@ -26,13 +26,15 @@ export default function SelectedDates() {
useEffect(() => { useEffect(() => {
// Update selected dates // Update selected dates
let updatedDates = eventsToDates(eventsList) let updatedDates = eventsToDates(eventsList);
setDatesList(updatedDates) setDatesList(updatedDates);
console.log(eventsList)
}, [eventsList]); }, [eventsList]);
const handleDelete = (date) => { const handleDelete = (date) => {
let currentEvent = { start: date.toFormat('yyyy-MM-dd'), display: 'background' } let currentEvent = {
start: date.toFormat('yyyy-MM-dd'),
display: 'background',
};
// Find the event corresponding to the date // Find the event corresponding to the date
let selectedEventIndex; let selectedEventIndex;
@ -48,12 +50,13 @@ export default function SelectedDates() {
eventsList.splice(selectedEventIndex, 1); eventsList.splice(selectedEventIndex, 1);
let updatedEvents = [...eventsList]; let updatedEvents = [...eventsList];
// Update the eventsList // Update the eventsList
setEventsList(updatedEvents) setEventsList(updatedEvents);
} };
if (datesList.length > 0) {
return ( return (
<> <>
{datesList.length > 0 && (
<div>
<h3>Dates selected</h3> <h3>Dates selected</h3>
<ul> <ul>
{datesList.map((date) => ( {datesList.map((date) => (
@ -70,29 +73,30 @@ export default function SelectedDates() {
</li> </li>
))} ))}
</ul> </ul>
</div>
)}
</> </>
); );
} else {
return(<></>)
} }
}
// Convert events to Luxon Datetime objects // Convert events to Luxon Datetime objects
const eventsToDates = (events) => { const eventsToDates = (events) => {
let dates = [] let dates = [];
events.forEach((event) => { events.forEach((event) => {
dates.push(DateTime.fromFormat(event.start, 'yyyy-MM-dd')); dates.push(DateTime.fromFormat(event.start, 'yyyy-MM-dd'));
}); });
return dates return dates;
} };
// Convert Luxon Datetime objects to events // Convert Luxon Datetime objects to events
const datesToEvents = (dates) => { const datesToEvents = (dates) => {
let events = [] let events = [];
dates.forEach((date) => { dates.forEach((date) => {
let event = { start: date.toFormat('yyyy-MM-dd'), display: 'background' } let event = {
start: date.toFormat('yyyy-MM-dd'),
display: 'background',
};
events.push(event); events.push(event);
}); });
return events return events;
} };