diff --git a/src/App.vue b/src/App.vue index 9f417f0..1570930 100644 --- a/src/App.vue +++ b/src/App.vue @@ -57,11 +57,41 @@
{{ menuSel }}

- +
+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ + + + {{ createMsg }} +
+ + -
+
Kontaktok
@@ -132,6 +162,117 @@
Tipp: duplán kattints egy mezőre a szerkesztéshez. Enter ment, Esc mégse.
+ + +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ + + + {{ createUserMsg }} +
+ + +
+
Felhasználók
+
+ + Töltés… +
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
IDFelhasználónévAdminMegjegyzésJelszó (új)Művelet
{{ u.id }} + + {{ u.uname }} + + + {{ u.admin ? 'igen' : 'nem' }} + + + {{ u.note }} + + + + + +
Nincs felhasználó.
+
+ +
+ Tipp: duplán katt egy mezőre a szerkesztéshez. A „Jelszó (új)” oszlopban megadott érték azonnal új jelszó lesz. +
+
+ +
@@ -156,6 +297,198 @@ const isLoadingContacts = ref(false) const editing = ref({ id: null, field: null }) const editBuffer = ref({ id: null, name: "", phone: "", address: "", note: "" }) +// Új kontakt űrlap state +const newContact = ref({ name: '', phone: '', address: '', note: '' }) +const isCreating = ref(false) +const createMsg = ref('') +const createOk = ref(false) + +// --- Admin / users állapot --- +const users = ref([]) +const isLoadingUsers = ref(false) +const userEditing = ref({ id: null, field: null }) +const userEditBuffer = ref({ id: null, uname: '', admin: false, note: '', pw: '' }) + +// Új user űrlap +const newUser = ref({ uname: '', pw: '', admin: false, note: '' }) +const isCreatingUser = ref(false) +const createUserMsg = ref('') +const createUserOk = ref(false) + +async function loadUsers() { + if (!userAdmin.value) return + isLoadingUsers.value = true + try { + const res = await getWithBody(`${apiBase}/users`, { + authUname: userLoginName.value, + authPw: userLoginPw.value + }) + const data = await res.json() + if (data.ok) { + users.value = (data.users || []).map(u => ({ ...u, admin: !!u.admin })) + } else { + users.value = [] + } + } catch (e) { + console.error(e) + users.value = [] + } finally { + isLoadingUsers.value = false + } +} + +async function createUser() { + if (!newUser.value.uname || !newUser.value.pw) return + isCreatingUser.value = true + createUserMsg.value = '' + try { + const res = await fetch(`${apiBase}/users`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + authUname: userLoginName.value, + authPw: userLoginPw.value, + uname: newUser.value.uname, + pw: newUser.value.pw, + admin: !!newUser.value.admin, + note: newUser.value.note || null + }) + }) + const data = await res.json() + if (data.ok) { + createUserOk.value = true + createUserMsg.value = 'Felhasználó hozzáadva' + users.value.unshift({ + id: data.id, + uname: newUser.value.uname, + admin: !!newUser.value.admin, + note: newUser.value.note || null + }) + newUser.value = { uname: '', pw: '', admin: false, note: '' } + } else { + createUserOk.value = false + createUserMsg.value = data.error || 'Hiba történt' + } + } catch (e) { + console.error(e) + createUserOk.value = false + createUserMsg.value = 'Hálózati hiba' + } finally { + isCreatingUser.value = false + } +} + +async function patchUser(id, patch) { + const res = await fetch(`${apiBase}/users`, { + method: 'PATCH', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + authUname: userLoginName.value, + authPw: userLoginPw.value, + id, + ...patch + }) + }) + return res.json() +} + +async function deleteUser(id) { + if (!confirm(`Biztosan törlöd a(z) #${id} felhasználót?`)) return + const res = await fetch(`${apiBase}/users`, { + method: 'DELETE', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + authUname: userLoginName.value, + authPw: userLoginPw.value, + id + }) + }) + const data = await res.json() + if (data.ok) { + users.value = users.value.filter(u => u.id !== id) + } +} + +function startUserEdit(row, field) { + userEditing.value = { id: row.id, field } + userEditBuffer.value = { id: row.id, uname: row.uname, admin: !!row.admin, note: row.note ?? '', pw: '' } +} + +function isUserEditing(row, field) { + return userEditing.value.id === row.id && userEditing.value.field === field +} + +async function commitUserEdit() { + if (!userEditing.value.id) return + const { id, field } = userEditing.value + let val = userEditBuffer.value[field] + + // speciális esetek + if (field === 'admin') val = !!val + if (field === 'pw') { + if (!val) { cancelUserEdit(); return } + const res = await patchUser(id, { pw: val }) + if (res.ok) userEditBuffer.value.pw = '' + userEditing.value = { id: null, field: null } + return + } + + const res = await patchUser(id, { [field]: val }) + if (res.ok) { + const idx = users.value.findIndex(u => u.id === id) + if (idx !== -1) users.value[idx][field] = val + } + userEditing.value = { id: null, field: null } +} + +function cancelUserEdit() { + userEditing.value = { id: null, field: null } + userEditBuffer.value.pw = '' +} + + +async function createContact () { + if (!newContact.value.name || !newContact.value.phone) return + isCreating.value = true + createMsg.value = '' + try { + const res = await fetch(`${apiBase}/contacts`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + authUname: userLoginName.value, + authPw: userLoginPw.value, + name: newContact.value.name, + phone: newContact.value.phone, + address: newContact.value.address || null, + note: newContact.value.note || null + }) + }) + const data = await res.json() + if (data.ok) { + createOk.value = true + createMsg.value = 'Hozzáadva' + contacts.value.unshift({ + id: data.id, + name: newContact.value.name, + phone: newContact.value.phone, + address: newContact.value.address || null, + note: newContact.value.note || null + }) + newContact.value = { name: '', phone: '', address: '', note: '' } + } else { + createOk.value = false + createMsg.value = data.error || 'Hiba történt' + } + } catch (e) { + console.error(e) + createOk.value = false + createMsg.value = 'Hálózati hiba' + } finally { + isCreating.value = false + } +} + function switchAdmin(){ pageStatus.value = "admin" menuSel.value = "Admin"