Add priority

This commit is contained in:
Kasper Juul Hermansen 2021-11-21 21:00:30 +01:00
parent 33f050085c
commit 43eef83aa8
Signed by: kjuulh
GPG Key ID: DCD9397082D97069
7 changed files with 73 additions and 32 deletions

View File

@ -28,24 +28,24 @@ public class TodosController : ApiController
[JsonPropertyName("project")] [JsonPropertyName("project")]
public string? Project { get; init; } public string? Project { get; init; }
[JsonPropertyName("priority")]
public string? Priority { get; init; }
internal CreateTodoCommand To() => new( internal CreateTodoCommand To() => new(
Title, Title,
Project, Project,
Description); Description,
Priority);
} }
[HttpPost] [HttpPost]
public async Task<ActionResult<string>> CreateTodo( public async Task<ActionResult<string>> CreateTodo(
[FromBody] CreateTodoRequest request) [FromBody] CreateTodoRequest request)
{ => Ok(await Mediator.Send(request.To()));
return Ok(await Mediator.Send(request.To()));
}
[HttpGet("{todoId}")] [HttpGet("{todoId}")]
public async Task<ActionResult<TodoViewModel>> GetTodoById([FromRoute] string todoId) public async Task<ActionResult<TodoViewModel>> GetTodoById([FromRoute] string todoId)
{ => await Mediator.Send(new GetTodoByIdQuery(todoId));
return await Mediator.Send(new GetTodoByIdQuery(todoId));
}
[HttpGet] [HttpGet]
public async Task<ActionResult<IEnumerable<TodoViewModel>>> GetTodos([FromQuery] bool onlyActive = false) public async Task<ActionResult<IEnumerable<TodoViewModel>>> GetTodos([FromQuery] bool onlyActive = false)

View File

@ -42,7 +42,7 @@ public record CreateTodoCommand(
request.TodoTitle, request.TodoTitle,
request.TodoProject, request.TodoProject,
request.TodoDescription, request.TodoDescription,
request.Priority request.Priority,
userId, userId,
DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()); DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());

View File

@ -7,7 +7,8 @@ public record TodoViewModel(
string AuthorId, string AuthorId,
string? Project, string? Project,
string? Description, string? Description,
long Created long Created,
string? Priority
) )
{ {
internal static TodoViewModel From(Entities.Todo t) => new( internal static TodoViewModel From(Entities.Todo t) => new(
@ -17,5 +18,6 @@ public record TodoViewModel(
t.AuthorId, t.AuthorId,
t.Project, t.Project,
t.Description, t.Description,
t.Created); t.Created,
t.Priority);
} }

View File

@ -21,6 +21,7 @@ public class TodoRepository : ITodoRepository
string title, string title,
string? projectName, string? projectName,
string? description, string? description,
string? priority,
string userId, string userId,
long created) long created)
{ {
@ -31,7 +32,8 @@ public class TodoRepository : ITodoRepository
Status = false, Status = false,
AuthorId = userId, AuthorId = userId,
Description = description, Description = description,
Created = created Created = created,
Priority = priority
}; };
await _todosCollection.InsertOneAsync(todo); await _todosCollection.InsertOneAsync(todo);
return todo.To(); return todo.To();
@ -77,7 +79,8 @@ public class TodoRepository : ITodoRepository
ProjectName = todo.Project, ProjectName = todo.Project,
AuthorId = todo.AuthorId, AuthorId = todo.AuthorId,
Description = todo.Description, Description = todo.Description,
Created = todo.Created Created = todo.Created,
Priority = todo.Priority
}); });
return updatedTodo.To(); return updatedTodo.To();

View File

@ -11,6 +11,22 @@ interface TodoItemProps {
displayProject: boolean; 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<TodoItemProps> = (props) => { export const TodoItem: FC<TodoItemProps> = (props) => {
const [isHovering, setIsHovering] = useState(false); const [isHovering, setIsHovering] = useState(false);
const [menuIsOpen, setMenuIsOpen] = useState(false); const [menuIsOpen, setMenuIsOpen] = useState(false);
@ -42,6 +58,15 @@ export const TodoItem: FC<TodoItemProps> = (props) => {
{props.todo.description && ( {props.todo.description && (
<div className="h-3 w-3 bg-gray-100 dark:border-black border border-gray-300 dark:bg-gray-900 rounded-sm" /> <div className="h-3 w-3 bg-gray-100 dark:border-black border border-gray-300 dark:bg-gray-900 rounded-sm" />
)} )}
{props.todo.priority && (
<div
className={`rounded-md select-none text-xs p-1 text-white font-bold ${getColorFromPriority(
props.todo.priority
)}`}
>
{props.todo.priority}
</div>
)}
{!props.todo.authorId && ( {!props.todo.authorId && (
<div className="h-3 w-3 bg-red-500 rounded-sm" /> <div className="h-3 w-3 bg-red-500 rounded-sm" />
)} )}

View File

@ -2,6 +2,7 @@ import { Todo } from "@src/core/entities/todo";
import { TodoItem } from "@src/components/todos/todoItem"; import { TodoItem } from "@src/components/todos/todoItem";
import { AddTodo } from "@src/components/todos/addTodo"; import { AddTodo } from "@src/components/todos/addTodo";
import { useUpdateTodo } from "@src/presentation/hooks/socketHooks"; import { useUpdateTodo } from "@src/presentation/hooks/socketHooks";
import { useMemo } from "react";
export const TodoList = (props: { export const TodoList = (props: {
todos: Todo[]; todos: Todo[];
@ -11,17 +12,21 @@ export const TodoList = (props: {
}) => { }) => {
const { updateTodo } = useUpdateTodo(); const { updateTodo } = useUpdateTodo();
return ( const todos: Todo[] = useMemo(() => {
<> return props.todos
<ul id="inbox">
{props.todos
.filter((t) => { .filter((t) => {
if (!props.hideDone) return true; if (!props.hideDone) return true;
return t.status == false; return t.status == false;
}) })
.sort((a, b) => a.created - b.created) .sort((a, b) => a.created - b.created)
.map((t, i) => ( .sort((a, b) => b.priority.localeCompare(a.priority));
}, [props.todos, props.hideDone]);
return (
<>
<ul id="inbox">
{todos.map((t, i) => (
<li key={i}> <li key={i}>
<TodoItem <TodoItem
todo={t} todo={t}

View File

@ -6,6 +6,12 @@ export const StatusState: { done: Done; notDone: NotDone } = {
notDone: false, notDone: false,
}; };
enum Priorities {
p1 = "p1",
p2 = "p2",
p3 = "p3",
}
export interface Todo { export interface Todo {
id: string; id: string;
title: string; title: string;
@ -14,7 +20,7 @@ export interface Todo {
project?: string; project?: string;
authorId?: string; authorId?: string;
created: number; created: number;
priority?: string; priority?: Priorities;
} }
export const asTodo = (item: Todo): Todo => { export const asTodo = (item: Todo): Todo => {