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

[ITG-105] feat: use enum task status #33

Merged
merged 5 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion bode/bode/models/task.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import enum
import uuid

from sqlalchemy import Enum
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.exc import IntegrityError, NoResultFound

Expand All @@ -9,14 +11,23 @@
from bode.models.utc_datetime import UTCDateTime


class Status(enum.Enum):
tgargula marked this conversation as resolved.
Show resolved Hide resolved
todo = "todo"
indirectly_done = "indirectly_done"
done = "done"
tgargula marked this conversation as resolved.
Show resolved Hide resolved

def __str__(self):
return self.name

tgargula marked this conversation as resolved.
Show resolved Hide resolved

class Task(db.Model):
__tablename__ = "tasks"

id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
title = db.Column(db.String(80), nullable=False)
description = db.Column(db.String(1024), nullable=False, server_default="")
due_date = db.Column(UTCDateTime(), nullable=True)
is_done = db.Column(db.Boolean, nullable=False, server_default="false")
status = db.Column(Enum(Status), nullable=False, server_default="todo")

tags = db.relationship("Tag", secondary=task_tag, back_populates="task")

Expand Down
12 changes: 6 additions & 6 deletions bode/bode/models/task_actions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from bode.app import db
from bode.models.task import Task
from bode.models.task import Status, Task
from bode.models.task_relation import RelationType, TaskRelation


Expand All @@ -23,7 +23,7 @@ def is_subtask_relation(relation):


def edit_task(task_id, **task_data):
"""Function edit task. If task is checked, all interchangable task will be checked."""
"""Function edit task. If task is checked, all interchangable tasks will be checked."""
tgargula marked this conversation as resolved.
Show resolved Hide resolved

def is_interchangable_relation(relation):
return relation.type == RelationType.Interchangable.value and str(relation.first_task_id) == task_id
Expand All @@ -33,20 +33,20 @@ def is_interchangable_relation(relation):
task.title = task_data["title"]
task.description = task_data["description"]
task.due_date = task_data["due_date"]
task.is_done = task_data["is_done"]
task.status = task_data["status"]
tgargula marked this conversation as resolved.
Show resolved Hide resolved

db.session.commit()

if task_data["is_done"]:
if task_data["status"] == Status.done.name:
tgargula marked this conversation as resolved.
Show resolved Hide resolved
for relation, related_task in TaskRelation.get_related_tasks(task_id):
if is_interchangable_relation(relation):
if related_task.is_done:
if related_task.status != Status.todo:
continue
inter_task_data = {
"title": related_task.title,
"description": related_task.description,
"due_date": related_task.due_date,
"is_done": True,
"status": Status.indirectly_done.name,
}
edit_task(str(related_task.id), **inter_task_data)
tgargula marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
2 changes: 1 addition & 1 deletion bode/bode/resources/tasks/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class Tasks(MethodView):
@blueprint.response(200, TaskSchema(many=True))
def get(self):
return Task.query.order_by(Task.is_done, Task.due_date).all()
return Task.query.order_by(Task.status, Task.due_date).all()

@blueprint.arguments(TaskInputSchema)
@blueprint.response(201, TaskSchema)
Expand Down
4 changes: 2 additions & 2 deletions bode/bode/resources/tasks/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Meta:
title = fields.String(validate=validate.Length(1, 80), required=True)
description = fields.String(validate=validate.Length(0, 1024), default="")
due_date = fields.DateTime(allow_none=True)
is_done = fields.Boolean(default=False)
status = fields.String(default="todo")
tgargula marked this conversation as resolved.
Show resolved Hide resolved
tags = fields.List(fields.Nested(TagInputSchema), default=[])


Expand All @@ -20,5 +20,5 @@ class TaskSchema(BaseSchema):
title = fields.String()
description = fields.String()
due_date = fields.DateTime()
is_done = fields.Boolean()
status = fields.String()
tags = fields.List(fields.Nested(TagSchema))
43 changes: 43 additions & 0 deletions bode/migrations/versions/4c05d8b56a2d_use_task_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Use task status

