diff --git a/application/frontend/src/main.js b/application/frontend/src/main.js index e14fe2de..59ecd996 100644 --- a/application/frontend/src/main.js +++ b/application/frontend/src/main.js @@ -23,7 +23,9 @@ const endpoints = { "refresh-token": domains.security + "/session/refresh-token", "sign-in": domains.security + "/session/sign-in", "sign-out": domains.security + "/session/sign-out", - "list-credit-card-products": domains.card + "/product/list-items" + "list-credit-card-products": domains.card + "/product/list-items", + "activate-product": domains.card + "/product/activate", + "deactivate-product": domains.card + "/product/deactivate" } // Accept JSON bodies @@ -103,6 +105,7 @@ app.post('/sign-in', unauthenticated, routeSignIn); app.post('/sign-up', unauthenticated, routeSignUp); app.get('/verification-emails', routeVerificationEmails); app.get('/card/products', authenticated, routeCardProducts); +app.post('/card/products', authenticated, cardToggle); async function userDetails(token) { const response = await fetch(endpoints["user-details"], { @@ -385,6 +388,44 @@ async function routeCardProducts(req, res) { }); } +async function cardToggle(req, res) { + const productId = req.body.productId; + const active = req.body.active; + + if (!productId) { + return res.status(400).json({ error: 'Product ID is required' }); + } + + try { + // Make the appropriate fetch request based on the product's current status + const endpoint = active === "true" + ? endpoints["deactivate-product"] + "/" + productId + : endpoints["activate-product"] + "/" + productId; + + const response = await fetch(endpoint, { + method: "POST", + body: '{}', + headers: { + 'Content-Type': 'application/json', + } + }); + + // Bit of a hack, the request -> event -> projection will take some time. + // Realistically you would update the interface locally, and refresh state async + await sleep(2000); + + // After successfully toggling the product status, render the updated product list + return await routeCardProducts(req, res); + } catch (error) { + console.error('Error in cardToggle:', error); + return await routeCardProducts(req, res); + } +} + +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + app.get("*", authenticated, render("404", { title: "Not Found" })) app.listen(port, () => { diff --git a/application/frontend/src/views/card/products.handlebars b/application/frontend/src/views/card/products.handlebars index 0419d54f..81f5c673 100644 --- a/application/frontend/src/views/card/products.handlebars +++ b/application/frontend/src/views/card/products.handlebars @@ -22,36 +22,55 @@ -