From c692260bda940fb34f49197b6a3097a4953423f5 Mon Sep 17 00:00:00 2001 From: Saladin21 <13519187@std.stei.itb.ac.id> Date: Mon, 21 Mar 2022 22:44:25 +0700 Subject: [PATCH] index, insert, update, delete admin --- src/views/AdminEdit.vue | 118 ++++++++++++++------------------------ src/views/AdminIndex.vue | 12 +++- src/views/AdminInsert.vue | 36 ++++++++---- 3 files changed, 77 insertions(+), 89 deletions(-) diff --git a/src/views/AdminEdit.vue b/src/views/AdminEdit.vue index fe04354..4a8cac4 100644 --- a/src/views/AdminEdit.vue +++ b/src/views/AdminEdit.vue @@ -16,32 +16,30 @@ <h1 class="text-blue">Data Admin</h1> <hr class="my-4 " style="border: 1px solid #6992b4"> </div> - <form @submit="insert" autocomplete="off" id="formAdmin"> + <form @submit="save" autocomplete="off" id="formAdmin"> <div class="row p-3"> <div class="col"> - <div class="mb-3 me-3"> - <label for="namaInput" class="text-blue"> Nama </label> - <input v-model="nama" type="text" id="namaInput" class="form-control" required> - </div> <div class="mb-3 me-3"> - <label for="usernameInput" class="text-blue"> Username </label> - <input v-model="username" type="text" id="usernameInput" class="form-control" required> - </div> - <button type="submit" class="btn-blue p-1" >Simpan</button> - <router-link :to="{ path: '/user' }" style="text-decoration: none"> - <button class="btn-red p-1">Batalkan</button> - </router-link> + <label for="usernameInput" class="text-blue"> Username </label> + <input v-model="username" type="text" id="usernameInput" class="form-control" required> + </div> + <div class="mb-3 me-3"> + <label for="passwordInput" class="text-blue"> Kata Sandi </label> + <input v-model="password" type="password" id="passwordInput" class="form-control"> + </div> + <button type="submit" class="btn-blue p-1" >Simpan</button> + <router-link :to="{ path: '/admin' }" style="text-decoration: none"> + <button class="btn-red p-1">Batalkan</button> + </router-link> + </div> <div class="col"> - <div class="mb-3"> <label for="emailInput" class="text-blue"> Email </label> <input v-model="email" type="email" id="emailInput" class="form-control" required> </div> - <div class="mb-3"> - <label for="passwordInput" class="text-blue"> Kata Sandi </label> - <input v-model="password" type="password" id="passwordInput" class="form-control"> - </div> + + </div> </div> </form> @@ -54,6 +52,7 @@ import Header from "../components/header" import Sidebar from "../components/sidebar" +import { HTTP } from '../http-common' export default { @@ -64,76 +63,43 @@ export default { }, data() { return{ - nama: "", username: "", email: "", password: "", - temp_admins: [ - { - id: 1, - nama: "Budi", - username: "budi", - email: "budi@example.com", - tanggal: "02-15-2020", - }, - { - id: 2, - nama: "Asep", - username: "asep", - email: "asep@example.com", - tanggal: "02-12-2020", - }, - { - id: 3, - nama: "Yana", - username: "yana", - email: "yana@example.com", - tanggal: "02-10-2020", - }, - { - id: 4, - nama: "Yusuf", - username: "yusuf", - email: "yusuf@example.com", - tanggal: "02-10-2020", - }, - { - id: 5, - nama: "Siti", - username: "siti", - email: "siti@example.com", - tanggal: "02-10-2020", - }, - { - id: 6, - nama: "Yuni", - username: "yuni", - email: "yuni@example.com", - tanggal: "02-10-2020", - }, - { - id: 7, - nama: "Hadi", - username: "hadi", - email: "hadi@example.com", - tanggal: "02-10-2020", - }, - ], } }, methods: { save(e){ e.preventDefault(); - let formValue = [this.nama, this.username, this.email, this.password] - console.log(formValue) - alert("Admin berhasil ditambahkan") + let data = { + username: this.username, + email: this.email, + password: null + } + if (this.password != ""){ + data.password = this.password + } + HTTP.put(`admins/${this.id}`, data, { + headers: { + 'Content-Type': 'application/json' + } + }).then((res)=>{ + if (res.status == 200){ + alert("Admin berhasil diedit") + } + }).catch(() => alert("Gagal mengedit Admin")) + }, getData(){ // di sini ambil data user dari backend - let admin = this.temp_admins[parseInt(this.id)-1] - this.nama = admin.nama - this.username = admin.username - this.email = admin.email + HTTP.get(`admins/${this.id}`).then((res)=>{ + if (res.status == 200){ + this.username = res.data.data.username + this.email = res.data.data.email + } + } + ) + } }, diff --git a/src/views/AdminIndex.vue b/src/views/AdminIndex.vue index 62fe854..ac9255e 100644 --- a/src/views/AdminIndex.vue +++ b/src/views/AdminIndex.vue @@ -161,7 +161,17 @@ export default { console.log(id); }, deleteAdmin(id) { - console.log(id); + if (confirm("Yakin akan menghapus Admin?")){ + HTTP.delete(`admins/${id}`).then((res) =>{ + if (res.status == 200){ + alert("Berhasil menghapus Admin") + this.getAdmins() + } + }).catch( () => { + alert("Gagal Menghapus Admin") + } + ) + } }, editAdmin(id) { this.$router.push({ name: "AdminEdit", params: { id: id }}); diff --git a/src/views/AdminInsert.vue b/src/views/AdminInsert.vue index bf2ec48..43b9bf9 100644 --- a/src/views/AdminInsert.vue +++ b/src/views/AdminInsert.vue @@ -19,13 +19,13 @@ <form @submit="insert" autocomplete="off" id="formAdmin"> <div class="row p-3"> <div class="col"> - <div class="mb-3 me-3"> - <label for="namaInput" class="text-blue"> Nama </label> - <input v-model="nama" type="text" id="namaInput" class="form-control" required> - </div> <div class="mb-3 me-3"> <label for="usernameInput" class="text-blue"> Username </label> <input v-model="username" type="text" id="usernameInput" class="form-control" required> + </div> + <div class="mb-3 me-3"> + <label for="passwordInput" class="text-blue"> Kata Sandi </label> + <input v-model="password" type="password" id="passwordInput" class="form-control" required> </div> <button type="submit" class="btn-blue p-1" >Simpan</button> <router-link :to="{ path: '/user' }" style="text-decoration: none"> @@ -38,10 +38,7 @@ <label for="emailInput" class="text-blue"> Email </label> <input v-model="email" type="email" id="emailInput" class="form-control" required> </div> - <div class="mb-3"> - <label for="passwordInput" class="text-blue"> Kata Sandi </label> - <input v-model="password" type="password" id="passwordInput" class="form-control" required> - </div> + </div> </div> </form> @@ -54,6 +51,7 @@ import Header from "../components/header" import Sidebar from "../components/sidebar" +import { HTTP } from '../http-common' export default { @@ -73,10 +71,24 @@ export default { methods: { insert(e){ e.preventDefault(); - let formValue = [this.nama, this.username, this.email, this.password] - console.log(formValue) - alert("Admin berhasil ditambahkan") - document.getElementById("formAdmin").reset(); + let data = { + username: this.username, + email: this.email, + password: this.password, + password_confirmation: this.password} + HTTP.post("register", data, { + headers:{ + 'Content-Type': 'application/json' + } + }).then((res)=>{ + if (res.status == 200){ + alert("Admin berhasil ditambahkan") + document.getElementById("formAdmin").reset(); + } + }).catch(()=>{ + alert("Gagal menambahkan Admin") + }) + } }, -- GitLab