Revision ID: 4c05d8b56a2d
Revises: d71419c8a49a
Create Date: 2022-05-02 19:55:23.069770

"""
import sqlalchemy as sa

from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "4c05d8b56a2d"
down_revision = "d71419c8a49a"
branch_labels = None
depends_on = None

status = postgresql.ENUM("todo", "indirectly_done", "done", name="status")


def upgrade():
status.create(op.get_bind())
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"tasks",
sa.Column(
"status", sa.Enum("todo", "indirectly_done", "done", name="status"), server_default="todo", nullable=False
),
)
op.drop_column("tasks", "is_done")
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"tasks",
sa.Column("is_done", sa.BOOLEAN(), server_default=sa.text("false"), autoincrement=False, nullable=False),
)
op.drop_column("tasks", "status")
# ### end Alembic commands ###
status.drop(op.get_bind())
4 changes: 2 additions & 2 deletions cabra/src/pages/components/CreateTaskForm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ITask, TaskStatus } from "../../types/task";
import { createTask, getTasks } from "../../api/tasks";
import { useMutation, useQueryClient } from "react-query";

import { ITask } from "../../types/task";
import TaskForm from "./TaskForm";
import { routeHelpers } from "../../routes";
import { useNavigate } from "react-router-dom";
Expand All @@ -10,7 +10,7 @@ const emptyTask: Omit<ITask, "id"> = {
description: "",
dueDate: null,
title: "",
isDone: false,
status: TaskStatus.TODO,
tags: [],
};

Expand Down
7 changes: 4 additions & 3 deletions cabra/src/pages/components/RelatedTask.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import "twin.macro";

import { ITask, TaskStatus } from "../../types/task";
import { getTask, getTasks, updateTask } from "../../api/tasks";
import { useMutation, useQueryClient } from "react-query";

import Checkbox from "./CheckBox";
import { DirectedRelationType } from "../../types/taskRelation";
import { ITask } from "../../types/task";
import { getRelatedTasks } from "../../api/taskRelations";
import { useState } from "react";

Expand Down Expand Up @@ -37,7 +37,8 @@ export default function RelatedTask({
try {
const updatedTask = {
...task,
isDone: !task.isDone,
status:
task.status === TaskStatus.DONE ? TaskStatus.TODO : TaskStatus.DONE,
};
await editTask.mutateAsync(updatedTask);
} catch (error) {
Expand All @@ -56,7 +57,7 @@ export default function RelatedTask({
<p tw="place-self-end">
<Checkbox
id={task.id}
checked={task.isDone}
checked={task.status !== TaskStatus.TODO}
onChange={handleIsDoneChange}
/>
</p>
Expand Down
4 changes: 2 additions & 2 deletions cabra/src/pages/components/SubtasksListEdit.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DirectedRelationType, ITaskRelation } from "../../types/taskRelation";
import { FormEvent, useState } from "react";
import { ITask, TaskStatus } from "../../types/task";
import tw, { styled } from "twin.macro";
import { useMutation, useQuery } from "react-query";

import { ITask } from "../../types/task";
import MiniTaskDelete from "./MiniTaskDelete";
import { createTask } from "../../api/tasks";
import { getRelatedTasks } from "../../api/taskRelations";
Expand All @@ -21,7 +21,7 @@ const emptyTask: Omit<ITask, "id"> = {
description: "",
dueDate: null,
title: "",
isDone: false,
status: TaskStatus.TODO,
tags: [],
};

Expand Down
3 changes: 2 additions & 1 deletion cabra/src/pages/components/TaskDetailsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DirectedRelationType } from "../../types/taskRelation";
import { Link } from "react-router-dom";
import NavigationButton from "./NavigationButton";
import RelatedTasksList from "./RelatedTaskList";
import { TaskStatus } from "../../types/task";
import { formatDateTime } from "../../utils/dates";
import { routeHelpers } from "../../routes";
import styled from "@emotion/styled";
Expand Down Expand Up @@ -48,7 +49,7 @@ export default function TaskDetails({ id }: Props) {
</CardField>
<Ctas align="center">
<CheckBox
checked={task.isDone}
checked={task.status !== TaskStatus.TODO}
id={`task-${task.id}`}
onChange={handleStatusChange}
/>
Expand Down
8 changes: 5 additions & 3 deletions cabra/src/pages/components/TaskListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ITask, TaskStatus } from "../../types/task";
import { getTask, getTasks, updateTask } from "../../api/tasks";
import tw, { styled } from "twin.macro";
import { useMutation, useQueryClient } from "react-query";

import { ArrowRightIcon } from "@heroicons/react/solid";
import CheckBox from "./CheckBox";
import { ITask } from "../../types/task";
import { Link } from "react-router-dom";
import NavigationButton from "./NavigationButton";
import { formatDateTime } from "../../utils/dates";
Expand All @@ -29,10 +29,12 @@ export default function TaskListItem({ task }: Props) {
},
});
const handleIsDoneChange = async () => {
const newStatus =
task.status === TaskStatus.DONE ? TaskStatus.TODO : TaskStatus.DONE;
try {
const updatedTask = {
...task,
isDone: !task.isDone,
status: newStatus,
};
await editTask.mutateAsync(updatedTask);
} catch (error) {
Expand Down Expand Up @@ -62,7 +64,7 @@ export default function TaskListItem({ task }: Props) {
<div>
<Card tw="flex justify-center items-center gap-4">
<CheckBox
checked={task.isDone}
checked={task.status !== TaskStatus.TODO}
id={`task-${task.id}`}
onChange={handleIsDoneChange}
size="sm"
Expand Down
8 changes: 4 additions & 4 deletions cabra/src/pages/components/TaskRelationsEdit.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ITask, TaskStatus } from "../../types/task";
import tw, { styled } from "twin.macro";

import { DirectedRelationType } from "../../types/taskRelation";
import { ITask } from "../../types/task";
import RelationListEdit from "./RelationListEdit";
import Select from "react-select";
import { getRelatedTasks } from "../../api/taskRelations";
Expand Down Expand Up @@ -48,16 +48,16 @@ export default function TaskRelationsEdit({ taskId }: Props) {

const subtasks = dataSubtasks?.data;

const potentialRelative = (isDone: boolean, id: string) => {
const potentialRelative = (status: TaskStatus, id: string) => {
return (
!isDone &&
status === TaskStatus.TODO &&
id !== taskId &&
!subtasks?.map(({ task: subtask }) => subtask.id).includes(id)
);
};

const formattedRelativesToBe: TaskOption[] = data.data
.filter(({ isDone, id }) => potentialRelative(isDone, id))
.filter(({ status, id }) => potentialRelative(status, id))
.map((task) => ({ value: task, label: task.title }));

const toggleShowSelected = () => {
Expand Down
5 changes: 3 additions & 2 deletions cabra/src/pages/hooks/useTaskDetails.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ITask, TaskStatus } from "../../types/task";
import { deleteTask, getTask, getTasks, updateTask } from "../../api/tasks";
import { useMutation, useQuery, useQueryClient } from "react-query";

import { ITask } from "../../types/task";
import { routeHelpers } from "../../routes";
import { useNavigate } from "react-router-dom";
import { useState } from "react";
Expand Down Expand Up @@ -30,7 +30,8 @@ const useTask = (id: string) => {
const handleStatusChange = async () => {
const updatedTask = {
...task,
isDone: !task?.isDone,
status:
task.status === TaskStatus.DONE ? TaskStatus.TODO : TaskStatus.DONE,
};
editTask.mutate(updatedTask);
};
Expand Down
8 changes: 7 additions & 1 deletion cabra/src/types/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ export interface ITag {
name: string;
}

export enum TaskStatus {
TODO = "todo",
INDIRECTLY_DONE = "indirectly_done",
DONE = "done",
}

export interface ITask {
id: string;
title: string;
description: string;
dueDate: string | null;
isDone: boolean;
status: TaskStatus;
tags: ITag[];
}