From 2f9b97d1a4ec559350b1b6139583000708adade3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 23 Apr 2023 15:55:31 +0200 Subject: [PATCH] solve community member edit with forms --- .../js/communities/community_members.js | 47 +------------------ public/static/js/rvr.js | 10 +++- src/Controller/CommunityController.php | 7 +-- views/communities/community_members.php | 33 +++++++++---- 4 files changed, 37 insertions(+), 60 deletions(-) diff --git a/public/static/js/communities/community_members.js b/public/static/js/communities/community_members.js index 6a3ecd2..35506a9 100644 --- a/public/static/js/communities/community_members.js +++ b/public/static/js/communities/community_members.js @@ -1,5 +1,5 @@ (function () { - const element = document.getElementById('new_member_user_id'); + const element = document.getElementById('newMember').elements['user_id']; const choices = new Choices(element, { noResultsText: 'No users found', noChoicesText: 'Start typing to search users' @@ -13,50 +13,5 @@ element.addEventListener('choice', function () { choices.setChoices([], 'value', 'label', true); - document.getElementById('new_member_button').disabled = false; }); - - document.getElementById('new_member_button').addEventListener('click', function () { - document.getElementById('loading').style.visibility = 'visible'; - - let data = new FormData(); - data.append('user_id', document.getElementById('new_member_user_id').value); - - RVR.httpRequest('POST', newMemberUrl, function () { - window.location.reload(); - }, data); - }); - - const ownerCheckboxesButtons = document.getElementsByClassName('member_owner'); - for (const ownerCheckboxesButton of ownerCheckboxesButtons) { - ownerCheckboxesButton.addEventListener('change', function () { - document.getElementById('loading').style.visibility = 'visible'; - - let data = new FormData(); - data.append('community_member_id', this.dataset.id); - data.append('owner', this.checked ? 1 : 0); - - RVR.httpRequest('POST', editMemberUrl, function () { - document.getElementById('loading').style.visibility = 'hidden'; - - if (!this.response.success) { - ownerCheckboxesButton.checked = !ownerCheckboxesButton.checked; - } - }, data); - }); - }; - - const deleteButtons = document.getElementsByClassName('delete_member'); - for (const deleteButton of deleteButtons) { - deleteButton.addEventListener('click', function () { - document.getElementById('loading').style.visibility = 'visible'; - - let data = new FormData(); - data.append('community_member_id', this.dataset.id); - - RVR.httpRequest('POST', deleteMemberUrl, function () { - window.location.reload(); - }, data); - }); - }; })(); diff --git a/public/static/js/rvr.js b/public/static/js/rvr.js index d8bc101..45096bb 100644 --- a/public/static/js/rvr.js +++ b/public/static/js/rvr.js @@ -163,8 +163,14 @@ var RVR = { for (var i = 0; i < observedInputs.length; i++) { var input = form.elements[observedInputs[i]]; - if (input.defaultValue !== input.value) { - anyChanged = true; + if (input.type === 'checkbox') { + if (input.defaultChecked !== input.checked) { + anyChanged = true; + } + } else { + if (input.defaultValue !== input.value) { + anyChanged = true; + } } } diff --git a/src/Controller/CommunityController.php b/src/Controller/CommunityController.php index 8f76ff5..101b5c0 100644 --- a/src/Controller/CommunityController.php +++ b/src/Controller/CommunityController.php @@ -96,6 +96,7 @@ class CommunityController implements IAuthenticationRequired $communityMember = new CommunityMember(); $communityMember->setCommunity($community); $communityMember->setUser($user); + $communityMember->setOwner((bool)\Container::$request->post('owner')); \Container::$persistentDataManager->saveToDb($communityMember); return new JsonContent(['success' => true]); @@ -107,14 +108,14 @@ class CommunityController implements IAuthenticationRequired return null; } - $communityMember = $this->communityMemberRepository->getById(\Container::$request->post('community_member_id')); + $communityMember = $this->communityMemberRepository->getById(\Container::$request->query('community_member_id')); if ($communityMember->getUserId() === \Container::$request->user()->getUniqueId()) { return new JsonContent([ 'error' => ['errorText' => 'Own user cannot be edited.'] ]); } - $communityMember->setOwner(\Container::$request->post('owner')); + $communityMember->setOwner((bool)\Container::$request->post('owner')); \Container::$persistentDataManager->saveToDb($communityMember); return new JsonContent(['success' => true]); @@ -126,7 +127,7 @@ class CommunityController implements IAuthenticationRequired return null; } - $communityMember = $this->communityMemberRepository->getById(\Container::$request->post('community_member_id')); + $communityMember = $this->communityMemberRepository->getById(\Container::$request->query('community_member_id')); if ($communityMember->getUserId() === \Container::$request->user()->getUniqueId()) { return new JsonContent([ 'error' => ['errorText' => 'Own user cannot be deleted.'] diff --git a/views/communities/community_members.php b/views/communities/community_members.php index ddb9d12..fd35325 100644 --- a/views/communities/community_members.php +++ b/views/communities/community_members.php @@ -18,15 +18,33 @@ getUserId() !== Container::$request->user()->getUniqueId(); ?> - getUser()->getDisplayName() ?> - getOwner() ? 'checked' : '' ?> /> - + +
+
+ getUser()->getDisplayName() ?> + + + getOwner() ? 'checked' : '' ?> /> + + + + + + - + + + + + + + @@ -35,8 +53,5 @@ @section(pageScript) @endsection -- 2.45.2