From 952ef392c3075099871240828ccfaba81dfba27d Mon Sep 17 00:00:00 2001 From: Mulia Nasution Date: Mon, 5 Jun 2023 11:58:31 +0700 Subject: [PATCH] Save user profile --- src/auth.js | 1 + src/views/profile-page.vue | 82 +++++++++++++++++++++++++++++++------- 2 files changed, 68 insertions(+), 15 deletions(-) diff --git a/src/auth.js b/src/auth.js index d8adace..62024f0 100644 --- a/src/auth.js +++ b/src/auth.js @@ -27,6 +27,7 @@ export default { const data = response.data; this._user = { + userId: data.data.userId, username: data.data.username, email: data.data.email, role: data.data.role, diff --git a/src/views/profile-page.vue b/src/views/profile-page.vue index 32cc313..39700b0 100644 --- a/src/views/profile-page.vue +++ b/src/views/profile-page.vue @@ -3,6 +3,7 @@

Lihat Profil

+
@@ -39,8 +44,12 @@ import { DxForm, DxSimpleItem, DxGroupItem, + DxButtonItem, // DxLabel, } from 'devextreme-vue/form'; +import notify from 'devextreme/ui/notify'; + +const baseUrl = process.env.VUE_APP_ROOT_API; export default { props: { @@ -48,6 +57,16 @@ export default { }, data() { return { + formData: { + nama: '', + username: '', + email: '', + roleId: null, + bidangId: null, + jabatanId: null, + instansiId: null, + phone: '', + }, bidangOptions: { items: ['Bidang 1', 'Bidang 2', 'Bidang 3'], searchEnabled: true, @@ -65,20 +84,35 @@ export default { } } }, + methods: { + handleSubmit(e) { + e.preventDefault(); + + fetch(`${baseUrl}/users/${this.formData.userId}`, { + method: 'PUT', + body: JSON.stringify(this.formData), + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, + }) + .then((res) => res.json()) + .then(() => { + notify({ + message: 'Profil telah disimpan.', + position: { + my: 'center top', + at: 'center top', + }, + }, 'success', 3000); + }) + }, + }, setup() { const picture = "images/employees/02.png"; const imageSrc = `https://js.devexpress.com/Demos/WidgetsGallery/JSDemos/${picture}`; - const formData = { - nama: 'Sample Name', - username: 'sample', - email: 'sample@yopmail.com', - role_id: 1, - bidang_id: 1, - jabatan_id: 1, - instansi_id: 1, - phone: '08123456789', - }; + const colCountByScreen = { xs: 1, sm: 2, @@ -88,7 +122,6 @@ export default { return { imageSrc, - formData, colCountByScreen }; }, @@ -96,6 +129,25 @@ export default { DxForm, DxSimpleItem, DxGroupItem, + DxButtonItem, + }, + + mounted() { + const user = JSON.parse(window.localStorage.getItem('user')); + this.formData.userId = user.userId; + + fetch(`${baseUrl}/users/${user.userId}`) + .then((res) => res.json()) + .then((json) => { + this.formData.nama = json.nama; + this.formData.username = json.username; + this.formData.email = json.email; + this.formData.phone = json.phone; + this.formData.roleId = json.roleId; + this.formData.bidangId = json.bidangId; + this.formData.instansiId = json.instansiId; + this.formData.jabatanId = json.jabatanId; + }) } };