Skip to content

Commit a06bb6f

Browse files
committed
persist new tasks to board
1 parent da5d1b9 commit a06bb6f

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
mutation addTaskToBoard($boardId: ID!, $title: String!, $type: String) {
2+
boardUpdate(
3+
filter: { id: $boardId }
4+
data: { tasks: { create: [{ title: $title, type: $type }] } }
5+
) {
6+
id
7+
tasks(last: 1) {
8+
items {
9+
id
10+
title
11+
createdAt
12+
updatedAt
13+
dueAt
14+
}
15+
}
16+
}
17+
}

src/pages/boards/[id].vue

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import getBoardQuery from "@/graphql/queries/board.query.gql";
77
import boardsQuery from "@/graphql/queries/boards.query.gql";
88
import deleteBoardMutation from "@/graphql/mutations/deleteBoard.mutation.gql";
99
import updateBoardMutation from "@/graphql/mutations/updateBoard.mutation.gql";
10-
import { v4 as uuidv4 } from "uuid";
10+
import addTaskToBoardMutation from "@/graphql/mutations/addTaskToBoard.mutation.gql";
1111
import { useRouter } from "vue-router";
1212
1313
const alerts = useAlerts();
@@ -24,7 +24,13 @@ const {
2424
result: boardData,
2525
loading: loadingBoard,
2626
onError: onBoardError,
27-
} = useQuery(getBoardQuery, { id: boardId.value });
27+
} = useQuery(
28+
getBoardQuery,
29+
{ id: boardId.value },
30+
{
31+
fetchPolicy: "cache-and-network",
32+
}
33+
);
2834
onBoardError(() => alerts.error("Error loading board"));
2935
const board = computed(() => boardData.value?.board || null);
3036
const tasks = computed(() => board.value?.tasks?.items);
@@ -36,8 +42,7 @@ const { mutate: updateBoard } = useMutation(updateBoardMutation);
3642
const { mutate: deleteBoard, onError: onErrorDeletingBoard } = useMutation(
3743
deleteBoardMutation,
3844
{
39-
update(cache, { data: { boardDelete } }) {
40-
console.log(boardDelete);
45+
update(cache) {
4146
cache.updateQuery({ query: boardsQuery }, (res) => ({
4247
boardsList: {
4348
items: res.boardsList.items.filter(
@@ -58,16 +63,38 @@ async function deleteBoardIfConfirmed() {
5863
}
5964
}
6065
66+
// handle create task
67+
const {
68+
mutate: addTaskToBoard,
69+
onError: onErrorCreatingTask,
70+
onDone: onDoneCreatingTask,
71+
} = useMutation(addTaskToBoardMutation);
72+
73+
// eslint-disable-next-line
74+
let taskResolve = (task: Task) => {};
75+
// eslint-disable-next-line
76+
let taskReject = (message: Error) => {};
77+
6178
function addTask(task: Task) {
6279
return new Promise((resolve, reject) => {
63-
const taskWithId = {
80+
taskResolve = resolve;
81+
taskReject = reject;
82+
addTaskToBoard({
83+
boardId: boardId.value,
6484
...task,
65-
id: uuidv4(),
66-
};
67-
tasks.value.push(taskWithId);
68-
resolve(taskWithId);
85+
});
6986
});
7087
}
88+
89+
onErrorCreatingTask((error) => {
90+
console.log(error);
91+
taskReject(error);
92+
alerts.error("Error creating task");
93+
});
94+
onDoneCreatingTask((res) => {
95+
taskResolve(res.data.boardUpdate.tasks.items[0]);
96+
alerts.success("New task created!");
97+
});
7198
</script>
7299
<template>
73100
<div v-if="board">

0 commit comments

Comments
 (0)