Skip to content

Commit

Permalink
Merge pull request #3 from iankityadav/prod
Browse files Browse the repository at this point in the history
Prod
  • Loading branch information
iankityadav authored Nov 26, 2023
2 parents c81e2de + 4d5aace commit 2e1dffb
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 65 deletions.
4 changes: 3 additions & 1 deletion app/dashboard/components/AddTodo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { useAppSelector } from "@/app/store";
import { XMarkIcon } from "@heroicons/react/24/outline";
import { useState } from "react";

const apiUrl = process.env.NEXT_PUBLIC_API_URL;

const AddTodo = (
{ flag, setFlag, addTodoFlag, setAddTodoFlag
}: { flag: boolean, setFlag: any, addTodoFlag: boolean, setAddTodoFlag: any }) => {
Expand All @@ -18,7 +20,7 @@ const AddTodo = (
e.preventDefault();
setValues({ title: "", desc: "", isComplete: false })
try {
const res = await fetch(`http://localhost:3000/users/${user?.id}/todos`, {
const res = await fetch(`${apiUrl}/users/${user?.id}/todos`, {
method: "POST",
headers: { "Content-Type": "application/json", "Authorization": "Bearer " + token },
body: JSON.stringify(values),
Expand Down
151 changes: 90 additions & 61 deletions app/dashboard/components/TodoContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,99 +1,128 @@
"use client"
import { useAppSelector } from '@/app/store'
import { useRouter } from 'next/navigation'
import React, { useEffect, useState } from 'react'
import TodoList from './TodoList'
import AddTodo from './AddTodo'
import { Todo } from '@/app/models/todo'
import { PlusCircleIcon } from '@heroicons/react/24/solid'
"use client";
import { useAppSelector } from "@/app/store";
import { useRouter } from "next/navigation";
import React, { useEffect, useState } from "react";
import TodoList from "./TodoList";
import AddTodo from "./AddTodo";
import { Todo } from "@/app/models/todo";
import { PlusCircleIcon } from "@heroicons/react/24/solid";

const apiUrl = process.env.NEXT_PUBLIC_API_URL;

const TodoContainer = () => {
const isAuth = useAppSelector((state) => state.auth.isAuth)
const router = useRouter()
const user = useAppSelector((state) => state.auth.user)
const token = useAppSelector((state) => state.auth.token)
const isAuth = useAppSelector((state) => state.auth.isAuth);
const router = useRouter();
const user = useAppSelector((state) => state.auth.user);
const token = useAppSelector((state) => state.auth.token);
const [todos, setTodos] = useState<Todo[]>([]);
const [flag, setFlag] = useState<boolean>(false);
const [addTodoFlag, setAddTodoFlag] = useState<boolean>(false);

const fetchTodoList = async () => {
if (user?.id === undefined) return []
if (user?.id === undefined) return [];
try {
const res = await fetch(`http://localhost:3000/users/${user?.id}/todos`, {
const res = await fetch(`${apiUrl}/users/${user?.id}/todos`, {
method: "GET",
headers: { "Content-Type": "application/json", "Authorization": "Bearer " + token },
mode: 'cors',
})
const data: Todo[] = await res.json()
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
mode: "cors",
});
const data: Todo[] = await res.json();
console.log(data);
setTodos(data)
setTodos(data);
} catch (error) {
console.error(error);
}
return []
}
return [];
};

useEffect(() => {
fetchTodoList()
}, [flag])

if (!isAuth) {
router.push('/login')
return null
}
if (!isAuth) {
router.push("/login");
}
fetchTodoList();
}, [flag]);

const deleteTodo = async (todo: Todo) => {
console.log("deleteTodo -- ");
try {
const res = await fetch(`http://localhost:3000/users/${user?.id}/todos?where=` + encodeURIComponent(JSON.stringify({
id: "" + todo.id
})), {
method: "DELETE",
headers: { "Content-Type": "application/json", "Authorization": "Bearer " + token },
mode: 'cors',
})
const data = await res.json()
const res = await fetch(
`${apiUrl}/users/${user?.id}/todos?where=` +
encodeURIComponent(
JSON.stringify({
id: "" + todo.id,
})
),
{
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
mode: "cors",
}
);
const data = await res.json();
console.log(data);
setTodos(todos.filter((e, i) => e.id != todo.id))
setFlag(!flag)
setTodos(todos.filter((e, i) => e.id != todo.id));
setFlag(!flag);
} catch (error) {
console.error(error);
}
}
};

const completeTodo = async (todo: Todo) => {
console.log("completeTodo -- ");
try {
todo.isComplete = true;
const res = await fetch(`http://localhost:3000/users/${user?.id}/todos?where=` + encodeURIComponent(JSON.stringify({
id: "" + todo.id
})), {
method: "PATCH",
headers: { "Content-Type": "application/json", "Authorization": "Bearer " + token },
body: JSON.stringify(todo),
mode: 'cors',
})
const data = await res.json()
const res = await fetch(
`${apiUrl}/users/${user?.id}/todos?where=` +
encodeURIComponent(
JSON.stringify({
id: "" + todo.id,
})
),
{
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
body: JSON.stringify(todo),
mode: "cors",
}
);
const data = await res.json();
console.log(data);
setFlag(!flag)
setFlag(!flag);
} catch (error) {
console.error(error);
}
}
};

return (
<div className="h-screen flex flex-col justify-center items-center">
{addTodoFlag ?
<AddTodo flag={flag} setFlag={setFlag}
addTodoFlag={addTodoFlag} setAddTodoFlag={setAddTodoFlag} >
</AddTodo> :
{addTodoFlag ? (
<AddTodo
flag={flag}
setFlag={setFlag}
addTodoFlag={addTodoFlag}
setAddTodoFlag={setAddTodoFlag}
></AddTodo>
) : (
<button onClick={() => setAddTodoFlag(!addTodoFlag)}>
<PlusCircleIcon className='w-12 h-12 m-2 text-indigo-500 fixed right-4 bottom-4 shadow-md'></PlusCircleIcon>
<PlusCircleIcon className="w-12 h-12 m-2 text-indigo-500 fixed right-4 bottom-4 shadow-md"></PlusCircleIcon>
</button>
}
<TodoList todos={todos} completeTodo={completeTodo} deleteTodo={deleteTodo}></TodoList>
</div >
)
}
)}
<TodoList
todos={todos}
completeTodo={completeTodo}
deleteTodo={deleteTodo}
></TodoList>
</div>
);
};

