├── .eslintrc.json ├── app ├── favicon.ico ├── globals.css ├── page.tsx └── layout.tsx ├── postcss.config.js ├── lib ├── getUrl.ts ├── uploadImage.ts └── getTodosGroupedByColumn.ts ├── next.config.js ├── tailwind.config.js ├── store ├── modalStore.ts └── boardStore.ts ├── appwrite.ts ├── .gitignore ├── public ├── vercel.svg └── next.svg ├── typings.d.ts ├── tsconfig.json ├── package.json ├── LICENSE ├── components ├── Header.tsx ├── TodoCard.tsx ├── TaskTypeRadioGroup.tsx ├── Column.tsx ├── Board.tsx └── Modal.tsx └── README.md /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "next/core-web-vitals" 3 | } 4 | -------------------------------------------------------------------------------- /app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedMohsen600/Trello/HEAD/app/favicon.ico -------------------------------------------------------------------------------- /app/globals.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | -------------------------------------------------------------------------------- /postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | } 7 | -------------------------------------------------------------------------------- /lib/getUrl.ts: -------------------------------------------------------------------------------- 1 | import { storage } from '@/appwrite'; 2 | 3 | export const getUrl = async (image: Image) => { 4 | const url = storage.getFilePreview(image.bucketId, image.fileId); 5 | return url; 6 | }; 7 | -------------------------------------------------------------------------------- /next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = { 3 | images: { 4 | domains: ['cloud.appwrite.io', 'links.papareact.com'], 5 | }, 6 | }; 7 | 8 | module.exports = nextConfig; 9 | -------------------------------------------------------------------------------- /app/page.tsx: -------------------------------------------------------------------------------- 1 | import Board from '@/components/Board'; 2 | import Header from '@/components/Header'; 3 | 4 | export default function Home() { 5 | return ( 6 |
7 |
8 | 9 |
10 | ); 11 | } 12 | -------------------------------------------------------------------------------- /tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | module.exports = { 3 | mode: 'jit', 4 | purge: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'], 5 | theme: { 6 | extend: {}, 7 | }, 8 | variants: { 9 | extend: {}, 10 | }, 11 | plugins: [], 12 | }; 13 | -------------------------------------------------------------------------------- /lib/uploadImage.ts: -------------------------------------------------------------------------------- 1 | import { ID, storage } from '@/appwrite'; 2 | 3 | export const uploadImage = async (file: File) => { 4 | if (!file) return; 5 | const fileUploaded = await storage.createFile( 6 | process.env.NEXT_PUBLIC_BUCKET_ID!, 7 | ID.unique(), 8 | file 9 | ); 10 | return fileUploaded; 11 | }; 12 | -------------------------------------------------------------------------------- /store/modalStore.ts: -------------------------------------------------------------------------------- 1 | import { create } from 'zustand'; 2 | 3 | interface ModalState { 4 | isOpen: boolean; 5 | openModal: () => void; 6 | closeModal: () => void; 7 | } 8 | 9 | export const useModalStore = create()((set, get) => ({ 10 | isOpen: false, 11 | openModal: () => set({ isOpen: true }), 12 | closeModal: () => set({ isOpen: false }), 13 | })); 14 | -------------------------------------------------------------------------------- /appwrite.ts: -------------------------------------------------------------------------------- 1 | import { Storage, Account, Client, Databases, ID } from 'appwrite'; 2 | 3 | const client = new Client(); 4 | 5 | client 6 | .setEndpoint('https://cloud.appwrite.io/v1') 7 | .setProject(process.env.NEXT_PUBLIC_APPWRITER_PROJECT_ID!); 8 | 9 | const databases = new Databases(client); 10 | const account = new Account(client); 11 | const storage = new Storage(client); 12 | 13 | export { client, account, databases, storage, ID }; 14 | -------------------------------------------------------------------------------- /app/layout.tsx: -------------------------------------------------------------------------------- 1 | import Modal from '@/components/Modal'; 2 | import './globals.css'; 3 | 4 | export const metadata = { 5 | title: 'Trello', 6 | description: 'Generated by create next app', 7 | }; 8 | 9 | export default function RootLayout({ 10 | children, 11 | }: { 12 | children: React.ReactNode; 13 | }) { 14 | return ( 15 | 16 | 17 | {children} 18 | 19 | 20 | 21 | ); 22 | } 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env*.local 29 | 30 | # vercel 31 | .vercel 32 | 33 | # typescript 34 | *.tsbuildinfo 35 | next-env.d.ts 36 | -------------------------------------------------------------------------------- /public/vercel.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /typings.d.ts: -------------------------------------------------------------------------------- 1 | interface Board { 2 | columns: Map; 3 | } 4 | 5 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 6 | 7 | type TypedColumn = 'todo' | 'inprogress' | 'done'; 8 | 9 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 10 | 11 | interface Column { 12 | id: TypedColumn; 13 | todos: Todo[]; 14 | } 15 | 16 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 17 | 18 | interface Todo { 19 | $id: string; 20 | $createdAt: string; 21 | title: string; 22 | status: TypedColumn; 23 | image?: Image; 24 | } 25 | 26 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 27 | 28 | interface Image { 29 | bucketId: string; 30 | fileId: string; 31 | } 32 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": ["dom", "dom.iterable", "esnext"], 5 | "allowJs": true, 6 | "skipLibCheck": true, 7 | "strict": true, 8 | "forceConsistentCasingInFileNames": true, 9 | "noEmit": true, 10 | "esModuleInterop": true, 11 | "module": "esnext", 12 | "moduleResolution": "node", 13 | "resolveJsonModule": true, 14 | "isolatedModules": true, 15 | "jsx": "preserve", 16 | "incremental": true, 17 | "plugins": [ 18 | { 19 | "name": "next" 20 | } 21 | ], 22 | "paths": { 23 | "@/*": ["./*"] 24 | } 25 | }, 26 | "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], 27 | "exclude": ["node_modules"] 28 | } 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "treollo-app", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "dev": "next dev", 7 | "build": "next build", 8 | "start": "next start", 9 | "lint": "next lint" 10 | }, 11 | "dependencies": { 12 | "@headlessui/react": "^1.7.15", 13 | "@heroicons/react": "^2.0.18", 14 | "@types/node": "20.2.5", 15 | "@types/react": "18.2.7", 16 | "@types/react-dom": "18.2.4", 17 | "appwrite": "^11.0.0", 18 | "autoprefixer": "10.4.14", 19 | "eslint": "8.41.0", 20 | "eslint-config-next": "13.4.4", 21 | "next": "13.4.4", 22 | "postcss": "8.4.24", 23 | "react": "18.2.0", 24 | "react-avatar": "^5.0.3", 25 | "react-beautiful-dnd": "^13.1.1", 26 | "react-dom": "18.2.0", 27 | "tailwindcss": "3.3.2", 28 | "typescript": "5.0.4", 29 | "zustand": "^4.3.8" 30 | }, 31 | "devDependencies": { 32 | "@types/react-beautiful-dnd": "^13.1.4" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | SPDX short identifier: MIT 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /public/next.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lib/getTodosGroupedByColumn.ts: -------------------------------------------------------------------------------- 1 | import { databases } from '@/appwrite'; 2 | 3 | export const getTodosGroupedByColumn = async () => { 4 | const data = (await databases.listDocuments( 5 | process.env.NEXT_PUBLIC_DATABASE_ID!, 6 | process.env.NEXT_PUBLIC_TODOS_COLLECTION_ID! 7 | )) || { documents: [] }; 8 | 9 | const todos = data.documents; 10 | 11 | const columns = todos.reduce((acc, todo) => { 12 | if (!acc.get(todo.status)) { 13 | acc.set(todo.status, { 14 | id: todo.status, 15 | todos: [], 16 | }); 17 | } 18 | 19 | acc.get(todo.status)?.todos.push({ 20 | $id: todo.$id, 21 | $createdAt: todo.$createdAt, 22 | title: todo.title, 23 | status: todo.status, 24 | ...(todo.image && { image: JSON.parse(todo.image) }), 25 | }); 26 | return acc; 27 | }, new Map()); 28 | 29 | // if columns dosen't have todos inprogress, todo, and done, add them with empty todos 30 | const columnTypes: TypedColumn[] = ['todo', 'inprogress', 'done']; 31 | for (const columnType of columnTypes) { 32 | if (!columns.get(columnType)) { 33 | columns.set(columnType, { id: columnType, todos: [] }); 34 | } 35 | } 36 | const sortedColumns = new Map( 37 | Array.from(columns.entries()).sort( 38 | (a, b) => columnTypes.indexOf(a[0]) - columnTypes.indexOf(b[0]) 39 | ) 40 | ); 41 | const board: Board = { 42 | columns: sortedColumns, 43 | }; 44 | return board; 45 | }; 46 | -------------------------------------------------------------------------------- /components/Header.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import Image from 'next/image'; 4 | import React from 'react'; 5 | import { MagnifyingGlassIcon, UserCircleIcon } from '@heroicons/react/24/solid'; 6 | import Avatar from 'react-avatar'; 7 | import { useBoardStore } from '@/store/boardStore'; 8 | 9 | // ---------------------------------------------------------------- 10 | 11 | function Header() { 12 | const { searchString, setSearchString } = useBoardStore((state) => state); 13 | return ( 14 |
15 |
16 |
17 | trello-logo 24 |
25 |
26 | 27 | setSearchString(e.target.value)} 33 | /> 34 | 37 | 38 | 39 |
40 |
41 |
42 | ); 43 | } 44 | 45 | export default Header; 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Trello Clone 2 | 3 | Trello is a Trello-like web application built using Next.js, TypeScript, React Beautiful DnD for drag and drop functionality, and Zustand as state management. It provides a simple and intuitive interface for managing tasks with three columns: Todo, In Progress, and Done. Users can easily drag and drop columns and todos to organize their tasks efficiently. 4 | 5 | Screen Shot 2023-07-08 at 5 59 22 PM 6 | 7 | ## Features 8 | 9 | - Three Columns: The app consists of three columns - Todo, In Progress, and Done, representing different stages of task completion. 10 | - Drag and Drop: Users can easily drag and drop todos within columns or between columns to change their status and reorder them. 11 | - Add and Remove Todos: Users can add new todos to any column and remove existing todos when they are no longer needed. 12 | - Update Todo Status: By dragging a todo to a different column, users can update its status and move it to the corresponding stage. 13 | - Responsive Design: The app is designed to be responsive, ensuring a seamless experience across different devices and screen sizes. 14 | 15 | ## Technologies Used 16 | 17 | - [Next.js](https://nextjs.org/): A React framework for server-side rendering and building modern web applications. 18 | - [TypeScript](https://www.typescriptlang.org/): A typed superset of JavaScript that provides static type-checking. 19 | - [React Beautiful DnD](https://github.com/atlassian/react-beautiful-dnd): A library for adding drag and drop functionality to React applications. 20 | - [Zustand](https://github.com/pmndrs/zustand): A small, fast, and scalable state management library for React. 21 | 22 | ## Installation 23 | 24 | 1. Clone the repository: `git clone https://github.com/AhmedMohsen600/Trello.git` 25 | 2. Navigate to the project directory: `cd Trello` 26 | 3. Install dependencies: `npm install` or `yarn install` 27 | 4. Start the development server: `npm run dev` or `yarn dev` 28 | 5. Open your browser and visit `http://localhost:3000` to see the app. 29 | 30 | ## Contributing 31 | 32 | Contributions are welcome! If you have any ideas, improvements, or bug fixes, please submit a pull request. Ensure that your code adheres to the project's coding standards and includes appropriate tests. 33 | 34 | ## License 35 | 36 | This project is licensed under the [MIT License](LICENSE). 37 | 38 | ## Acknowledgments 39 | 40 | - The Trello Clone app is inspired by the concept of the original [Trello](https://trello.com/) application. 41 | - Special thanks to the creators and maintainers of the open-source libraries used in this project. 42 | -------------------------------------------------------------------------------- /components/TodoCard.tsx: -------------------------------------------------------------------------------- 1 | import { getUrl } from '@/lib/getUrl'; 2 | import { useBoardStore } from '@/store/boardStore'; 3 | import { XCircleIcon } from '@heroicons/react/24/solid'; 4 | import Image from 'next/image'; 5 | import { useEffect, useState } from 'react'; 6 | import { 7 | DraggableProvidedDraggableProps, 8 | DraggableProvidedDragHandleProps, 9 | } from 'react-beautiful-dnd'; 10 | 11 | // ------------------------------------------------------- 12 | 13 | interface TodoCardProps { 14 | todo: Todo; 15 | imgUrl?: string; 16 | index: number; 17 | id: TypedColumn; 18 | innerRef: (element: HTMLElement | null) => void; 19 | draggableProps: DraggableProvidedDraggableProps; 20 | dragHandleProps?: DraggableProvidedDragHandleProps | null; 21 | } 22 | 23 | // ------------------------------------------------------- 24 | 25 | function TodoCard({ 26 | todo, 27 | index, 28 | innerRef, 29 | id, 30 | dragHandleProps, 31 | draggableProps, 32 | }: TodoCardProps) { 33 | const deleteTodo = useBoardStore((state) => state.deleteTodo); 34 | const [imageUrl, setImageUrl] = useState(null); 35 | const [loading, setLoading] = useState(false); 36 | 37 | // --------------------------------------- 38 | 39 | useEffect(() => { 40 | if (!todo.image) return; 41 | const fetchImage = async () => { 42 | try { 43 | setLoading(true); 44 | const url = await getUrl(todo.image!); 45 | setImageUrl(url.toString()); 46 | setLoading(false); 47 | } catch (error) { 48 | setLoading(false); 49 | } 50 | }; 51 | 52 | fetchImage(); 53 | }, [todo, loading]); 54 | 55 | // --------------------------------------- 56 | 57 | if (loading) { 58 | return ( 59 |
60 |

