Skip to content

Commit

Permalink
fix(pomodoro): case when there is no task available and the error whe…
Browse files Browse the repository at this point in the history
…n the last task was deleted
  • Loading branch information
johannesjo committed Jan 9, 2018
1 parent 5bd2935 commit 914149d
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 46 deletions.
87 changes: 66 additions & 21 deletions app-src/scripts/dialogs/task-selection/task-selection-c.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,77 @@
<md-dialog aria-label="Time estimation dialog"
class="dialog-task-selection"
md-theme="vm.theme">
<md-toolbar>
<div class="md-toolbar-tools">
<h2>Select a task</h2>
<span flex></span>
<md-button class="md-icon-button"
aria-label="Cancel"
ng-click="vm.cancel()">
<ng-md-icon icon="close"></ng-md-icon>
</md-button>
</div>
</md-toolbar>

<form ng-submit="vm.submit()">

<md-dialog-content>
<div class="md-dialog-content">

<p>Select the task you want to work on next or just press enter to work on</p><p>
<strong ng-bind="vm.undoneTasks[0].title"></strong></p>

<md-autocomplete
md-selected-item="vm.selectedTask"
md-search-text="vm.searchText"
md-items="task in vm.getFilteredUndoneTasks(vm.searchText)"
md-item-text="task.title"
md-input-minlength="0"
md-require-match="true"
md-autofocus="true"
md-autoselect
placeholder="Select a task to start with">
<md-item-template>
<span md-highlight-text="vm.searchText">{{ task.title }}</span>
</md-item-template>
<md-not-found>
No states matching "<span ng-bind="vm.searchText"></span>" were found.
<a ng-click="vm.newState(vm.searchText)">Create a new one!</a>
</md-not-found>
</md-autocomplete>
<section ng-if="!vm.isShowTaskCreationForm">
<p>Select the task you want to work on next or just press enter to work on</p><p>
<strong ng-bind="vm.undoneTasks[0].title"></strong></p>

<md-autocomplete
md-selected-item="vm.selectedTask"
md-search-text="vm.searchText"
md-items="task in vm.getFilteredUndoneTasks(vm.searchText)"
md-item-text="task.title"
md-input-minlength="0"
md-require-match="true"
md-autofocus="true"
md-autoselect
placeholder="Select a task to start with">
<md-item-template>
<span md-highlight-text="vm.searchText">{{ task.title }}</span>
</md-item-template>
<md-not-found>
No states matching "<span ng-bind="vm.searchText"></span>" were found.
<a ng-click="vm.newState(vm.searchText)">Create a new one!</a>
</md-not-found>
</md-autocomplete>
</section>


<section ng-if="vm.isShowTaskCreationForm">
<p><strong>No tasks created for today's list. Please create a new one.</strong></p>
<md-input-container class="md-block">
<label>Task Name</label>
<input type="text"
ng-model="vm.newTask.title"
md-auto-focus
required
aria-label="Title">
</md-input-container>

<md-input-container class="md-icon-float md-block">
<ng-md-icon icon="access_time"
aria-label="Estimated Durations"></ng-md-icon>
<label>Estimated Duration</label>

<input type="text"
input-duration="optional"
ng-model="vm.newTask.timeEstimate"
aria-label="Estimated Durations">
</md-input-container>

<md-input-container class="md-block">
<label>Notes</label>

<textarea ng-model="vm.newTask.notes"
aria-label="Notes"></textarea>
</md-input-container>
</section>
</div>
</md-dialog-content>

Expand Down
17 changes: 15 additions & 2 deletions app-src/scripts/dialogs/task-selection/task-selection-c.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,25 @@

vm.undoneTasks = Tasks.getUndoneToday(true);

if (!vm.undoneTasks || vm.undoneTasks.length === 0) {
vm.isShowTaskCreationForm = true;
vm.newTask = {};
}