export default TodoContainer
export default TodoContainer;
4 changes: 3 additions & 1 deletion app/login/components/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { useRouter } from "next/navigation";
import { useState } from "react";
import Swal from "sweetalert2";

const apiUrl = process.env.NEXT_PUBLIC_API_URL;

const LoginForm = () => {
const isAuth = useAppSelector((state) => state.auth.isAuth)
const router = useRouter()
Expand All @@ -22,7 +24,7 @@ const LoginForm = () => {
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
try {
const res = await fetch("http://localhost:3000/users/login", {
const res = await fetch(`${apiUrl}/users/login`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(values),
Expand Down
4 changes: 3 additions & 1 deletion app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { useAppDispatch, useAppSelector } from "./store";
import { setUser } from "./store/auth/auth.slice";
import { User } from "./models/user";

const apiUrl = process.env.NEXT_PUBLIC_API_URL;

export default function Home() {
const isAuth = useAppSelector((state) => state.auth.isAuth);
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(true);
Expand All @@ -15,7 +17,7 @@ export default function Home() {

const loadProfile = async () => {
try {
const res = await fetch("http://localhost:3000/whoAmI", {
const res = await fetch(`${apiUrl}/whoAmI`, {
method: "POST",
headers: { "Content-Type": "application/json", "Authorization": "Bearer " + token },
body: '',
Expand Down
4 changes: 3 additions & 1 deletion app/signup/components/SignUpContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { useRouter } from "next/navigation"
import { useState } from "react"
import Swal from "sweetalert2"

const apiUrl = process.env.NEXT_PUBLIC_API_URL;

const SignUpContainer = () => {
const isAuth = useAppSelector((state) => state.auth.isAuth)
const router = useRouter()
Expand All @@ -29,7 +31,7 @@ const SignUpContainer = () => {
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
try {
const res = await fetch("http://localhost:3000/users/signup", {
const res = await fetch(`${apiUrl}/users/signup`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(values),
Expand Down

0 comments on commit 2e1dffb

Please sign in to comment.