Skip to content

Commit

Permalink
Added default Project label message if none exist'
Browse files Browse the repository at this point in the history
  • Loading branch information
pacholoamit committed Jul 4, 2022
1 parent 8c1cfa8 commit b8d0d3a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 41 deletions.
17 changes: 0 additions & 17 deletions api/pkg/repositories/tasks.repositories.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package repositories

import (
"errors"

"github.com/pacholoamit/GO-TASK-MGR/pkg/dto"
"github.com/pacholoamit/GO-TASK-MGR/pkg/models"
"gorm.io/gorm/clause"
Expand All @@ -13,26 +11,11 @@ type task struct{}
var Task task

func (task) CreateTask(t *dto.Task) (*dto.Task, error) {
var projectModel models.Project

if err := db.Create(&t).Error; err != nil {
return t, err
}

if t.ProjectID != 0 {

if err := db.Clauses(clause.Returning{}).Find(&projectModel, t.ProjectID).Error; err != nil {
return t, err
}

if projectModel.ID == 0 {
return t, errors.New("project does not exist")
}

db.Model(&projectModel).Association("Tasks").Append(&t)

}

return t, nil
}

Expand Down
7 changes: 7 additions & 0 deletions api/pkg/services/tasks.services.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ func (task) GetAllTasks() (*dto.Tasks, error) {

func (task) CreateTask(t *dto.Task) (*dto.Task, error) {
createdTask, err := repositories.Task.CreateTask(t)

if t.ProjectID != 0 {
if _, err := repositories.Project.AssignTaskToProject(int(createdTask.ID), int(t.ProjectID)); err != nil {
return createdTask, err
}
return createdTask, err
}
if err != nil {
fmt.Println("Error when Creating a task:", err)
return createdTask, err
Expand Down
16 changes: 0 additions & 16 deletions web/components/layout/layout-header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,10 @@ const ColorSchemeToggle = () => {
);
};

const HomeButton = () => {
return (
<Link href="/">
<ActionIcon
size={32}
variant="light"
color={"teal"}
title="Navigate to home"
>
<Home size={24} />
</ActionIcon>
</Link>
);
};

const HeaderOptions = () => {
return (
<Group>
<ColorSchemeToggle />
<HomeButton />
</Group>
);
};
Expand Down
15 changes: 12 additions & 3 deletions web/components/layout/layout-navbar/sections/actions.section.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import { Navbar, Title } from "@mantine/core";
import { useRouter } from "next/router";
import React from "react";
import { FolderPlus, Note } from "tabler-icons-react";
import { FolderPlus, Home, Note } from "tabler-icons-react";
import useCreateProjectModal from "../../../../hooks/useCreateProjectModal";
import useTaskContext from "../../../../hooks/useTaskContext";

import NavbarButton from "../navbar.button";

const ActionsSection: React.FC = () => {
const { newTask } = useTaskContext();
const { push } = useRouter();
const { openCreateProjectModal } = useCreateProjectModal();

const onClick = () => newTask();
const handleNewTask = () => newTask();
const handleHome = () => push("/");
return (
<Navbar.Section mx="-x" px="xs">
<Title order={3}>Actions</Title>
<NavbarButton
onClick={onClick}
onClick={handleHome}
icon={<Home size={16} />}
color={"green"}
label={"Home"}
/>
<NavbarButton
onClick={handleNewTask}
icon={<Note size={16} />}
color={"red"}
label={"New task"}
Expand Down
10 changes: 8 additions & 2 deletions web/components/tasks/task-drawer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ const TaskDrawer: React.FC = () => {
label: project.name,
}));

const projectLabel = `Project ${
!projectSelectOptions?.length
? "(Create a project to assign this task)"
: ""
}`;

React.useEffect(() => {
form.setValues(currentTask ?? initialValues);
if (mut.isError)
Expand Down Expand Up @@ -135,8 +141,8 @@ const TaskDrawer: React.FC = () => {
)}
</Group>
<Select
label="Project"
data={projectSelectOptions || []}
label={projectLabel}
data={projectSelectOptions || [""]}
size="md"
variant="unstyled"
{...form.getInputProps("projectId")}
Expand Down
2 changes: 1 addition & 1 deletion web/hooks/useCreateOrUpdateTask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const useCreateOrUpdateTask = () => {
// Revalidate all tasks of the assigned project from Task request
const projectId = variables.projectId?.toString() || null;

if (!projectId) revalidate(getAllTasksEndpoint);
revalidate(getAllTasksEndpoint);
revalidate(getAllTasksByProjectEndpoint(projectId as string));
},
});
Expand Down
4 changes: 2 additions & 2 deletions web/hooks/useDeleteTask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const useDeleteTask = () => {
const { mutate: revalidate } = useSWRConfig();
return useMutation((id: string) => deleteTask(id), {
onSuccess: (data, variables, context) => {
const projectId = data.projectId?.toString() || null;
if (!projectId) revalidate(getAllTasksEndpoint);
const projectId = data.projectId?.toString();
revalidate(getAllTasksByProjectEndpoint(projectId as string));
revalidate(getAllTasksEndpoint);
},
});
};
Expand Down

0 comments on commit b8d0d3a

Please sign in to comment.