vm.submit = () => {
if (!vm.selectedTask) {
if (vm.isShowTaskCreationForm) {
vm.selectedTask = Tasks.addToday(vm.newTask);
} else if (!vm.selectedTask) {
vm.selectedTask = vm.undoneTasks[0];
}

Tasks.updateCurrent(vm.selectedTask);
$mdDialog.hide(vm.selectedTask);

if (vm.selectedTask) {
$mdDialog.hide(vm.selectedTask);
} else {
$mdDialog.cancel();
}
};

vm.getFilteredUndoneTasks = (searchText) => {
Expand Down
58 changes: 57 additions & 1 deletion app-src/scripts/main/global-services/tasks-s.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
/* @ngInject */
class Tasks {

constructor(Uid, $rootScope, Dialogs, IS_ELECTRON, ShortSyntax, TasksUtil, Jira, TakeABreakReminder, SimpleToast, AppStorage, EV) {
constructor(Uid, $rootScope, Dialogs, IS_ELECTRON, ShortSyntax, TasksUtil, Jira, TakeABreakReminder, SimpleToast, AppStorage, EV, $q) {
this.EV = EV;
this.$rootScope = $rootScope;
this.$q = $q;
this.Uid = Uid;
this.$rootScope = $rootScope;
this.Dialogs = Dialogs;
Expand Down Expand Up @@ -83,10 +84,41 @@
return this.$rootScope.r.currentTask;
}

isInTodaysList(taskToCheck) {
let currentTask;
let subTaskMatch;

if (!taskToCheck || !taskToCheck.id) {
return false;
}

currentTask = _.find(this.$rootScope.r.tasks, (task) => {
if (task.subTasks && task.subTasks.length > 0) {
let subTaskMatchTmp = _.find(task.subTasks, { id: taskToCheck.id });
if (subTaskMatchTmp) {
subTaskMatch = subTaskMatchTmp;
}
}
return task.id === taskToCheck.id;
});
const match = currentTask || subTaskMatch;

return !!match;
}

getLastCurrent() {
return this.$rootScope.r.lastCurrentTask;
}

getLastCurrentIfInTodaysList() {
const lastCurrent = this.$rootScope.r.lastCurrentTask;
if (this.isInTodaysList(lastCurrent)) {
return lastCurrent;
} else {
return undefined;
}
}

setLastCurrent(task) {
this.$rootScope.r.lastCurrentTask = task;
}
Expand Down Expand Up @@ -264,6 +296,9 @@
if (task && task.title) {
this.$rootScope.r.tasks.unshift(this.createTask(task));
this.SimpleToast('SUCCESS', 'Task "' + task.title + '" created.', 200);

// return correct reference in today's list
return this.$rootScope.r.tasks[0];
}
}

Expand Down Expand Up @@ -628,6 +663,27 @@
this.updateCurrent(undefined);
}

selectLastTaskOrOpenDialog() {
const defer = this.$q.defer();

if (!this.getCurrent()) {
const lastCurrentTask = this.getLastCurrentIfInTodaysList();

if (lastCurrentTask) {
this.updateCurrent(lastCurrentTask);
defer.resolve(lastCurrentTask);
} else {
this.Dialogs('TASK_SELECTION')
.then(defer.resolve)
.catch(defer.reject);
}
} else {
defer.resolve();
}

return defer.promise;
}

selectNextTask(finishedCurrentTask) {
if (this.$rootScope.r.config.isAutoStartNextTask && finishedCurrentTask && finishedCurrentTask.isDone) {
// if sub task try to select the next undone sub task of the same parent
Expand Down
23 changes: 1 addition & 22 deletions app-src/scripts/pomodoro-button/pomodoro-button-s.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@

play() {
// select task if none selected
this.selectTask()
this.Tasks.selectLastTaskOrOpenDialog()
.then(() => {
this.start();

Expand Down Expand Up @@ -222,27 +222,6 @@
return (this.data.isOnBreak && (this.data.currentCycle % this.config.cyclesBeforeLongerBreak !== 0));
}

selectTask() {
const defer = this.$q.defer();

if (!this.Tasks.getCurrent()) {
const lastCurrentTask = this.Tasks.getLastCurrent();

if (lastCurrentTask) {
this.Tasks.updateCurrent(lastCurrentTask);
defer.resolve(lastCurrentTask);
} else {
this.Dialogs('TASK_SELECTION')
.then(defer.resolve)
.catch(defer.reject);
}
} else {
defer.resolve();
}

return defer.promise;
}

playSessionDoneSound() {
if (this.config.isPlaySound) {
new Audio(DEFAULT_SOUND).play();
Expand Down

0 comments on commit 914149d

Please sign in to comment.