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-64,ITG-41,ITG-42] Routing and GET task implementation #6

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions bode/bode/resources/tasks/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from flask.views import MethodView
from flask_smorest import Blueprint
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.exc import DataError

from bode.models.task import Task
from bode.resources.tasks.schemas import TaskSchema
Expand All @@ -23,6 +24,13 @@ def post(self, task_data):

@blueprint.route("/<task_id>")
class TasksById(MethodView):
@blueprint.response(200, TaskSchema)
def get(self, task_id):
try:
return Task.query.get(task_id) or abort(404)
except DataError:
abort(404)
Comment on lines +27 to +32
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. if a correct UUID is passed -> returns a result if it exists. If not, responds with 404 (line 30)
  2. if an incorrect UUID is passed -> DataError is caught and also responds with 404 (line 32)

@bkulawska please, test if your implementation of DELETE works. I got some errors in an abort function when I tried to pass message kwarg as you did.


@blueprint.response(200, TaskSchema)
def delete(self, task_id):
try:
Expand Down
36 changes: 29 additions & 7 deletions cabra/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cabra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"react-dom": "^17.0.2",
"react-hook-form": "^7.28.1",
"react-query": "^3.34.18",
"react-router": "^6.2.2",
"react-router-dom": "^6.3.0",
"vite": "^2.6.14",
"yup": "^0.32.11"
},
Expand Down
24 changes: 9 additions & 15 deletions cabra/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import { QueryClient, QueryClientProvider } from "react-query";
import tw, { styled } from "twin.macro";
import { Route, Routes } from "react-router-dom";

import RandomGoat from "./components/RandomGoat";
import TaskForm from "./components/TaskForm";
import TasksList from "./components/TasksList";
import HomePage from "./pages/Home";
import NotFoundPage from "./pages/NotFound";
import TaskDetailsPage from "./pages/TaskDetails";

const queryClient = new QueryClient();
const Container = styled.div`
${tw`flex flex-col items-center h-screen py-10 overflow-auto max-h-screen`}
${tw`bg-gradient-to-b from-blue-700 to-fuchsia-500`}
`;

export default function App() {
return (
<QueryClientProvider client={queryClient}>
<Container>
<div tw="w-[90%] max-w-xl space-y-6 relative z-10">
<TaskForm />
<TasksList />
</div>
</Container>
<RandomGoat />
<Routes>
<Route path="/" element={<HomePage />}></Route>
<Route path="/task/:id" element={<TaskDetailsPage />}></Route>
<Route path="*" element={<NotFoundPage />}></Route>
</Routes>
</QueryClientProvider>
);
}
15 changes: 0 additions & 15 deletions cabra/src/components/Task.tsx

This file was deleted.

5 changes: 4 additions & 1 deletion cabra/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import App from "./App";
import { BrowserRouter } from "react-router-dom";
import GlobalStyles from "./styles/GlobalStyles";
import ReactDOM from "react-dom";
import { StrictMode } from "react";
Expand All @@ -10,7 +11,9 @@ axios.defaults.headers.common["Content-Type"] = "application/json";
ReactDOM.render(
<StrictMode>
<GlobalStyles />
<App />
<BrowserRouter>
<App />
</BrowserRouter>
</StrictMode>,
document.getElementById("root")
);
18 changes: 18 additions & 0 deletions cabra/src/pages/Home.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import "twin.macro";

import Layout from "./components/Layout";
import RandomGoat from "./components/RandomGoat";
import TaskForm from "./components/TaskForm";
import TasksList from "./components/TasksList";

export default function HomePage() {
return (
<Layout>
<div tw="w-[90%] max-w-xl space-y-6 relative z-10">
<TaskForm />
<TasksList />
</div>
<RandomGoat />
</Layout>
);
}
14 changes: 14 additions & 0 deletions cabra/src/pages/NotFound.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import "twin.macro";

import Layout from "./components/Layout";
import { Link } from "react-router-dom";
import RandomGoat from "./components/RandomGoat";

export default function NotFoundPage() {
return (
<Layout>
<Link to="/">Take me home</Link>
<RandomGoat />
</Layout>
);
}
36 changes: 36 additions & 0 deletions cabra/src/pages/TaskDetails.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import "twin.macro";

import { useNavigate, useParams } from "react-router-dom";

import { ITask } from "../types/task";
import Layout from "./components/Layout";
import Task from "./components/Task";
import axios from "axios";
import { useQuery } from "react-query";

export default function TaskDetailsPage() {
const navigate = useNavigate();
const { id } = useParams();
const { data, isLoading } = useQuery(
["task", id],
() => axios.get<ITask>(`/tasks/${id}`),
{ onError: () => navigate("/not-found", { replace: true }), retry: 1 }
);

if (isLoading) return <Layout>Loading</Layout>;

return (
<Layout>
<button tw="text-purple-800 font-black" onClick={() => navigate(-1)}>
{"<"} go back
</button>
{data ? (
<div tw="w-[90%] max-w-xl space-y-6 relative z-10">
<Task task={data.data} detailsLink={false}></Task>
</div>
) : (
<div>Loading</div>
)}
</Layout>
);
}
8 changes: 8 additions & 0 deletions cabra/src/pages/components/Layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import tw, { styled } from 'twin.macro';

const Layout = styled.div`
${tw`flex flex-col items-center h-screen py-10 overflow-auto max-h-screen`}
${tw`bg-gradient-to-b from-blue-700 to-fuchsia-500`}
`;

export default Layout;
26 changes: 26 additions & 0 deletions cabra/src/pages/components/Task.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import "twin.macro";

import { ITask } from "../../types/task";
import { Link } from "react-router-dom";

interface Props {
task: ITask;
detailsLink: boolean;
}

export default function Task({ task, detailsLink }: Props) {
return (
<div tw="rounded-xl w-full bg-white shadow-2xl text-blue-800 text-xl p-4 md:text-2xl flex justify-between items-center">
{task.title}
{detailsLink ? (
<Link
tw="underline text-blue-600 text-lg hover:text-purple-800"
to={`/task/${task.id}` }
state={{ task }}
>
Details
</Link>
) : null}
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SubmitHandler, useForm } from "react-hook-form";
import tw, { styled } from "twin.macro";
import { useMutation, useQueryClient } from "react-query";

import { ITask } from "../types/task";
import { ITask } from "../../types/task";
import axios from "axios";
import { yupResolver } from "@hookform/resolvers/yup";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import tw, { styled } from "twin.macro";

import { ITask } from "../types/task";
import { ITask } from "../../types/task";
import Task from "./Task";
import axios from "axios";
import { useQuery } from "react-query";
Expand All @@ -20,7 +20,7 @@ export default function TasksList() {
return (
<Container>
{tasks.map((task) => (
<Task key={task.id} task={task} />
<Task key={task.id} task={task} detailsLink />
))}
</Container>
);
Expand Down