Skip to content

Commit

Permalink
feat: create tasks automatically on load and if relevant input changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-zelger committed Feb 7, 2024
1 parent 2fb0b9b commit aa370f6
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions src/components/CalendarForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ function CalendarForm() {
window.sessionStorage.setItem(calendarPrefixCacheKey, newPrefix);
}

const resetValues = () => {
window.sessionStorage.removeItem(startDateCacheKey);
setStartDate(initialStartDate)

window.sessionStorage.removeItem(responsibleCacheKey);
setResponsible(initialResponsible)

window.sessionStorage.removeItem(bufferCacheKey);
setPuffer(0)

window.sessionStorage.removeItem(calendarPrefixCacheKey);
setCalendarTitlePrefix('')
}

const hasActiveCache = () => {
return !!window.sessionStorage.getItem(startDateCacheKey)
|| !!window.sessionStorage.getItem(responsibleCacheKey)
|| !!window.sessionStorage.getItem(bufferCacheKey)
|| !!window.sessionStorage.getItem(calendarPrefixCacheKey)
}

const createTasks = useCallback((event?: FormEvent<HTMLFormElement> | undefined) => {
event?.preventDefault()

Expand Down Expand Up @@ -102,28 +123,11 @@ function CalendarForm() {
.sort((a: TaskT, b: TaskT) => a.deadline.getTime() - b.deadline.getTime())

setTasks(tasks)
}, [taskList, startDate, responsible, puffer])
}, [startDate, responsible, puffer, taskList]);

const resetValues = () => {
window.sessionStorage.removeItem(startDateCacheKey);
setStartDate(initialStartDate)

window.sessionStorage.removeItem(responsibleCacheKey);
setResponsible(initialResponsible)

window.sessionStorage.removeItem(bufferCacheKey);
setPuffer(0)

window.sessionStorage.removeItem(calendarPrefixCacheKey);
setCalendarTitlePrefix('')
}

const hasActiveCache = () => {
return !!window.sessionStorage.getItem(startDateCacheKey)
|| !!window.sessionStorage.getItem(responsibleCacheKey)
|| !!window.sessionStorage.getItem(bufferCacheKey)
|| !!window.sessionStorage.getItem(calendarPrefixCacheKey)
}
useEffect(() => {
createTasks()
}, [createTasks]);

useEffect(() => {
const getTasks = async () => {
Expand All @@ -146,13 +150,13 @@ function CalendarForm() {
}

loadCachedValues()
getTasks().then(() => createTasks())
}, [taskList, createTasks]);
getTasks()
}, []);

return (
<div>
<div className='calendar-form-container'>
<form onSubmit={createTasks}>
<form>
<ul className='calendar-form'>
<li>
<label>
Expand Down Expand Up @@ -195,7 +199,6 @@ function CalendarForm() {
</li>
<li>
<div style={buttonGroupStyle}>
<button type="submit"> {t('calendarPage.ics.generate')}</button>
{hasActiveCache() ?
<button className={"as-link"}
onClick={resetValues}>
Expand Down

0 comments on commit aa370f6

Please sign in to comment.