Commit feefefc1 authored by TTS Kieu Tuan Anh's avatar TTS Kieu Tuan Anh

update & create user CRUD

parent c8b6574d
...@@ -195,8 +195,7 @@ ...@@ -195,8 +195,7 @@
<script> <script>
import Nav from "@/components/Nav"; import Nav from "@/components/Nav";
import axios from "axios"; import axios from "axios";
import Toasted from 'vue-toasted';
import { ModalPlugin } from 'bootstrap-vue';
export default { export default {
layout: "admin", layout: "admin",
...@@ -225,6 +224,7 @@ export default { ...@@ -225,6 +224,7 @@ export default {
{ text: 'Actions', value: 'actions', sortable: false }, { text: 'Actions', value: 'actions', sortable: false },
], ],
users: [], users: [],
eIndex: [],
eID: '', eID: '',
eEmail:'', eEmail:'',
eName:'', eName:'',
...@@ -235,10 +235,8 @@ export default { ...@@ -235,10 +235,8 @@ export default {
name: "", name: "",
id: "", id: "",
email: "", email: "",
status: "",
created_at: "", created_at: "",
updated_at: "", updated_at: "",
}, },
defaultItem: { defaultItem: {
name: "", name: "",
...@@ -269,7 +267,8 @@ export default { ...@@ -269,7 +267,8 @@ export default {
}, },
methods: { methods: {
initialize() { initialize() {
this.users = []; this.users = [],
this.editedItem = {}
}, },
editItem(item) { editItem(item) {
this.editedIndex = this.users.indexOf(item); this.editedIndex = this.users.indexOf(item);
...@@ -328,10 +327,15 @@ export default { ...@@ -328,10 +327,15 @@ export default {
}) })
.then(response => { .then(response => {
// $bvModal.hide('modal-login'); // $bvModal.hide('modal-login');
this.$bvModal.hide('modal-login')
self.$toast.success('User created successfully!',{ self.$toast.success('User created successfully!',{
duration: 3000 duration: 3000
}); });
console.log(response); console.log(response)
this.editedItem = response.data.data
console.log(this.editedItem)
this.users.push(this.editedItem)
}) })
.catch(errors => { .catch(errors => {
this.$bvModal.hide('modal-login') this.$bvModal.hide('modal-login')
...@@ -343,13 +347,19 @@ export default { ...@@ -343,13 +347,19 @@ export default {
}); });
}, },
deleteUser(userID, index) { deleteUser(userID, index) {
const self = this
this.editedIndex = this.users.indexOf(index); this.editedIndex = this.users.indexOf(index);
if(confirm("Do you really want to delete?")){ if(confirm("Do you really want to delete?")){
try{ try{
axios axios
.delete(`http://127.0.0.1:8000/api/users/${userID}`) .delete(`http://127.0.0.1:8000/api/users/${userID}`)
.then(response => { .then(response => {
console.log(response.data)
this.users.splice(this.editedIndex, 1); this.users.splice(this.editedIndex, 1);
self.$toast.success('User deleted successfully!',{
duration: 3000
});
}) })
} catch(error){ } catch(error){
console.log(error) console.log(error)
...@@ -361,6 +371,7 @@ export default { ...@@ -361,6 +371,7 @@ export default {
this.eID = user.id; this.eID = user.id;
this.eEmail = user.email; this.eEmail = user.email;
this.eName = user.name; this.eName = user.name;
this.editedIndex = this.users.indexOf(user);
console.log(this?.eID); console.log(this?.eID);
}, },
updateUser(userID) { updateUser(userID) {
...@@ -379,7 +390,10 @@ export default { ...@@ -379,7 +390,10 @@ export default {
self.$toast.success('User updated successfully!',{ self.$toast.success('User updated successfully!',{
duration: 3000 duration: 3000
}); });
console.log(response); console.log(response)
this.editedItem = response.data.data
this.$bvModal.hide('modal-edit')
Object.assign(this.users[this.editedIndex], this.editedItem)
}) })
} catch(error){ } catch(error){
console.log(error) console.log(error)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment