app.post('/menu', (req,res) => {
// connection.query('SELECT * FROM menu', (err, result) => {
// if(err) {
// console.error(err);
// return;
// }
// const index = result.reduce((acc, row) => ({...acc, [row.id]: row}), {});
// var menu = [];
// for(const row of result) {
// if(row.parent_id === 0) {
// menu.push(row);
// continue;
// }
// const parent = index[row.parent_id];
// if(!parent) {
// console.warn(`Undefined parent with id ${row.parent_id}`);
// continue;
// }
// if(!parent.children) {
// parent.children = [];
// }
// parent.children.push(row);
// }
// });
var dataToSendToClient = {'message': 'error message from server'};
// convert whatever we want to send (preferably should be an object) to JSON
var JSONdata = JSON.stringify(dataToSendToClient);
res.send(JSONdata);
})
$(document).on('click',(e) => {
function imgName() {
var filename = null;
$.ajax({
async: false,
url: '/menu',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
type: 'post',
success: function(res){
filename = res;
}
});
return filename;
}
alert(imgName())
})
$(document).on('click',(e) => {
function viewMenu() {
var menu = null;
$.ajax({
async: false,
url: '/menu',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: '',
type: 'post',
success: function(res){
menu = makeMenuLevel(res);
}
});
return menu;
}
console.log(viewMenu())
})
app.get('/menu', (req,res) => {
res.sendFile(__dirname + '/menu.html')
})
app.post('/menu', (req,res) => {
connection.query('SELECT * FROM menu', (err, result) => {
if(err) {
console.error(err);
return;
}
const index = result.reduce((acc, row) => ({...acc, [row.id]: row}), {});
var menu = [];
for(const row of result) {
if(row.parent_id === 0) {
menu.push(row);
continue;
}
const parent = index[row.parent_id];
if(!parent) {
console.warn(`Undefined parent with id ${row.parent_id}`);
continue;
}
if(!parent.children) {
parent.children = [];
}
parent.children.push(row);
}
});
})
io.on('menu', (data) => {
let menu = makeMenuLevel(data)
$('nav').append(menu)
})
function makeMenuLevel(menuItems) {
return `<ul>${menuItems.map(
item =>`<li>${item.title}${item.children ? makeMenuLevel(item.children) : ''}</li>`;
).join('')}</ul>`;
}
io.on('menu', (data) => {
makeMenuLevel(data)
})