diff --git a/src/auth/middleware.js b/src/auth/middleware.js index 3c12f61..5c24807 100644 --- a/src/auth/middleware.js +++ b/src/auth/middleware.js @@ -1,7 +1,7 @@ const status = require("../util/statuses"); const session = require("express-session"); -// Get all user routes names +// Get all names of user routes let userRouter = require("../router/user")((req, res, next) => next(), null, null, null); userRouter.stack.shift(); const userRoutes = userRouter.stack.map((layer) => layer.route.path.split("/")[1]); @@ -12,8 +12,9 @@ const userRoutes = userRouter.stack.map((layer) => layer.route.path.split("/")[1 */ const createMiddleware = (users) => { return async (req, res, next) => { - // Redirect not found pages to 404 page - if (!userRoutes.includes(req.url.split("/")[1])) { + // Check if incoming request route exists in user routes + // redirect to 404 if not + if (!userRoutes.includes(req.url.split("/")[1].split("?")[0])) { return res.status(status.NotFound).redirect("/notFound"); } diff --git a/src/public/scripts/assetManager.js b/src/public/scripts/assetManager.js index 86ddd08..16bbc37 100644 --- a/src/public/scripts/assetManager.js +++ b/src/public/scripts/assetManager.js @@ -8,6 +8,12 @@ const assetForms = [ * resetAll asset creation forms */ function resetAll() { + document.getElementById("dropdown-icon-button").innerHTML = ` + Other + + + + `; document.getElementById("create-other-asset-form").reset(); document.getElementById("create-saving-asset-form").reset(); document.getElementById("create-stock-asset-form").reset(); @@ -40,6 +46,7 @@ function resetRadio() { const assetKeys = { other: [ "name", + "dropdown-icon-button", "value", "description", "purchaseDate", @@ -59,8 +66,17 @@ const assetKeys = { /** * lockAsset prevents edits to asset view modal * @param {string} assetId + * @param {string} icon to defualt to */ -function lockAsset(assetId) { +function lockAsset(assetId, icon) { + let dropdown = document.getElementById(`dropdown-icon-button-${assetId}`); + if (dropdown) dropdown.innerHTML = ` + ${icon} + + + + `; + document.getElementById(`${assetId}-form`).reset(); const type = document.getElementById(`type-${assetId}`).value; @@ -74,14 +90,15 @@ function lockAsset(assetId) { document.getElementById(`save-${assetId}`).classList.add("cursor-not-allowed"); document.getElementById(`edit-${assetId}`).innerHTML = "Edit"; - document.getElementById(`edit-${assetId}`).onclick = () => { unlockAsset(assetId) }; + document.getElementById(`edit-${assetId}`).onclick = () => { unlockAsset(assetId, icon) }; } /** * unlockAsset allows edits to asset view modal * @param {string} assetId + * @param {string} icon to defualt to */ -function unlockAsset(assetId) { +function unlockAsset(assetId, icon) { const type = document.getElementById(`type-${assetId}`).value; assetKeys[type].forEach((key) => { @@ -92,8 +109,40 @@ function unlockAsset(assetId) { document.getElementById(`save-${assetId}`).classList.remove("cursor-not-allowed"); document.getElementById(`save-${assetId}`).classList.add("cursor-pointer"); - document.getElementById(`edit-${assetId}`).innerHTML = "Cancel changes"; - document.getElementById(`edit-${assetId}`).onclick = () => { lockAsset(assetId) }; + document.getElementById(`edit-${assetId}`).innerHTML = "Cancel"; + document.getElementById(`edit-${assetId}`).onclick = () => { lockAsset(assetId, icon) }; +} + +/** + * autoOpenCreate checks if popup param + * in url to auto open create popup + */ +function autoOpenCreate() { + const query = window.location.search; + const params = new URLSearchParams(query); + + if (params.has("popup")) { + document.getElementById('create-asset-modal').showModal(); + } +} + +/** + * selectIcon updated selected icon while creating + * or modifying assets. + * @param {button element} selectedIcon + * @param {string} assetId + */ +function selectIcon(selectedIcon, assetId="") { + document.getElementById(`dropdown-icon-button${assetId != "" ? "-" : ""}${assetId}`).value = selectedIcon.value; + document.getElementById(`icon${assetId != "" ? "-" : ""}${assetId}`).value = selectedIcon.value + + document.getElementById(`dropdown-icon-button${assetId != "" ? "-" : ""}${assetId}`).innerHTML = selectedIcon.innerHTML + + ` + + + + `; } resetRadio(); +autoOpenCreate(); diff --git a/src/public/svgs/icons/Bike.svg b/src/public/svgs/icons/Bike.svg new file mode 100644 index 0000000..c31ed29 --- /dev/null +++ b/src/public/svgs/icons/Bike.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/public/svgs/icons/Book.svg b/src/public/svgs/icons/Book.svg new file mode 100644 index 0000000..d1cd59b --- /dev/null +++ b/src/public/svgs/icons/Book.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/public/svgs/icons/Camera.svg b/src/public/svgs/icons/Camera.svg new file mode 100644 index 0000000..4fda7d9 --- /dev/null +++ b/src/public/svgs/icons/Camera.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/public/svgs/icons/Car.svg b/src/public/svgs/icons/Car.svg new file mode 100644 index 0000000..63014de --- /dev/null +++ b/src/public/svgs/icons/Car.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/public/svgs/icons/Coins.svg b/src/public/svgs/icons/Coins.svg new file mode 100644 index 0000000..17fee69 --- /dev/null +++ b/src/public/svgs/icons/Coins.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/public/svgs/icons/Console.svg b/src/public/svgs/icons/Console.svg new file mode 100644 index 0000000..932964d --- /dev/null +++ b/src/public/svgs/icons/Console.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/public/svgs/icons/Desktop.svg b/src/public/svgs/icons/Desktop.svg new file mode 100644 index 0000000..2ad4fbd --- /dev/null +++ b/src/public/svgs/icons/Desktop.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/public/svgs/icons/Device.svg b/src/public/svgs/icons/Device.svg new file mode 100644 index 0000000..ea62d89 --- /dev/null +++ b/src/public/svgs/icons/Device.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/public/svgs/icons/Electronic.svg b/src/public/svgs/icons/Electronic.svg new file mode 100644 index 0000000..7f34f13 --- /dev/null +++ b/src/public/svgs/icons/Electronic.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/public/svgs/icons/Home.svg b/src/public/svgs/icons/Home.svg new file mode 100644 index 0000000..230751a --- /dev/null +++ b/src/public/svgs/icons/Home.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/public/svgs/icons/Laptop.svg b/src/public/svgs/icons/Laptop.svg new file mode 100644 index 0000000..eeb9595 --- /dev/null +++ b/src/public/svgs/icons/Laptop.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/public/svgs/icons/Money.svg b/src/public/svgs/icons/Money.svg new file mode 100644 index 0000000..1476d84 --- /dev/null +++ b/src/public/svgs/icons/Money.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/public/svgs/icons/Motorcycle.svg b/src/public/svgs/icons/Motorcycle.svg new file mode 100644 index 0000000..bd3776e --- /dev/null +++ b/src/public/svgs/icons/Motorcycle.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/public/svgs/icons/Other.svg b/src/public/svgs/icons/Other.svg new file mode 100644 index 0000000..31bfd17 --- /dev/null +++ b/src/public/svgs/icons/Other.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/public/svgs/icons/Phone.svg b/src/public/svgs/icons/Phone.svg new file mode 100644 index 0000000..b34a759 --- /dev/null +++ b/src/public/svgs/icons/Phone.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/public/svgs/icons/Stock.svg b/src/public/svgs/icons/Stock.svg new file mode 100644 index 0000000..3caf5b6 --- /dev/null +++ b/src/public/svgs/icons/Stock.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/router/user.js b/src/router/user.js index 084ea03..dc8b232 100644 --- a/src/router/user.js +++ b/src/router/user.js @@ -4,7 +4,9 @@ const status = require("../util/statuses"); const ObjectId = require('mongodb').ObjectId; const session = require("express-session"); const bcrypt = require("bcrypt"); +const path = require("path"); const joi = require("joi"); +const fs = require("fs"); const salt = 12; /** @@ -20,6 +22,7 @@ const getAssetSchema = (type) => { case "other": assetSchema = joi.object({ type: joi.string().valid("other", "stock", "saving").required(), + icon: joi.string().alphanum().required(), name: joi.string().alphanum().min(3).max(30).required(), value: joi.number().min(0).required(), purchaseDate: joi.date().required(), @@ -60,7 +63,11 @@ const getAssetSchema = (type) => { */ module.exports = (middleware, users, plans, assets) => { const router = require("express").Router(); - + + // Create list of icon filenames + let icons = fs.readdirSync(path.join(__dirname, "../public/svgs/icons")); + icons = icons.map((icon) => icon.split(".")[0]); + router.use(middleware); router.get('/home', async (req, res) => { @@ -83,7 +90,9 @@ module.exports = (middleware, users, plans, assets) => { user: req.session.user, errMessage: req.session.errMessage, assets: userAssets, - geoData: req.session.geoData }); + geoData: req.session.geoData, + icons: icons, + }); return res.status(status.Ok); }); @@ -388,6 +397,7 @@ module.exports = (middleware, users, plans, assets) => { newAsset.name = `${newAsset.ticker} Stock`; } newAsset.value = parseFloat(newAsset.value); + newAsset.icon = type == "stock" ? "Stock" : type == "saving" ? "Coins" : newAsset.icon; assets.insertOne(newAsset, (err, _) => { if (err) { @@ -433,7 +443,8 @@ module.exports = (middleware, users, plans, assets) => { update.name = `${update.ticker} Stock`; } update.value = parseFloat(update.value); - delete update.id + delete update.id; + delete update.userId; assets.updateOne( { "_id": new ObjectId(req.body.id) }, @@ -524,6 +535,7 @@ module.exports = (middleware, users, plans, assets) => { return res.status(status.InternalServerError).redirect("/profile"); }); }); + router.get("/exRates/:lat/:lon", async (req, res) => { if (!req.session.geoData.country) { const response = await fetch(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${req.params.lat},${req.params.lon}&result_type=country&key=${process.env.GEOLOCATION_API}`); diff --git a/src/views/assets.ejs b/src/views/assets.ejs index cec34b0..0b6eae4 100644 --- a/src/views/assets.ejs +++ b/src/views/assets.ejs @@ -2,7 +2,6 @@ <%- include("./partials/header") %> - @@ -38,7 +37,31 @@ - + + + + Select an Icon + + + Other + + + + + + + <% icons.forEach(icon => { %> + + + + (<%= icon %>) + + + + <% }); %> + + + Asset Name @@ -178,7 +201,9 @@ - Icon + + + <%= asset.name %> @@ -237,7 +262,7 @@ @@ -259,24 +284,52 @@ - - ICON - + <% if (asset.type == "other") { %> + + + <%= asset.icon %> + + + + + + + <% icons.forEach(icon => { %> + + + + (<%= icon %>) + + + + <% }); %> + + + + <% } else { %> + + + + <% } %> + + <%= String(asset.type).charAt(0).toUpperCase() + String(asset.type).slice(1); %> Asset Edit + + <% if (asset.type != "stock") { %> Name @@ -328,16 +381,12 @@ <%- include("./partials/navBar") %> +<%- include("./partials/scriptLoader") %> <%- include("./partials/footer") %> \ No newline at end of file diff --git a/src/views/dashboard.ejs b/src/views/dashboard.ejs index 11979d3..519832d 100644 --- a/src/views/dashboard.ejs +++ b/src/views/dashboard.ejs @@ -91,4 +91,5 @@ <%- include("./partials/navBar") %> +<%- include("./partials/scriptLoader") %> <%- include("./partials/footer") %> \ No newline at end of file diff --git a/src/views/more.ejs b/src/views/more.ejs index 59e3acf..934700e 100644 --- a/src/views/more.ejs +++ b/src/views/more.ejs @@ -6,5 +6,6 @@ <%- include("./partials/navBar") %> +<%- include("./partials/scriptLoader") %> <%- include("./partials/footer") %> \ No newline at end of file diff --git a/src/views/newPlan.ejs b/src/views/newPlan.ejs index a05dd1a..5370290 100644 --- a/src/views/newPlan.ejs +++ b/src/views/newPlan.ejs @@ -37,4 +37,6 @@ +<%- include("./partials/navBar") %> +<%- include("./partials/scriptLoader") %> <%- include("./partials/footer") %> \ No newline at end of file diff --git a/src/views/partials/footer.ejs b/src/views/partials/footer.ejs index 89cc09b..9ee0b91 100644 --- a/src/views/partials/footer.ejs +++ b/src/views/partials/footer.ejs @@ -7,14 +7,5 @@ - - - - -