Skip to content

Commit

Permalink
Merge pull request #164 from AnthonyDeSantiago/users
Browse files Browse the repository at this point in the history
Add Add User Functionality
  • Loading branch information
AnthonyDeSantiago authored Dec 5, 2023
2 parents 95ff7f7 + a496a61 commit abf652c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
9 changes: 7 additions & 2 deletions public/admin_table_all_users.html
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,13 @@ <h5 class="modal-title" id="addAccountModalLabel">Create New User</h5>
</div>

<div class="form-group">
<label for="email">Address:</label>
<input type="email" class="form-control" id="address" name="email" required>
<label for="address">Address:</label>
<input type="text" class="form-control" id="address" name="address" required>
</div>

<div class="form-group">
<label for="dob">Date of Birth:</label>
<input type="text" class="form-control" id="dob" name="dob" required>
</div>

<div class="form-group">
Expand Down
13 changes: 13 additions & 0 deletions public/js/database_module.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,16 @@ export async function deleteDocument(collectionName, docID) {
console.error("Error deleting document", error);
}
}


export async function addNewUser(data) {
const usersCollection = collection(db, 'users');
addDoc(usersCollection, data)
.then((docRef) => {
console.log('Document written with ID: ', docRef.id);
return docRef.id;
})
.catch((error) => {
console.error('Error adding document: ', error);
});
}
55 changes: 51 additions & 4 deletions public/js/users_page.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { changeFieldValue, convertBalanceToFloat, deleteDocument, formatNumberToCurrency, getDocReferencesWithValue, getDocsWithValue, getDocumentReference, getFieldValue } from "./database_module.mjs";
import { addNewUser, changeFieldValue, convertBalanceToFloat, deleteDocument, formatNumberToCurrency, getDocReferencesWithValue, getDocsWithValue, getDocumentReference, getFieldValue } from "./database_module.mjs";

console.log("users_page.js has loaded");

Expand All @@ -17,6 +17,13 @@ const suspendButon = document.getElementById('suspend-button');
const dropDownButton = document.getElementById('dropdownMenuButton');
const dropDownMenu = document.getElementById('role-dropdown');

const userName = document.getElementById('username');
const firstName = document.getElementById('firstName');
const lastName = document.getElementById('lastName');
const email = document.getElementById('email');
const address = document.getElementById('address');
const dob = document.getElementById('dob');

approveButton.setAttribute('disabled', 'true');
createUserButton.setAttribute('disabled', 'true');

Expand Down Expand Up @@ -122,7 +129,7 @@ $('#addUserModal').on('hidden.bs.modal', function () {
$('#commentField').val('');
$('#commentError').text('');
createUserButton.setAttribute('disabled', 'true');
createUserDropDown.textContent = "Select a Role";
createUserDropDownButton.textContent = "Select a Role";
});


Expand Down Expand Up @@ -161,6 +168,18 @@ createUserDropDownButton.addEventListener('click', async () => {
createUserDropDown.classList.toggle('show');
});

createUserDropDown.addEventListener('click', function (event) {
if (event.target.classList.contains('dropdown-item')) {
selectedRole = event.target.textContent;
createUserDropDownButton.textContent = selectedRole;
console.log('Selected Role: ', selectedRole);
}

if (selectedRole != null) {
createUserButton.removeAttribute('disabled');
}
});

dropDownMenu.addEventListener('click', function (event) {
if (event.target.classList.contains('dropdown-item')) {
selectedRole = event.target.textContent;
Expand All @@ -171,7 +190,7 @@ dropDownMenu.addEventListener('click', function (event) {
if (selectedRole != null) {
approveButton.removeAttribute('disabled');
}
})
});

window.addEventListener('click', function (event) {
if (!event.target.matches('.btn-primary')) {
Expand Down Expand Up @@ -208,4 +227,32 @@ addUserButton.addEventListener('click', function() {
$('#addUserModal').modal('show');
});


createUserButton.addEventListener('click', async () => {
const userNameValue = userName.value;
const firstNameValue = firstName.value;
const lastNameValue = lastName.value;
const emailValue = email.value;
const addressValue = address.value;
const dobValue = dob.value;

const data = {
username: userNameValue,
firstName: firstNameValue,
lastName: lastNameValue,
email: emailValue,
address: addressValue,
role: selectedRole,
answer1: "default",
answer2: "default",
approved: true,
avatar: "assets/panda.png",
password: "Sprout1234!",
suspended: false
};
console.log("UserData: ", data);

await addNewUser(data);
setTimeout(function () {
location.reload();
}, 500);
});

0 comments on commit abf652c

Please sign in to comment.