Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modified task status flow #583

Merged
merged 10 commits into from Mar 22, 2024
2 changes: 2 additions & 0 deletions app/constants/tasks.js
Expand Up @@ -141,6 +141,8 @@ export const TASK_MESSAGES = {
MARK_DONE: 'This task will be marked as Done',
UPDATE_TASK: 'Updating task',
FIND_TASK: 'Finding new task for you!',
CHANGE_TO_100_PROGRESS: 'Proceeding further will make task progress 100%.',
CHANGE_TO_0_PROGRESS: 'Proceeding further will make task progress 0%.',
};

export const TASK_PERCENTAGE = {
Expand Down
130 changes: 113 additions & 17 deletions app/controllers/tasks.js
Expand Up @@ -42,7 +42,7 @@ export default class TasksController extends Controller {
@tracked showTasks = false;
@tracked showFetchButton = this.isShowFetchButton() && !this.alreadyFetched;
alreadyFetched = localStorage.getItem('already-fetched');

resetCurrentTask = null;
get isDevMode() {
return this.featureFlag.isDevMode;
}
Expand Down Expand Up @@ -97,16 +97,23 @@ export default class TasksController extends Controller {
const requestBody = { ...object };
const taskCompletionPercentage = object.percentCompleted;
if (taskCompletionPercentage) {
if (taskCompletionPercentage === TASK_PERCENTAGE.completedPercentage) {
this.isDevMode === true
? (requestBody.status = 'DONE')
: (requestBody.status = 'COMPLETED');
if (
taskCompletionPercentage === TASK_PERCENTAGE.completedPercentage &&
!this.dev
) {
requestBody.status = 'COMPLETED';
}
requestBody.percentCompleted = parseInt(taskCompletionPercentage);
}
return requestBody;
}

getTaskById(taskId) {
const indexOfSelectedTask = this.allTasks.findIndex(
Atifsid marked this conversation as resolved.
Show resolved Hide resolved
(task) => task.id === taskId
);
const selectedTask = this.allTasks[indexOfSelectedTask];
return selectedTask;
}
@action goBack() {
this.showModal = false;
this.onTaskChange('percentCompleted', '75');
Expand Down Expand Up @@ -143,9 +150,12 @@ export default class TasksController extends Controller {
delete this.taskFields[prop];
}
this.taskFields[key] = value;
if (this.resetCurrentTask) {
this.resetCurrentTask();
}
}

@action async updateTask(taskId, error) {
@action async updateTask(taskId) {
this.disabled = true;
this.buttonRequired = false;
const taskData = this.taskFields;
Expand All @@ -154,7 +164,9 @@ export default class TasksController extends Controller {
if (taskData.status || taskData.percentCompleted) {
try {
const response = await fetch(
`${API_BASE_URL}/tasks/self/${taskId}?userStatusFlag=true`,
`${API_BASE_URL}/tasks/self/${taskId}${
this.dev ? '?userStatusFlag=true' : ''
}`,
{
method: 'PATCH',
body: JSON.stringify(cleanBody),
Expand Down Expand Up @@ -195,7 +207,9 @@ export default class TasksController extends Controller {
toastNotificationTimeoutOptions
);
this.disabled = false;
error();
if (this.resetCurrentTask) {
this.resetCurrentTask();
}
}
} catch (err) {
this.toast.error(
Expand All @@ -204,7 +218,9 @@ export default class TasksController extends Controller {
toastNotificationTimeoutOptions
);
console.error('Error : ', err);
error();
if (this.resetCurrentTask) {
this.resetCurrentTask();
}
} finally {
this.disabled = false;
}
Expand Down Expand Up @@ -262,16 +278,96 @@ export default class TasksController extends Controller {
);
}
}
showTaskChangeInfoModal(msg) {
this.message = msg;
this.showModal = true;
this.buttonRequired = true;
}
isTaskStatusChanged(task, taskStatus) {
return task.status === taskStatus;
}
isProgressChanged(task, progress) {
return parseInt(task.percentCompleted || 0) === progress;
}
shouldTaskProgressBe100(taskId) {
const currentTask = this.getTaskById(taskId);
const taskData = this.taskFields;
const isCurrentTaskStatusBlock = this.isTaskStatusChanged(
currentTask,
this.TASK_KEYS.BLOCKED
);
const isCurrentTaskStatusInProgress = this.isTaskStatusChanged(
currentTask,
this.TASK_KEYS.IN_PROGRESS
);
const isNewStatusInProgress = this.isTaskStatusChanged(
taskData,
this.TASK_KEYS.IN_PROGRESS
);
const isNewTaskStatusBlock = this.isTaskStatusChanged(
taskData,
this.TASK_KEYS.BLOCKED
);

const isCurrProgress100 = this.isProgressChanged(currentTask, 100);
return (
(isCurrentTaskStatusBlock || isCurrentTaskStatusInProgress) &&
!isNewStatusInProgress &&
!isNewTaskStatusBlock &&
!isCurrProgress100
);
}
shouldTaskProgressBe0(taskId) {
const taskData = this.taskFields;
const currentTask = this.getTaskById(taskId);
const isCurrentTaskStatusBlock = this.isTaskStatusChanged(
currentTask,
this.TASK_KEYS.BLOCKED
);
const isNewStatusInProgress = this.isTaskStatusChanged(
taskData,
this.TASK_KEYS.IN_PROGRESS
);
const isCurrProgress0 = this.isProgressChanged(currentTask, 0);
return (
isNewStatusInProgress && !isCurrentTaskStatusBlock && !isCurrProgress0
);
}
getInfoMsg(taskId) {
const currentTask = this.getTaskById(taskId);
const msg = `The progress of current task is ${currentTask.percentCompleted}%. `;

@action async handleUpdateTask(taskId, error) {
if (this.shouldTaskProgressBe100(taskId)) {
this.taskFields.percentCompleted = 100;
return `${msg}${TASK_MESSAGES.CHANGE_TO_100_PROGRESS}`;
}

if (this.shouldTaskProgressBe0(taskId)) {
this.taskFields.percentCompleted = 0;

return `${msg}${TASK_MESSAGES.CHANGE_TO_0_PROGRESS}`;
}
return;
}
@action async handleUpdateTask(taskId, resetCurrentTask) {
this.resetCurrentTask = resetCurrentTask;
const taskData = this.taskFields;
if (taskData.percentCompleted === TASK_PERCENTAGE.completedPercentage) {
this.message = TASK_MESSAGES.MARK_DONE;
this.showModal = true;
this.buttonRequired = true;
this.tempTaskId = taskId;
this.tempTaskId = taskId;
if (this.dev && taskData.status) {
const msg = this.getInfoMsg(taskId);
if (msg) {
this.showTaskChangeInfoModal(msg);
return;
}
}

if (
taskData.percentCompleted === TASK_PERCENTAGE.completedPercentage &&
!this.dev
) {
this.showTaskChangeInfoModal(TASK_MESSAGES.MARK_DONE);
} else {
return this.updateTask(taskId, error);
return this.updateTask(taskId);
}
}
}
101 changes: 101 additions & 0 deletions tests/integration/components/tasks-test.js
skv93-coder marked this conversation as resolved.
Show resolved Hide resolved
Expand Up @@ -80,4 +80,105 @@ module('Integration | Component | tasks', function (hooks) {
);
assert.dom('[data-test-task-spinner]').doesNotExist();
});
test('changing the task status from any status other than BLOCKED to IN_PROGRESS inform proceeding further will make progress 0', async function (assert) {
tasks[0].status = 'SMOKE_TESTING';
this.set('dev', true);
const ctrl = this.owner.lookup('controller:tasks');

// before action
assert.equal(ctrl.showModal, false);
assert.equal(ctrl.message, '');

ctrl.allTasks = tasks;
ctrl.dev = true;
ctrl.taskFields = {
status: 'IN_PROGRESS',
};

//action
ctrl.send('handleUpdateTask', tasks[0].id, () => {});

//after action
assert.equal(ctrl.showModal, true);
assert.equal(
ctrl.message,
'The progress of current task is 25%. Proceeding further will make task progress 0%.'
);
assert.equal(ctrl.taskFields.percentCompleted, 0);
});
test('changing the task status from IN_PROGRESS to any status other than BLOCKED inform proceeding further will make progress 100', async function (assert) {
tasks[0].status = 'IN_PROGRESS';
this.set('dev', true);
const ctrl = this.owner.lookup('controller:tasks');

// before action
assert.equal(ctrl.showModal, false);
assert.equal(ctrl.message, '');

ctrl.allTasks = tasks;
ctrl.dev = true;
ctrl.taskFields = {
status: 'SMOKE_TESTING',
};

// action
ctrl.send('handleUpdateTask', tasks[0].id, () => {});

// after action
assert.equal(ctrl.showModal, true);
assert.equal(
ctrl.message,
'The progress of current task is 25%. Proceeding further will make task progress 100%.'
);
assert.equal(ctrl.taskFields.percentCompleted, 100);
});
test('changing the task status from BLOCKED to any status other than IN_PROGRESS when percentCompleted is less than 100, inform the user proceeding further will make progress 100', async function (assert) {
tasks[0].status = 'BLOCKED';
this.set('dev', true);
const ctrl = this.owner.lookup('controller:tasks');

// before action
assert.equal(ctrl.showModal, false);
assert.equal(ctrl.message, '');

ctrl.allTasks = tasks;
ctrl.dev = true;
ctrl.taskFields = {
status: 'SMOKE_TESTING',
};

// action
skv93-coder marked this conversation as resolved.
Show resolved Hide resolved
ctrl.send('handleUpdateTask', tasks[0].id, () => {});

// after action
assert.equal(ctrl.showModal, true);
assert.equal(
ctrl.message,
`The progress of current task is ${tasks[0].percentCompleted}%. Proceeding further will make task progress 100%.`
);
assert.equal(ctrl.taskFields.percentCompleted, 100);
});
test('changing the task status from any status other than blocked and in progress to other status does not result in showing modal', async function (assert) {
tasks[0].status = 'SMOKE_TESTING';
this.set('dev', true);
const ctrl = this.owner.lookup('controller:tasks');

// before action
assert.equal(ctrl.showModal, false);
assert.equal(ctrl.message, '');

ctrl.allTasks = tasks;
ctrl.dev = true;
ctrl.taskFields = {
status: 'NEEDS_REVIEW',
};

// action
ctrl.send('handleUpdateTask', tasks[0].id, () => {});

// after action
assert.equal(ctrl.showModal, false);
assert.equal(ctrl.message, '');
assert.equal(ctrl.taskFields.percentCompleted, undefined);
});
});