61 | Loading... 62 |

63 |
64 | ); 65 | } 66 | 67 | return ( 68 |
74 |
75 |

{todo.title}

76 | 82 |
83 | {imageUrl && ( 84 |
85 | Task image 92 |
93 | )} 94 |
95 | ); 96 | } 97 | 98 | export default TodoCard; 99 | -------------------------------------------------------------------------------- /components/TaskTypeRadioGroup.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { useBoardStore } from '@/store/boardStore'; 4 | import { RadioGroup } from '@headlessui/react'; 5 | import { CheckCircleIcon } from '@heroicons/react/24/solid'; 6 | 7 | // -------------------------------------------- 8 | 9 | interface RadioProps { 10 | id: string; 11 | name: string; 12 | description: string; 13 | color: string; 14 | } 15 | 16 | // -------------------------------------------- 17 | 18 | const types: RadioProps[] = [ 19 | { 20 | id: 'todo', 21 | name: 'Todo', 22 | description: 'A new task to be completed', 23 | color: 'bg-red-500', 24 | }, 25 | { 26 | id: 'inprogress', 27 | name: 'In Progress', 28 | description: 'A task that is currently being working on', 29 | color: 'bg-yellow-500', 30 | }, 31 | { 32 | id: 'done', 33 | name: 'Done', 34 | description: 'A task that has been completed', 35 | color: 'bg-green-500', 36 | }, 37 | ]; 38 | 39 | // -------------------------------------------- 40 | 41 | export default function TaskTypeRadioGroup() { 42 | const { newTaskType, setNewTaskType } = useBoardStore((state) => state); 43 | 44 | return ( 45 |
46 |
47 | setNewTaskType(e)}> 48 |
49 | {types.map(({ id, color, name, description }) => ( 50 | 54 | `${ 55 | active 56 | ? 'ring-10 ring-white ring-opacity-60 ring-offset-sky-300' 57 | : '' 58 | } 59 | 60 | ${checked ? `${color} bg-opacity-75 text-white` : 'bg-white'} 61 | relative flex cursor-pointer rounded-lg px-5 py-4 shadow-md focus:outline-none` 62 | } 63 | > 64 | {({ checked }) => ( 65 |
66 |
67 |
68 | 74 | {name} 75 | 76 | 82 | {description} 83 | 84 |
85 |
86 | {checked && ( 87 |
88 | 89 |
90 | )} 91 |
92 | )} 93 |
94 | ))} 95 |
96 |
97 |
98 |
99 | ); 100 | } 101 | -------------------------------------------------------------------------------- /components/Column.tsx: -------------------------------------------------------------------------------- 1 | import { useBoardStore } from '@/store/boardStore'; 2 | import { useModalStore } from '@/store/modalStore'; 3 | import { PlusCircleIcon } from '@heroicons/react/24/solid'; 4 | import { Draggable, Droppable } from 'react-beautiful-dnd'; 5 | import TodoCard from './TodoCard'; 6 | 7 | // ------------------------------------------------- 8 | 9 | interface ColumnProps { 10 | id: TypedColumn; 11 | index: number; 12 | todos: Todo[]; 13 | } 14 | 15 | // ------------------------------------------------- 16 | 17 | const idToColumnText: { 18 | [Key in TypedColumn]: string; 19 | } = { 20 | todo: 'To Do', 21 | inprogress: 'In Progress', 22 | done: 'Done', 23 | }; 24 | 25 | // ------------------------------------------------- 26 | 27 | function Column({ id, index, todos }: ColumnProps) { 28 | const { searchString, setNewTaskType } = useBoardStore((state) => state); 29 | const openModal = useModalStore((state) => state.openModal); 30 | // if searchString is empty or undefined then return all todos else return todos that only includes the searchString 31 | const filteredTodos = todos.filter( 32 | (todo) => 33 | !searchString || 34 | todo.title.toLowerCase().includes(searchString.toLowerCase()) 35 | ); 36 | 37 | const handleModal = () => { 38 | openModal(); 39 | setNewTaskType(id); 40 | }; 41 | 42 | return ( 43 | 44 | {(provider) => ( 45 |
50 | 51 | {(provider, snapshot) => ( 52 |
59 |

60 | {idToColumnText[id]} 61 | 62 | {filteredTodos.length} 63 | 64 |

65 |
66 | {filteredTodos.map((todo, index) => ( 67 | 72 | {(provider) => ( 73 | 82 | )} 83 | 84 | ))} 85 | {provider.placeholder} 86 |
87 | 93 |
94 |
95 |
96 | )} 97 |
98 |
99 | )} 100 |
101 | ); 102 | } 103 | 104 | export default Column; 105 | -------------------------------------------------------------------------------- /components/Board.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { useBoardStore } from '@/store/boardStore'; 4 | import { useEffect } from 'react'; 5 | import { DragDropContext, Droppable, DropResult } from 'react-beautiful-dnd'; 6 | import Column from './Column'; 7 | 8 | function Board() { 9 | const { board, getBoard, setBoardState, updateTodoInDB } = useBoardStore( 10 | (state) => state 11 | ); 12 | 13 | useEffect(() => { 14 | getBoard(); 15 | }, [getBoard]); 16 | 17 | const handleOnDragEnd = (result: DropResult) => { 18 | const { destination, type, source } = result; 19 | 20 | if (!destination) return; 21 | 22 | // -------------------------------- 23 | 24 | if (type === 'column') { 25 | const entries = Array.from(board.columns.entries()); 26 | const movedColumn = entries.splice(source.index, 1)[0]; 27 | entries.splice(destination.index, 0, movedColumn); 28 | setBoardState({ 29 | ...board, 30 | columns: new Map(entries), 31 | }); 32 | } 33 | 34 | // -------------------------------- 35 | 36 | const columns = Array.from(board.columns); 37 | const startColIndex = columns[Number(source.droppableId)]; 38 | const endColIndex = columns[Number(destination.droppableId)]; 39 | 40 | // -------------------------------- 41 | 42 | const startCol: Column = { 43 | id: startColIndex[0], 44 | todos: startColIndex[1].todos, 45 | }; 46 | 47 | const endCol: Column = { 48 | id: endColIndex[0], 49 | todos: endColIndex[1].todos, 50 | }; 51 | 52 | // -------------------------------- 53 | 54 | if (!startCol || !endCol) return; 55 | if (source.index === destination.index && startCol === endCol) return; 56 | 57 | // -------------------------------- 58 | 59 | const newTodos = startCol.todos; 60 | const [movedTodo] = newTodos.splice(source.index, 1); 61 | 62 | // -------------------------------- 63 | 64 | if (startCol.id === endCol.id) { 65 | newTodos.splice(destination.index, 0, movedTodo); 66 | 67 | const newCol: Column = { 68 | id: startCol.id, 69 | todos: newTodos, 70 | }; 71 | 72 | const newColumns = board.columns.set(startCol.id, newCol); 73 | 74 | setBoardState({ 75 | ...board, 76 | columns: newColumns, 77 | }); 78 | } else { 79 | const finishTodos = endCol.todos; 80 | finishTodos.splice(destination.index, 0, movedTodo); 81 | const newColumns = board.columns; 82 | const newCol: Column = { 83 | id: startCol.id, 84 | todos: newTodos, 85 | }; 86 | 87 | newColumns.set(startCol.id, newCol); 88 | newColumns.set(endCol.id, { 89 | id: endCol.id, 90 | todos: finishTodos, 91 | }); 92 | updateTodoInDB(movedTodo, endCol.id); 93 | setBoardState({ 94 | ...board, 95 | columns: newColumns, 96 | }); 97 | } 98 | }; 99 | 100 | return ( 101 | 102 | 103 | {(provider) => ( 104 |
109 | {Array.from(board.columns.entries()).map(([id, column], index) => ( 110 | 111 | ))} 112 |
113 | )} 114 |
115 |
116 | ); 117 | } 118 | 119 | export default Board; 120 | 121 | // type CartItem = { 122 | // id: number; 123 | // qty: number; 124 | // }; 125 | 126 | // const cartItems: CartItem[] = [ 127 | // { id: 1, qty: 0 }, 128 | // { id: 2, qty: 0 }, 129 | // { id: 3, qty: 0 }, 130 | // ]; 131 | 132 | // const cartQty = cartItems.reduce((qty, item) => item.qty + qty, 0); 133 | -------------------------------------------------------------------------------- /store/boardStore.ts: -------------------------------------------------------------------------------- 1 | import { databases, storage, ID } from '@/appwrite'; 2 | import { getTodosGroupedByColumn } from '@/lib/getTodosGroupedByColumn'; 3 | import { uploadImage } from '@/lib/uploadImage'; 4 | 5 | import { create } from 'zustand'; 6 | 7 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 8 | 9 | interface BoardState { 10 | board: Board; 11 | searchString: string; 12 | newTaskInput: string; 13 | newTaskType: TypedColumn; 14 | image: File | null; 15 | 16 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 17 | 18 | getBoard: () => void; 19 | updateTodoInDB: (todo: Todo, columnId: TypedColumn) => void; 20 | addTodo: (todo: string, columnId: TypedColumn, image?: File | null) => void; 21 | deleteTodo: (todoIndex: number, todo: Todo, id: TypedColumn) => void; 22 | setBoardState: (board: Board) => void; 23 | setSearchString: (searchString: string) => void; 24 | setNewTaskInput: (input: string) => void; 25 | setNewTaskType: (columnId: TypedColumn) => void; 26 | setImage: (image: File | null) => void; 27 | } 28 | 29 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 30 | 31 | export const useBoardStore = create((set, get) => ({ 32 | board: { 33 | columns: new Map(), 34 | }, 35 | searchString: '', 36 | newTaskInput: '', 37 | newTaskType: 'todo', 38 | image: null, 39 | 40 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 41 | 42 | setSearchString: (searchString: string) => set({ searchString }), 43 | setNewTaskInput: (newTaskInput: string) => set({ newTaskInput }), 44 | setNewTaskType: (columnId: TypedColumn) => set({ newTaskType: columnId }), 45 | setImage: (image: File | null) => set({ image }), 46 | getBoard: async () => { 47 | const board = await getTodosGroupedByColumn(); 48 | set({ board }); 49 | }, 50 | 51 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 52 | 53 | addTodo: async (todo: string, columnId: TypedColumn, image?: File | null) => { 54 | const fileUploaded = image && (await uploadImage(image)); 55 | const file = fileUploaded 56 | ? { 57 | bucketId: fileUploaded.bucketId, 58 | fileId: fileUploaded.$id, 59 | } 60 | : undefined; 61 | 62 | const { $id } = await databases.createDocument( 63 | process.env.NEXT_PUBLIC_DATABASE_ID!, 64 | process.env.NEXT_PUBLIC_TODOS_COLLECTION_ID!, 65 | ID.unique(), 66 | { 67 | title: todo, 68 | status: columnId, 69 | ...(file && { image: JSON.stringify(file) }), 70 | } 71 | ); 72 | set({ newTaskInput: '' }); 73 | 74 | set((state) => { 75 | const newColumns = new Map(state.board.columns); 76 | const newTodo: Todo = { 77 | $id, 78 | $createdAt: new Date().toISOString(), 79 | title: todo, 80 | status: columnId, 81 | ...(file && { image: file }), 82 | }; 83 | const column = newColumns.get(columnId); 84 | // if there is not column create one and add the new todo inside it 85 | if (!column) 86 | newColumns.set(columnId, { 87 | id: columnId, 88 | todos: [newTodo], 89 | }); 90 | else newColumns.get(columnId)?.todos.push(newTodo); 91 | return { 92 | board: { columns: newColumns }, 93 | }; 94 | }); 95 | }, 96 | 97 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 98 | 99 | deleteTodo: async (todoIndex: number, todo: Todo, id: TypedColumn) => { 100 | const newColumns = get().board.columns; 101 | newColumns.get(id)?.todos.splice(todoIndex, 1); 102 | set({ board: { columns: newColumns } }); 103 | if (todo.image) 104 | await storage.deleteFile(todo.image.bucketId, todo.image.fileId); 105 | 106 | await databases.deleteDocument( 107 | process.env.NEXT_PUBLIC_DATABASE_ID!, 108 | process.env.NEXT_PUBLIC_TODOS_COLLECTION_ID!, 109 | todo.$id 110 | ); 111 | }, 112 | 113 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 114 | 115 | setBoardState: (board) => set({ board }), 116 | updateTodoInDB: async (todo, columnId) => { 117 | await databases.updateDocument( 118 | process.env.NEXT_PUBLIC_DATABASE_ID!, 119 | process.env.NEXT_PUBLIC_TODOS_COLLECTION_ID!, 120 | todo.$id, 121 | { 122 | title: todo.title, 123 | status: columnId, 124 | } 125 | ); 126 | }, 127 | })); 128 | -------------------------------------------------------------------------------- /components/Modal.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { FormEvent, Fragment, useRef } from 'react'; 4 | import { Dialog, Transition } from '@headlessui/react'; 5 | import { useModalStore } from '@/store/modalStore'; 6 | import { useBoardStore } from '@/store/boardStore'; 7 | import TaskTypeRadioGroup from './TaskTypeRadioGroup'; 8 | import Image from 'next/image'; 9 | import { PhotoIcon } from '@heroicons/react/24/solid'; 10 | 11 | export default function Modal() { 12 | const imagePickerRef = useRef(null); 13 | const { isOpen, closeModal } = useModalStore((state) => state); 14 | const { 15 | newTaskInput, 16 | setNewTaskInput, 17 | setImage, 18 | image, 19 | newTaskType, 20 | addTodo, 21 | } = useBoardStore((state) => state); 22 | const handleSubmit = (e: FormEvent) => { 23 | e.preventDefault(); 24 | if (!newTaskInput.trim()) return; 25 | addTodo(newTaskInput, newTaskType, image); 26 | setImage(null); 27 | closeModal(); 28 | }; 29 | return ( 30 | 31 | 37 | 46 |
47 | 48 |
49 |
50 | 59 | 60 | 64 | Add Task 65 | 66 |
67 | setNewTaskInput(e.target.value)} 70 | placeholder='Enter a task here...' 71 | type='text' 72 | className='w-full border border-gray-300 rounded-md outline-none p-5' 73 | /> 74 |
75 | 76 |
77 | 85 | {image && ( 86 | Uploaded image setImage(null)} 93 | /> 94 | )} 95 | { 100 | // if not image return 101 | if (!e.target.files![0].type.startsWith('image/')) return; 102 | setImage(e.target.files![0]); 103 | }} 104 | /> 105 |
106 |
107 | 114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 | ); 122 | } 123 | --------------------------------------------------------------------------------