Skip to content

Commit

Permalink
Merge pull request #1648 from BluBloos/main
Browse files Browse the repository at this point in the history
feat: Add validation to edit tasks modal
  • Loading branch information
claremacrae authored Feb 10, 2023
2 parents 7585a05 + 826ab63 commit 7e57787
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/ui/EditTask.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@
};
let parsedStartDate: string = '';
let isStartDateValid: boolean = true;
let parsedScheduledDate: string = '';
let isScheduledDateValid: boolean = true;
let parsedDueDate: string = '';
let isDueDateValid: boolean = true;
let parsedRecurrence: string = '';
let isRecurrenceValid: boolean = true;
let parsedDone: string = '';
let addGlobalFilterOnSave: boolean = false;
let withAccessKeys: boolean = true;
Expand Down Expand Up @@ -92,11 +96,11 @@
*/
/**
* Read the entered value for a date field, and return the text to be displayed,
* to explain how the date string was interpreted.
* Parse and return the entered value for a date field.
* @param fieldName
* @param typedDate - what the user has entered, such as '2023-01-23' or 'tomorrow'
* @param forwardDate
* @returns the parsed date string. Includes "invalid" if {@code typedDate} was invalid.
*/
function parseTypedDateForDisplay(
fieldName: 'start' | 'scheduled' | 'due' | 'done',
Expand All @@ -119,6 +123,7 @@
* Like {@link parseTypedDateForDisplay} but also accounts for the 'Only future dates' setting.
* @param fieldName
* @param typedDate - what the user has entered, such as '2023-01-23' or 'tomorrow'
* @returns the parsed date string. Includes "invalid" if {@code typedDate} was invalid.
*/
function parseTypedDateForDisplayUsingFutureDate(fieldName: 'start' | 'scheduled' | 'due' | 'done', typedDate: string): string {
return parseTypedDateForDisplay(
Expand Down Expand Up @@ -150,30 +155,39 @@
$: {
editableTask.startDate = doAutocomplete(editableTask.startDate);
parsedStartDate = parseTypedDateForDisplayUsingFutureDate('start', editableTask.startDate);
isStartDateValid = !parsedStartDate.includes('invalid');
}
$: {
editableTask.scheduledDate = doAutocomplete(editableTask.scheduledDate);
parsedScheduledDate = parseTypedDateForDisplayUsingFutureDate('scheduled', editableTask.scheduledDate);
isScheduledDateValid = !parsedScheduledDate.includes('invalid');
}
$: {
editableTask.dueDate = doAutocomplete(editableTask.dueDate);
parsedDueDate = parseTypedDateForDisplayUsingFutureDate('due', editableTask.dueDate);
isDueDateValid = !parsedDueDate.includes('invalid');
}
$: {
isRecurrenceValid = true;
if (!editableTask.recurrenceRule) {
parsedRecurrence = '<i>not recurring</>';
} else {
parsedRecurrence =
Recurrence.fromText({
const recurrenceFromText = Recurrence.fromText({
recurrenceRuleText: editableTask.recurrenceRule,
// Only for representation in the modal, no dates required.
startDate: null,
scheduledDate: null,
dueDate: null,
})?.toText() ?? '<i>invalid recurrence rule</i>';
})?.toText();
if (!recurrenceFromText) {
parsedRecurrence = '<i>invalid recurrence rule</i>';
isRecurrenceValid = false;
} else {
parsedRecurrence = recurrenceFromText;
}
}
}
Expand Down Expand Up @@ -350,6 +364,7 @@
bind:value={editableTask.recurrenceRule}
id="recurrence"
type="text"
class:tasks-modal-error={!isRecurrenceValid}
placeholder="Try 'every 2 weeks on Thursday'."
accesskey={accesskey("r")}
/>
Expand All @@ -364,6 +379,7 @@
bind:value={editableTask.dueDate}
id="due"
type="text"
class:tasks-modal-error={!isDueDateValid}
placeholder={datePlaceholder}
accesskey={accesskey("d")}
/>
Expand All @@ -378,6 +394,7 @@
bind:value={editableTask.scheduledDate}
id="scheduled"
type="text"
class:tasks-modal-error={!isScheduledDateValid}
placeholder={datePlaceholder}
accesskey={accesskey("s")}
/>
Expand All @@ -392,6 +409,7 @@
bind:value={editableTask.startDate}
id="start"
type="text"
class:tasks-modal-error={!isStartDateValid}
placeholder={datePlaceholder}
accesskey={accesskey("a")}
/>
Expand Down Expand Up @@ -450,8 +468,10 @@
</div>
</div>
<div class="tasks-modal-section tasks-modal-buttons">
<button type="submit" class="mod-cta">Apply</button>
<button disabled={!(isDueDateValid && isRecurrenceValid && isScheduledDateValid && isStartDateValid)}
type="submit" class="mod-cta">Apply
</button>
<button type="button" on:click={_onClose}>Cancel</button>
</div>
</form>
</div>
</div>
9 changes: 9 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@
justify-content: space-between;
}

.tasks-modal-error {
border: 1px solid red !important;
}

.tasks-modal button:disabled {
pointer-events: none !important;
opacity: 0.3 !important;
}

@media (max-width: 649px) {
.tasks-modal-priorities {
grid-template-columns: 4em 7.5em 5em;
Expand Down

0 comments on commit 7e57787

Please sign in to comment.