From 43eef83aa8fc32653fc8770ccaa86ecdb562387e Mon Sep 17 00:00:00 2001 From: Kasper Juul Hermansen Date: Sun, 21 Nov 2021 21:00:30 +0100 Subject: [PATCH] Add priority --- .../Todo.Api/Controllers/TodosController.cs | 16 ++++---- .../Commands/Todo/CreateTodoCommand.cs | 2 +- .../Queries/Todos/TodoViewModel.cs | 6 ++- .../Mongo/Repositories/TodoRepository.cs | 7 +++- src/client/src/components/todos/todoItem.tsx | 25 +++++++++++ src/client/src/components/todos/todoList.tsx | 41 +++++++++++-------- src/client/src/core/entities/todo.tsx | 8 +++- 7 files changed, 73 insertions(+), 32 deletions(-) diff --git a/src/backend/server/src/Todo.Api/Controllers/TodosController.cs b/src/backend/server/src/Todo.Api/Controllers/TodosController.cs index 81c7276..f1bf00c 100644 --- a/src/backend/server/src/Todo.Api/Controllers/TodosController.cs +++ b/src/backend/server/src/Todo.Api/Controllers/TodosController.cs @@ -28,24 +28,24 @@ public class TodosController : ApiController [JsonPropertyName("project")] public string? Project { get; init; } + [JsonPropertyName("priority")] + public string? Priority { get; init; } + internal CreateTodoCommand To() => new( Title, Project, - Description); + Description, + Priority); } [HttpPost] public async Task> CreateTodo( [FromBody] CreateTodoRequest request) - { - return Ok(await Mediator.Send(request.To())); - } + => Ok(await Mediator.Send(request.To())); [HttpGet("{todoId}")] public async Task> GetTodoById([FromRoute] string todoId) - { - return await Mediator.Send(new GetTodoByIdQuery(todoId)); - } + => await Mediator.Send(new GetTodoByIdQuery(todoId)); [HttpGet] public async Task>> GetTodos([FromQuery] bool onlyActive = false) @@ -80,7 +80,7 @@ public class TodosController : ApiController [JsonPropertyName("created")] public long Created { get; init; } = 0; - + [JsonPropertyName("priority")] public string? Priority { get; init; } diff --git a/src/backend/server/src/Todo.Core/Application/Commands/Todo/CreateTodoCommand.cs b/src/backend/server/src/Todo.Core/Application/Commands/Todo/CreateTodoCommand.cs index 43cc640..73a2f84 100644 --- a/src/backend/server/src/Todo.Core/Application/Commands/Todo/CreateTodoCommand.cs +++ b/src/backend/server/src/Todo.Core/Application/Commands/Todo/CreateTodoCommand.cs @@ -42,7 +42,7 @@ public record CreateTodoCommand( request.TodoTitle, request.TodoProject, request.TodoDescription, - request.Priority + request.Priority, userId, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()); diff --git a/src/backend/server/src/Todo.Core/Application/Queries/Todos/TodoViewModel.cs b/src/backend/server/src/Todo.Core/Application/Queries/Todos/TodoViewModel.cs index f6a259c..26d94de 100644 --- a/src/backend/server/src/Todo.Core/Application/Queries/Todos/TodoViewModel.cs +++ b/src/backend/server/src/Todo.Core/Application/Queries/Todos/TodoViewModel.cs @@ -7,7 +7,8 @@ public record TodoViewModel( string AuthorId, string? Project, string? Description, - long Created + long Created, + string? Priority ) { internal static TodoViewModel From(Entities.Todo t) => new( @@ -17,5 +18,6 @@ public record TodoViewModel( t.AuthorId, t.Project, t.Description, - t.Created); + t.Created, + t.Priority); } \ No newline at end of file diff --git a/src/backend/server/src/Todo.Persistence/Mongo/Repositories/TodoRepository.cs b/src/backend/server/src/Todo.Persistence/Mongo/Repositories/TodoRepository.cs index 34f8814..7d5dab4 100644 --- a/src/backend/server/src/Todo.Persistence/Mongo/Repositories/TodoRepository.cs +++ b/src/backend/server/src/Todo.Persistence/Mongo/Repositories/TodoRepository.cs @@ -21,6 +21,7 @@ public class TodoRepository : ITodoRepository string title, string? projectName, string? description, + string? priority, string userId, long created) { @@ -31,7 +32,8 @@ public class TodoRepository : ITodoRepository Status = false, AuthorId = userId, Description = description, - Created = created + Created = created, + Priority = priority }; await _todosCollection.InsertOneAsync(todo); return todo.To(); @@ -77,7 +79,8 @@ public class TodoRepository : ITodoRepository ProjectName = todo.Project, AuthorId = todo.AuthorId, Description = todo.Description, - Created = todo.Created + Created = todo.Created, + Priority = todo.Priority }); return updatedTodo.To(); diff --git a/src/client/src/components/todos/todoItem.tsx b/src/client/src/components/todos/todoItem.tsx index 32f4068..8dc6e7b 100644 --- a/src/client/src/components/todos/todoItem.tsx +++ b/src/client/src/components/todos/todoItem.tsx @@ -11,6 +11,22 @@ interface TodoItemProps { displayProject: boolean; } +function getColorFromPriority(priority: string) { + switch (priority) { + case "p3": + return "bg-red-500"; + + case "p2": + return "bg-yellow-500"; + + case "p1": + return "bg-green-500"; + + default: + throw "No priority matches"; + } +} + export const TodoItem: FC = (props) => { const [isHovering, setIsHovering] = useState(false); const [menuIsOpen, setMenuIsOpen] = useState(false); @@ -42,6 +58,15 @@ export const TodoItem: FC = (props) => { {props.todo.description && (
)} + {props.todo.priority && ( +
+ {props.todo.priority} +
+ )} {!props.todo.authorId && (
)} diff --git a/src/client/src/components/todos/todoList.tsx b/src/client/src/components/todos/todoList.tsx index 36e8495..2285fda 100644 --- a/src/client/src/components/todos/todoList.tsx +++ b/src/client/src/components/todos/todoList.tsx @@ -2,6 +2,7 @@ import { Todo } from "@src/core/entities/todo"; import { TodoItem } from "@src/components/todos/todoItem"; import { AddTodo } from "@src/components/todos/addTodo"; import { useUpdateTodo } from "@src/presentation/hooks/socketHooks"; +import { useMemo } from "react"; export const TodoList = (props: { todos: Todo[]; @@ -11,27 +12,31 @@ export const TodoList = (props: { }) => { const { updateTodo } = useUpdateTodo(); + const todos: Todo[] = useMemo(() => { + return props.todos + .filter((t) => { + if (!props.hideDone) return true; + + return t.status == false; + }) + .sort((a, b) => a.created - b.created) + .sort((a, b) => b.priority.localeCompare(a.priority)); + }, [props.todos, props.hideDone]); + return ( <>
    - {props.todos - .filter((t) => { - if (!props.hideDone) return true; - - return t.status == false; - }) - .sort((a, b) => a.created - b.created) - .map((t, i) => ( -
  • - { - updateTodo(todo); - }} - displayProject={!props.hideProject} - /> -
  • - ))} + {todos.map((t, i) => ( +
  • + { + updateTodo(todo); + }} + displayProject={!props.hideProject} + /> +
  • + ))}
diff --git a/src/client/src/core/entities/todo.tsx b/src/client/src/core/entities/todo.tsx index ac1d9f9..6e791de 100644 --- a/src/client/src/core/entities/todo.tsx +++ b/src/client/src/core/entities/todo.tsx @@ -6,6 +6,12 @@ export const StatusState: { done: Done; notDone: NotDone } = { notDone: false, }; +enum Priorities { + p1 = "p1", + p2 = "p2", + p3 = "p3", +} + export interface Todo { id: string; title: string; @@ -14,7 +20,7 @@ export interface Todo { project?: string; authorId?: string; created: number; - priority?: string; + priority?: Priorities; } export const asTodo = (item: Todo): Todo => {