`);
$(".close").on("click", function () {
$("#validation-text").html("");
});
$("#confirmDeptDelete").on("click", function () {
callApi(
"deleteDepartmentById",
"GET",
deleteConfirmation,
departmentToChange.id
);
});
}
);
$("#listDepartments").on("click", `#edit${departmentToChange.id}`, function () {
callApi("getDepartmentById", "GET", editDepartmentForm, departmentToChange.id)
})
});
$("#selectDeptLocation").html(
`
`
);
locations.forEach((location) => {
$("#selectDeptLocation").append(
`
`
);
});
$(".newDepartmentForm").show();
$("#extraInfo").modal("show");
};
const editDepartmentForm = (data) => {
let departmentToChange = data.data;
$("#validation-text").html(`
Edit department
Location
Choose Location
Save
Cancel `);
$("#locationNew").html(
`
Choose location `
);
locations.forEach((location) => {
$("#locationNew").append(
`
${location.name} `
);
});
$("#newDepartmentName").val(departmentToChange.name);
$("#locationNew").val(departmentToChange.locationID);
$(".close").on("click", function () {
$("#validation-text").html("");
});
$("#confirmEditDept").on("click", function () {
locationForEditedDepartment = $("#locationNew").val()
let departmentName = $("#newDepartmentName")
.val()
.toLowerCase()
.replace(/(\b[a-z](?!\s))/g, function (x) {
return x.toUpperCase();
})
validateField("new department", departmentName, 2, 30, callUpdateDepartment, departmentToChange.id)
})
}
$("#addNewLocation").on("click", function () {
let location = $("#newLocation")
.val()
.toLowerCase()
.replace(/(\b[a-z](?!\s))/g, function (x) {
return x.toUpperCase();
});
validateField("new location", location, 2, 20, lastLocationCheck);
});
const lastLocationCheck = (location) => {
if (locations.find((office) => office.name === location)) {
validateString = `${location} already exists within Company Directory. Duplicates are not allowed.`;
validationWarning(validateString);
} else {
callApi("insertLocation", "GET", getNewLocConfirmation, location);
}
};
const lastUpdateLocationCheck = (location, locationId) => {
if (locations.find((office) => office.name === location)) {
validateString = `${location} already exists within Company Directory. Duplicates are not allowed.`;
validationWarning(validateString);
} else {
callApi("updateLocation", "GET", getEditConfirmation, location, locationId);
}
};
const callUpdateDepartment = (department, departmentId) => {
callApi(
"updateDepartment", "GET", getEditConfirmation, department, locationForEditedDepartment, departmentId)
}
const getNewLocConfirmation = (data) => {
initialiseData();
closeModal();
$("#validation-text").html(
`
${data.data} has successfully been added to the Company Directory. Please add departments for this location.
`
);
$("#extraInfo").modal("show");
};
const getNewDeptConfirmation = (data) => {
initialiseData();
closeModal();
$("#extraInfo").modal("show");
$("#validation-text").html(
`
${data.data}
`
);
};
// Data filters
// On whole name
$("#searchNames").on("input", function (e) {
let lettersToSearch = e.currentTarget.value.toLowerCase().trim();
let searchData =
$("#selectDept").val() === "reset" && $("#selectLoc").val() === "reset"
? staticResults
: searchResults;
results = searchData.filter((result) => {
result.wholeName =
result.firstName.toLowerCase() + " " + result.lastName.toLowerCase();
return result.wholeName.includes(lettersToSearch);
});
displayStaffData(results);
});
$("#selectDept").on("change", function (e) {
searchDept = e.currentTarget.value;
if (searchDept === "reset") {
} else {
const searchData = results;
results = searchData.filter((result) => result.departmentID === searchDept);
searchResults = results;
displayStaffData(results);
}
});
$("#selectLoc").on("change", function (e) {
searchLoc = e.currentTarget.value;
if (searchLoc === "reset") {
resetData();
} else {
const searchData = results;
results = searchData.filter((result) => result.location === searchLoc);
searchResults = results;
displayStaffData(results);
}
});
const resetData = () => {
displayStaffData(staticResults);
$("#searchNames").val("");
$("#selectDept").val("reset");
$("#selectLoc").val("reset");
newEmployee = {
firstName: "",
lastName: "",
department: "Choose Department",
departmentID: "reset",
location: "Choose Location",
locationID: "reset",
email: "",
id: "not assigned",
};
};
$("#refreshButton").click(function () {
location.reload(true);
});
$("#resetButton").click(function () {
resetData();
});
$("#resetHidingButton").click(function () {
resetData();
});
$("#addOrEditStaff").click(function () {
newEmployee.firstName = $("#forename")
.val()
.toLowerCase()
.replace(/(\b[a-z](?!\s))/g, function (x) {
return x.toUpperCase();
});
validateField(
"employee's first name",
newEmployee.firstName,
2,
15,
lastNameCheck
);
});
const lastNameCheck = (firstName) => {
newEmployee.lastName = $("#surname")
.val()
.toLowerCase()
.replace(/(\b[a-z](?!\s))/g, function (x) {
return x.toUpperCase();
});
validateField(
"employee's last name",
newEmployee.lastName,
2,
20,
departmentCheck
);
};
const departmentCheck = () => {
newEmployee.departmentID = $("#modalSelectDept").val();
newEmployee.locationID = $("#modalSelectLoc").val();
if (
newEmployee.departmentID === "reset" || newEmployee.departmentID === "resetSubset" ||
newEmployee.locationID === "reset"
) {
validateString =
"The employee must be associated with a location and a department";
validationWarning(validateString);
} else {
newEmployee.email = $("#email").val().toLowerCase();
newEmployee.id !== "not assigned"
? validateField("email", newEmployee.email, 6, 40, updatePersonnel)
: validateField("email", newEmployee.email, 6, 40, emailDuplicationCheck);
}
};
const emailDuplicationCheck = () => {
if (staticResults.find((staff) => staff.email === newEmployee.email)) {
validateString =
"There is already an employee with this email address in the Company Directory. Duplicates are not allowed";
validationWarning(validateString);
} else {
callApi(
"insertEmployee",
"POST",
getAddConfirmation,
newEmployee.firstName,
newEmployee.lastName,
newEmployee.email,
newEmployee.departmentID
);
}
};
const updatePersonnel = () => {
callApi(
"updateEmployee",
"GET",
getEditConfirmation,
newEmployee.firstName,
newEmployee.lastName,
newEmployee.email,
newEmployee.departmentID,
newEmployee.id
);
};
const getAddConfirmation = (data) => {
closeModal();
initialiseData();
$("#validation-text").html(`
${data.data[0]}
`);
$("#extraInfo").modal("show");
};
const getEditConfirmation = (data) => {
closeModal();
initialiseData();
$("#validation-text").html(`
${data.data[0]}'s information has successfully been updated
`);
$("#extraInfo").modal("show");
};
const validationWarning = (validateString) => {
$("#validation-text").html("");
$("#validation-text").html(`
`);
};
const validateField = (
field,
fieldInput,
min,
max,
lastCheckCallback,
extraField
) => {
if (fieldInput.length > max || fieldInput.length < min) {
validateString = `The ${field} must be between ${min} and ${max} characters.`;
validationWarning(validateString);
} else {
validatePattern(field, fieldInput, lastCheckCallback, extraField);
}
};
const validatePattern = (field, fieldInput, lastCheckCallback, extraField) => {
if (field === "email") {
if (
!fieldInput.match(
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
)
) {
validateString = `${fieldInput} does not seem to be a regularly formatted email address.`;
validationWarning(validateString);
} else {
lastCheckCallback(fieldInput);
}
} else {
if (!fieldInput.match(/^[A-Za-z -]+$/)) {
validateString = `The ${field} must not contain any unusual characters.`;
validationWarning(validateString);
} else {
lastCheckCallback(fieldInput, extraField);
}
}
};