OSDN Git Service

ヘッダーの追加削除ができるようになった master
authortkskjri05 <t.kujirai@nagatake.co.jp>
Sun, 9 May 2021 02:37:43 +0000 (11:37 +0900)
committertkskjri05 <t.kujirai@nagatake.co.jp>
Sun, 9 May 2021 02:37:43 +0000 (11:37 +0900)
src/components/todoApp.vue

index 5f1fbe0..71fcdb8 100644 (file)
@@ -155,9 +155,83 @@ export default {
         return
       }
       
+      const CREATE_TODO_HEAD = gql`mutation createHeader($id: Int!, $name: String = "") {
+                                insert_todo_header_one(object: {id: $id, name: $name}) {
+                                  id
+                                  name
+                                }
+                              }`
+
+      this.$refs.spinner.show()
+      this.$apollo.queries.todo_header_aggregate.refetch()
+      .then(() => {
+        this.selectedHeadrId = this.todo_header_aggregate.aggregate.max.id + 1
+        return this.mutateHead(CREATE_TODO_HEAD, {
+                        id: this.selectedHeadrId
+                        , name: this.titleName
+                      });
+      }).then(() => {
+        this.onHeaderChange()
+      }).catch((error) => {
+        this.$refs.snackBar.setMessageAndshow('add error!')
+        console.error(error)
+      }).finally(() => {
+        this.$refs.spinner.close()
+      })
+
     },
     deleteHeader(){
+      if(!_.isNumber(this.selectedHeadrId)){
+        this.$refs.snackBar.setMessageAndshow('タイトルが選ばれていないよ!')
+        return
+      }
 
+      const DELETE_TODO_HEAD = gql`mutation delete_todo_header($id: Int!) {
+                                    delete_todo_header_by_pk(id: $id) {
+                                      id
+                                    }
+                                  }`
+
+      this.$refs.spinner.show()
+      this.deleteTodoList(this.selectedHeadrId)
+      .then(() => {
+        return this.mutateHead(DELETE_TODO_HEAD, {id: this.selectedHeadrId})
+      }).then(() => {
+        this.selectedHeadrId = 0
+        this.titleName = ""
+      }).catch((error) => {
+        this.$refs.snackBar.setMessageAndshow('add error!')
+        console.error(error)
+      }).finally(() => {
+        this.$refs.spinner.close()
+      })
+    },
+    deleteTodoList(headerId){
+      const DELETE_TODO = gql`mutation deleteTitleMutate($header_id: Int!) {
+                                  delete_todo_list(where: {header_id: {_eq:$header_id}}) {
+                                    affected_rows
+                                  }
+                                }`
+      const promise = this.mutateApollo(DELETE_TODO, {header_id: headerId})
+      .then(() => {
+        return new Promise(resolve => {resolve()})
+      }).catch((error) => {
+        return new Promise((resolve, reject) => {reject(error)})
+      })
+      return promise
+    },
+    mutateHead(gqlMutation, variables){
+      const promise = this.$apollo.mutate({
+            mutation: gqlMutation,
+            variables: variables
+          })
+      .then(() => {
+        return this.$apollo.queries.todo_header.refetch()
+      })
+      .catch((error) => {
+        return new Promise((resolve, reject) => {reject(error)})
+      })
+      return promise
     },
     searchHeader(){
       this.$refs.spinner.show()
@@ -332,7 +406,15 @@ export default {
           }
         }
     }`,
-    
+    todo_header_aggregate: gql`query {
+        todo_header_aggregate {
+          aggregate {
+            max {
+              id
+            }
+          }
+        }
+    }`,    
   },
 }
 </script>