Skip to content

Commit

Permalink
Fix category creation
Browse files Browse the repository at this point in the history
  • Loading branch information
megastary committed Mar 10, 2024
1 parent 69aac92 commit dca618c
Show file tree
Hide file tree
Showing 7 changed files with 449 additions and 453 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
## ✨ Novinky

## 🐞 Opravy chyb

- 🎨 Kategorie se v nabídce zobrazují ihned po vytvoření, není je potřeba ještě dodatečně upravit přes formulář Upravit kategorii
27 changes: 10 additions & 17 deletions public/javascripts/edit_category.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
// Handles category to edit selection
function loadCategoryInfo() {
// Get selected category
const categoryId = document.getElementById('category_id').value
const categoryId = document.getElementById("category_id").value;

// Reset form if category deselected
if (categoryId === '') {
document.getElementById('admin_edit_category').reset()
return
if (categoryId === "") {
document.getElementById("admin_edit_category").reset();
return;
}

// Name
document.getElementById('category_name').value =
json_data[categoryId].category_name
document.getElementById("category_name").value =
json_data[categoryId].category_name;

// Color
document.getElementById('category_color').value =
json_data[categoryId].category_color
document.getElementById("category_color").value =
json_data[categoryId].category_color;

// Disable
document.getElementById('category_disabled').checked =
json_data[categoryId].category_disabled
document.getElementById("category_disabled").checked =
json_data[categoryId].category_disabled;
}

// document.addEventListener('DOMContentLoaded', function () {
// // Load image when browser navigates back to this page
// if (document.getElementById('category_id').value) {
// loadCategoryInfo()
// }
// })
153 changes: 77 additions & 76 deletions routes/admin/edit_category.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
import { Router } from 'express'
import { ensureAuthenticated } from '../../functions/ensureAuthenticated.js'
import Category from '../../models/category.js'
import csrf from 'csurf'
import logger from '../../functions/logger.js'
var router = Router()
var csrfProtection = csrf()
router.use(csrfProtection)
import { Router } from "express";
import { ensureAuthenticated } from "../../functions/ensureAuthenticated.js";
import Category from "../../models/category.js";
import csrf from "csurf";
import logger from "../../functions/logger.js";
var router = Router();
var csrfProtection = csrf();
router.use(csrfProtection);

/* GET edit category page. */
router.get('/', ensureAuthenticated, function (req, res) {
router.get("/", ensureAuthenticated, function (req, res) {
if (!req.user.admin) {
logger.warn(
`server.routes.editcategory.get__User tried to access admin page without permission.`,
{
metadata: {
result: req.user
}
result: req.user,
},
}
)
res.redirect('/')
return
);
res.redirect("/");
return;
}
if (req.session.alert) {
var alert = req.session.alert
delete req.session.alert
var alert = req.session.alert;
delete req.session.alert;
}
Category.find()
.sort([['name', 1]])
.sort([["name", 1]])
.then((categories) => {
logger.debug(
`server.routes.editcategory.get__Successfully loaded ${categories?.length} categories.`,
{
metadata: {
result: categories
}
result: categories,
},
}
)
);

res.render('admin/edit_category', {
title: 'Upravit kategorii | Lednice IT',
res.render("admin/edit_category", {
title: "Upravit kategorii | Lednice IT",
categories: categories,
user: req.user,
alert: alert,
csrfToken: req.csrfToken()
})
csrfToken: req.csrfToken(),
});
})
.catch((err) => {
logger.error(
`server.routes.editcategory.get__Failed to find categories.`,
{
metadata: {
error: err.message
}
error: err.message,
},
}
)
})
})
);
});
});

/* POST edit category form handle. */
router.post('/', ensureAuthenticated, function (req, res) {
router.post("/", ensureAuthenticated, function (req, res) {
if (!req.user.admin) {
logger.warn(
`server.routes.editcategory.post__User tried to access admin page without permission.`,
{
metadata: {
result: req.user
}
result: req.user,
},
}
)
res.redirect('/')
return
);
res.redirect("/");
return;
}

Category.findById(req.body.category_id)
Expand All @@ -78,25 +78,26 @@ router.post('/', ensureAuthenticated, function (req, res) {
if (category.name !== req.body.category_name) {
logger.info(
`server.routes.editcategory.post__Changing category's name:[${category.name}] to new name:[${req.body.category_name}].`
)
category.name = req.body.category_name
);
category.name = req.body.category_name;
}

// Handle color change
if (category.color !== req.body.category_color) {
logger.info(
`server.routes.editcategory.post__Changing category's color:[${category.color}] to new color:[${req.body.category_color}].`
)
category.color = req.body.category_color
);
category.color = req.body.category_color;
}

// Handle disabled state change - either sets disabled to true or deletes field
// Filter for other pages should use Category.find({ disabled: { $exists: false } })
// Handle disabled state change - either sets disabled to true or false
// Filter for other pages should use Category.find({ disabled: { $in: [null, false] } })
if (category.disabled !== req.body.category_disabled) {
const category_state = req.body.category_disabled ? true : false;
logger.info(
`server.routes.editcategory.post__Changing category's state:[${category.disabled}] to :[${req.body.category_disabled}].`
)
category.disabled = req.body.category_disabled
`server.routes.editcategory.post__Changing category's state:[${category.disabled}] to :[${category_state}].`
);
category.disabled = category_state;
}

// Save changes
Expand All @@ -107,58 +108,58 @@ router.post('/', ensureAuthenticated, function (req, res) {
`server.routes.editcategory.post__User:[${req.user.email}] updated category:[${req.body.category_name}] .`,
{
metadata: {
result: category
}
result: category,
},
}
)
);
const alert = {
type: 'success',
type: "success",
message: `Kategorie ${req.body.category_name} upravena v databázi.`,
success: 1
}
req.session.alert = alert
res.redirect('/edit_category')
success: 1,
};
req.session.alert = alert;
res.redirect("/edit_category");
})
.catch((err) => {
logger.error(
`server.routes.editcategory.post__Failed to save edited category to database.`,
{
metadata: {
error: err.message
}
error: err.message,
},
}
)
);
const alert = {
type: 'danger',
component: 'db',
type: "danger",
component: "db",
message: err.message,
danger: 1
}
req.session.alert = alert
res.redirect('/edit_category')
return
})
danger: 1,
};
req.session.alert = alert;
res.redirect("/edit_category");
return;
});
})
.catch((err) => {
// Handle Category.FindById error
logger.error(
`server.routes.editcategory.post__Failed to find category to edit by ID:[${req.body.category_id}] in the database.`,
{
metadata: {
error: err.message
}
error: err.message,
},
}
)
);
var alert = {
type: 'danger',
component: 'db',
type: "danger",
component: "db",
message: err.message,
danger: 1
}
req.session.alert = alert
res.redirect('/edit_category')
return
})
})
danger: 1,
};
req.session.alert = alert;
res.redirect("/edit_category");
return;
});
});

export default router
export default router;
Loading

0 comments on commit dca618c

Please sign in to comment.