|
1 | 1 | <script setup lang="ts">
|
2 |
| -import { ref, toRefs } from "vue"; |
3 |
| -// import AppPageHeading from "../../components/AppPageHeading.vue"; |
4 |
| -import BoardDragAndDrop from "../../components/BoardDragAndDrop.vue"; |
5 |
| -// import { useAlerts } from "@/stores/Alerts"; |
6 |
| -// const alerts = useAlerts(); |
7 |
| -import type { Task } from "@/types"; |
| 2 | +import { toRefs, computed } from "vue"; |
| 3 | +import type { Task, Board } from "@/types"; |
| 4 | +import { useAlerts } from "@/stores/alerts"; |
| 5 | +import { useQuery, useMutation } from "@vue/apollo-composable"; |
| 6 | +import getBoardQuery from "@/graphql/queries/board.query.gql"; |
| 7 | +import boardsQuery from "@/graphql/queries/boards.query.gql"; |
| 8 | +import deleteBoardMutation from "@/graphql/mutations/deleteBoard.mutation.gql"; |
| 9 | +import updateBoardMutation from "@/graphql/mutations/updateBoard.mutation.gql"; |
8 | 10 | import { v4 as uuidv4 } from "uuid";
|
| 11 | +import { useRouter } from "vue-router"; |
9 | 12 |
|
10 |
| -const props = defineProps({ |
11 |
| - id: String, |
12 |
| -}); |
| 13 | +const alerts = useAlerts(); |
| 14 | +const router = useRouter(); |
13 | 15 |
|
| 16 | +// Define Props |
| 17 | +const props = defineProps<{ |
| 18 | + id: string; |
| 19 | +}>(); |
14 | 20 | const { id: boardId } = toRefs(props);
|
15 | 21 |
|
16 |
| -const board = ref({ |
17 |
| - id: boardId?.value || "1", |
18 |
| - title: "Let's have an amazing time at Vue.js forge!! 🍍", |
19 |
| - order: JSON.stringify([ |
20 |
| - { id: "1", title: "backlog 🌴", taskIds: ["1", "2"] }, |
21 |
| - ]), |
22 |
| -}); |
| 22 | +// Init Page Data with Board and tasks |
| 23 | +const { |
| 24 | + result: boardData, |
| 25 | + loading: loadingBoard, |
| 26 | + onError: onBoardError, |
| 27 | +} = useQuery(getBoardQuery, { id: boardId.value }); |
| 28 | +onBoardError(() => alerts.error("Error loading board")); |
| 29 | +const board = computed(() => boardData.value?.board || null); |
| 30 | +const tasks = computed(() => board.value?.tasks?.items); |
23 | 31 |
|
24 |
| -const tasks = ref<Partial<Task>[]>([ |
25 |
| - { id: "1", title: "Code like mad people!" }, |
26 |
| - { id: "2", title: "Push clean code" }, |
27 |
| -]); |
| 32 | +// handle board updates |
| 33 | +const { mutate: updateBoard } = useMutation(updateBoardMutation); |
28 | 34 |
|
29 |
| -async function addTask(task: Task) { |
| 35 | +//handle delete board |
| 36 | +const { mutate: deleteBoard, onError: onErrorDeletingBoard } = useMutation( |
| 37 | + deleteBoardMutation, |
| 38 | + { |
| 39 | + update(cache, { data: { boardDelete } }) { |
| 40 | + console.log(boardDelete); |
| 41 | + cache.updateQuery({ query: boardsQuery }, (res) => ({ |
| 42 | + boardsList: { |
| 43 | + items: res.boardsList.items.filter( |
| 44 | + (b: Board) => b.id !== boardId.value |
| 45 | + ), |
| 46 | + }, |
| 47 | + })); |
| 48 | + }, |
| 49 | + } |
| 50 | +); |
| 51 | +onErrorDeletingBoard(() => alerts.error("Error deleting board")); |
| 52 | +async function deleteBoardIfConfirmed() { |
| 53 | + const yes = confirm("Are you sure you want to delete this board?"); |
| 54 | + if (yes) { |
| 55 | + await deleteBoard({ id: boardId.value }); |
| 56 | + router.push("/"); |
| 57 | + alerts.success(`Board successfully deleted`); |
| 58 | + } |
| 59 | +} |
| 60 | +
|
| 61 | +function addTask(task: Task) { |
30 | 62 | return new Promise((resolve, reject) => {
|
31 |
| - const taskWithTheId = { |
| 63 | + const taskWithId = { |
32 | 64 | ...task,
|
33 | 65 | id: uuidv4(),
|
34 | 66 | };
|
35 |
| - tasks.value.push(taskWithTheId); |
36 |
| - resolve(taskWithTheId); |
| 67 | + tasks.value.push(taskWithId); |
| 68 | + resolve(taskWithId); |
37 | 69 | });
|
38 | 70 | }
|
39 |
| -
|
40 |
| -const updateBoard = (b) => { |
41 |
| - board.value = b; |
42 |
| - // alerts.success("Board updated!"); |
43 |
| -}; |
44 |
| -
|
45 |
| -const deleteBoardIfConfirmed = () => { |
46 |
| - console.log("delete board"); |
47 |
| -}; |
48 | 71 | </script>
|
49 |
| - |
50 | 72 | <template>
|
51 |
| - <div> |
52 |
| - <AppPageHeading> |
53 |
| - {{ board.title }} |
54 |
| - </AppPageHeading> |
55 |
| - <BoardMenu :board="board" @deleteBoard="deleteBoardIfConfirmed" /> |
| 73 | + <div v-if="board"> |
| 74 | + <div class="flex justify-between"> |
| 75 | + <AppPageHeading> |
| 76 | + {{ board.title }} |
| 77 | + </AppPageHeading> |
| 78 | + <BoardMenu :board="board" @deleteBoard="deleteBoardIfConfirmed" /> |
| 79 | + </div> |
56 | 80 |
|
57 | 81 | <BoardDragAndDrop
|
58 |
| - :tasks="tasks" |
59 | 82 | :board="board"
|
| 83 | + :tasks="tasks" |
60 | 84 | @update="updateBoard"
|
61 | 85 | :addTask="addTask"
|
62 | 86 | />
|
63 | 87 | </div>
|
| 88 | + <AppLoader v-if="loadingBoard" :overlay="true" /> |
64 | 89 | </template>
|
65 |
| - |
66 |
| -<style scoped> |
67 |
| -pre { |
68 |
| - width: 400px; |
69 |
| - overflow-x: auto; |
70 |
| - white-space: pre-wrap; |
71 |
| - word-wrap: break-word; |
72 |
| -} |
73 |
| -</style> |
|
0 commit comments