var mode = false
async function toggleDarkMode() {
let darkMode = document.getElementById("dark-mode");
darkMode.disabled = !darkMode.disabled;
mode = !mode;
if(mode){
document.getElementById('modeToggle').innerHTML = "light_mode"
document.getElementById('main_html').classList.add('uk-light')
} else {
document.getElementById('modeToggle').innerHTML = "dark_mode"
document.getElementById('main_html').classList.remove('uk-light')
}
}
if(session.user.flags.darkmode){
toggleDarkMode()
}
// Beginning of site specific code!
var recipe = [];
document.addEventListener('DOMContentLoaded', async function() {
recipe = await getRecipe()
console.log(recipe)
await replenishRecipe()
});
async function replenishRecipe() {
document.getElementById('title').innerHTML = `${recipe.name}`
//document.getElementById('breadcrumb').innerHTML = `${recipe.name}`
document.getElementById('recipeName').value = `${recipe.name}`
document.getElementById('recipeCreationDate').value = `${recipe.creation_date}`
document.getElementById('recipeAuthor').value = `${recipe.author}`
document.getElementById('recipeDescription').value = `${recipe.description}`
await replenishInstructions()
await replenishIngrediants()
await getImage()
}
async function replenishInstructions() {
let instructions_list = document.getElementById('instructions_list')
instructions_list.innerHTML = ''
for(let i = 0; i < recipe.instructions.length; i++){
let liElem = document.createElement('li')
liElem.setAttribute('class', 'instruction-card')
liElem.innerHTML = `${i+1}. ${recipe.instructions[i]}`
instructions_list.append(liElem)
}
}
async function replenishIngrediants() {
let ingrediantsTableBody = document.getElementById('ingrediantsTableBody')
ingrediantsTableBody.innerHTML = ""
for(let i=0; i< recipe.recipe_items.length; i++){
let tableRow = document.createElement('tr')
let typeCell = document.createElement('td')
typeCell.innerHTML = `${recipe.recipe_items[i].item_type}`
let nameCell = document.createElement('td')
nameCell.innerHTML = `${recipe.recipe_items[i].item_name}`
let qtyCell = document.createElement('td')
qtyCell.innerHTML = `${recipe.recipe_items[i].qty}`
let uomCell = document.createElement('td')
uomCell.innerHTML = `${recipe.recipe_items[i].uom.fullname}`
tableRow.append(typeCell, nameCell, qtyCell, uomCell)
tableRow.onclick = async function(){
await openLineItemModal(recipe.recipe_items[i])
}
ingrediantsTableBody.append(tableRow)
}
}
async function openLineItemModal(item){
document.getElementById('lineHeader').innerHTML = `Edit ${item.uuid}...`
document.getElementById('lineType').value = `${item.item_type}`
document.getElementById('lineName').value = `${item.item_name}`
document.getElementById('lineQty').value = `${item.qty}`
document.getElementById('lineWeblink').value = `${item.links.main}`
document.getElementById('lineUOM').innerHTML = ""
if(item.item_type=="sku"){
document.getElementById('lineName').classList.add('uk-disabled')
document.getElementById('lineWeblink').classList.add('uk-disabled')
let main_uom = document.createElement('option')
main_uom.value = item.item_uom.id
main_uom.innerHTML = item.item_uom.fullname
document.getElementById('lineUOM').append(main_uom)
for(let i=0; i response.blob())
.then(imageBlob => {
const imageURL = URL.createObjectURL(imageBlob);
document.getElementById('recipeImage').src = imageURL;
});
}
async function addCustomItem() {
payload = {
rp_id: recipe.id,
item_type: document.getElementById('customType').value,
item_name: document.getElementById('customName').value,
qty: document.getElementById('customQty').value,
uom: document.getElementById('customUOM').value,
links: {main: document.getElementById('customWeblink').value}
}
const response = await fetch(`/recipes/postCustomItem`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});
data = await response.json()
message_type = "primary"
if(data.error){
message_type = "danger"
}
UIkit.notification({
message: data.message,
status: message_type,
pos: 'top-right',
timeout: 5000
});
recipe = data.recipe
await replenishRecipe()
document.getElementById('customName').value = ""
document.getElementById('customQty').value = ""
document.getElementById('customUOM').value = ""
document.getElementById('customWeblink').value = ""
UIkit.modal(document.getElementById('addCustomItem')).hide();
}
async function addSKUItem(item_id) {
const response = await fetch(`/recipes/postSKUItem`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
recipe_id: recipe.id,
item_id: item_id
}),
});
data = await response.json()
message_type = "primary"
if(data.error){
message_type = "danger"
}
UIkit.notification({
message: data.message,
status: message_type,
pos: 'top-right',
timeout: 5000
});
recipe = data.recipe
await replenishRecipe()
UIkit.modal(document.getElementById('itemsModal')).hide();
}
let updated = {}
async function postUpdate() {
let description = document.getElementById('recipeDescription').value
updated.description = description
const response = await fetch(`/recipes/postUpdate`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
recipe_id: recipe_id,
update: updated
}),
});
data = await response.json()
message_type = "primary"
if(data.error){
message_type = "danger"
} else {
updated = {}
}
UIkit.notification({
message: data.message,
status: message_type,
pos: 'top-right',
timeout: 5000
});
};
async function updateImage() {
const fileInput = document.querySelector('input[type="file"]');
const formData = new FormData();
formData.append('file', fileInput.files[0]);
await fetch(`/recipes/postImage/${recipe.id}`, {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => console.log('File uploaded!', data))
.catch(error => console.error('Error:', error));
}
async function updateName() {
updated.name = document.getElementById('recipeName').value
}
async function updateDescription() {
updated.description = document.getElementById('recipeDescription').value
}
async function addInstruction() {
let instruction = document.getElementById('addInstruction').value
if(!document.getElementById('addInstruction').value==""){
let instructions = recipe.instructions.slice()
document.getElementById('addInstruction').value = ""
instructions.push(instruction)
recipe.instructions = instructions
updated.instructions = instructions
await replenishInstructions()
}
}
async function deleteInstruction(index){
recipe.instructions.splice(index, 1)
let instructions = recipe.instructions.slice()
recipe.instructions = instructions
updated.instructions = instructions
await replenishInstructions()
}
let pagination_current = 1;
let pagination_end = 10;
let search_string = "";
let items_limit = 25;
async function openSKUModal() {
let itemsModal = document.getElementById('itemsModal')
pagination_current = 1;
search_string = '';
document.getElementById('searchItemsInput').value = '';
let items = await fetchItems()
await updateItemsModalTable(items)
await updateItemsPaginationElement()
UIkit.modal(itemsModal).show();
}
async function searchItemTable(event) {
if(event.key==='Enter'){
search_string = event.srcElement.value
let items = await fetchItems()
await updateItemsModalTable(items)
await updateItemsPaginationElement()
}
}
async function setItemsPage(pageNumber){
pagination_current = pageNumber;
let items = await fetchItems()
await updateItemsModalTable(items)
await updateItemsPaginationElement()
}
async function updateItemsPaginationElement() {
let paginationElement = document.getElementById('itemsPage');
paginationElement.innerHTML = "";
// previous
let previousElement = document.createElement('li')
if(pagination_current<=1){
previousElement.innerHTML = ``;
previousElement.classList.add('uk-disabled');
}else {
previousElement.innerHTML = ``;
}
paginationElement.append(previousElement)
//first
let firstElement = document.createElement('li')
if(pagination_current<=1){
firstElement.innerHTML = `1`;
firstElement.classList.add('uk-disabled');
}else {
firstElement.innerHTML = `1`;
}
paginationElement.append(firstElement)
// ...
if(pagination_current-2>1){
let firstDotElement = document.createElement('li')
firstDotElement.classList.add('uk-disabled')
firstDotElement.innerHTML = `…`;
paginationElement.append(firstDotElement)
}
// last
if(pagination_current-2>0){
let lastElement = document.createElement('li')
lastElement.innerHTML = `${pagination_current-1}`
paginationElement.append(lastElement)
}
// current
if(pagination_current!=1 && pagination_current != pagination_end){
let currentElement = document.createElement('li')
currentElement.innerHTML = `${pagination_current}`
paginationElement.append(currentElement)
}
// next
if(pagination_current+2${pagination_current+1}`
paginationElement.append(nextElement)
}
// ...
if(pagination_current+2<=pagination_end){
let secondDotElement = document.createElement('li')
secondDotElement.classList.add('uk-disabled')
secondDotElement.innerHTML = `…`;
paginationElement.append(secondDotElement)
}
//end
let endElement = document.createElement('li')
if(pagination_current>=pagination_end){
endElement.innerHTML = `${pagination_end}`;
endElement.classList.add('uk-disabled');
}else {
endElement.innerHTML = `${pagination_end}`;
}
paginationElement.append(endElement)
//next button
let nextElement = document.createElement('li')
if(pagination_current>=pagination_end){
nextElement.innerHTML = ``;
nextElement.classList.add('uk-disabled');
}else {
nextElement.innerHTML = ``;
console.log(nextElement.innerHTML)
}
paginationElement.append(nextElement)
}
async function fetchItems() {
const url = new URL('/recipes/getItems', window.location.origin);
url.searchParams.append('page', pagination_current);
url.searchParams.append('limit', items_limit);
url.searchParams.append('search_string', search_string);
const response = await fetch(url);
data = await response.json();
pagination_end = data.end
return data.items;
}
async function updateItemsModalTable(items) {
let itemsTableBody = document.getElementById('itemsTableBody');
itemsTableBody.innerHTML = "";
for(let i=0; i < items.length; i++){
let tableRow = document.createElement('tr')
let idCell = document.createElement('td')
idCell.innerHTML = `${items[i].id}`
let barcodeCell = document.createElement('td')
barcodeCell.innerHTML = `${items[i].barcode}`
let nameCell = document.createElement('td')
nameCell.innerHTML = `${items[i].item_name}`
tableRow.id = items[i].id
tableRow.onclick = async function(){
await addSKUItem(items[i].id)
}
tableRow.append(idCell, barcodeCell, nameCell)
itemsTableBody.append(tableRow)
}
}