From c8c7710043a70a5175cdfd9707548c1d6f3e61cd Mon Sep 17 00:00:00 2001 From: Bubun Das Date: Tue, 15 Mar 2022 21:28:48 +0530 Subject: [PATCH] theme init hook added, Client theme updated --- .../clientarea/profile.controller.ts | 17 ++++ src/core/api/users.api.ts | 8 ++ src/lib/theme.ts | 16 +++- src/middlewares/currency.middleware.ts | 19 ++++- src/routes/clientarea/currency.route.ts | 20 +++++ src/routes/clientarea/profile.route.ts | 16 ++++ themes/art-of-light/clientarea/profile.hbs | 81 +++++++++++++++++++ themes/art-of-light/functions.js | 3 +- themes/art-of-light/layouts/main.hbs | 5 +- themes/art-of-light/partials/header.hbs | 2 +- .../art-of-light/public/assets/css/custom.css | 3 + typings/common/express.d.ts | 2 +- 12 files changed, 183 insertions(+), 9 deletions(-) create mode 100644 src/controllers/clientarea/profile.controller.ts create mode 100644 src/routes/clientarea/currency.route.ts create mode 100644 src/routes/clientarea/profile.route.ts create mode 100644 themes/art-of-light/clientarea/profile.hbs create mode 100644 themes/art-of-light/public/assets/css/custom.css diff --git a/src/controllers/clientarea/profile.controller.ts b/src/controllers/clientarea/profile.controller.ts new file mode 100644 index 0000000..e880983 --- /dev/null +++ b/src/controllers/clientarea/profile.controller.ts @@ -0,0 +1,17 @@ +import UserApi from '@core/api/users.api'; +import { Request, Response } from 'express'; +import { User } from '@models/user.model'; + +export const getProfile = async (req: Request, res: Response) => { + const user = await UserApi.getUserById(req.user.id); + res.load('clientarea/profile', { + pathName: 'profile', + layout: 'clientarea', + user, + }); +}; + +export const postProfile = async (req: Request, res: Response) => { + await UserApi.updateUserSafe(req.user.id, req.body as User); + res.redirect('/clientarea/profile'); +}; diff --git a/src/core/api/users.api.ts b/src/core/api/users.api.ts index 05a4575..edaea98 100644 --- a/src/core/api/users.api.ts +++ b/src/core/api/users.api.ts @@ -16,6 +16,14 @@ class UserApi { return await UserModel.findOne({ email }).lean(); } + // Update User Function intended for use in the client area + static async updateUserSafe(user: User, data: User): Promise { + const newUser = await UserModel.findByIdAndUpdate(user._id, data, { + runValidators: true, + }); + return newUser; + } + static async getUserById(id: string) { return await UserModel.findById(id).lean(); } diff --git a/src/lib/theme.ts b/src/lib/theme.ts index e8e76f3..c243316 100644 --- a/src/lib/theme.ts +++ b/src/lib/theme.ts @@ -276,7 +276,21 @@ class Theme { const currentTheme = await this.getCurrentTheme(); - app.use('/public', express.static(currentTheme.publicFolderPath)); + // check if functions.js file exists + const functionsFilePath = join(currentTheme.absulutePath, 'functions.js'); + if (await util.isFile(functionsFilePath)) { + const functions = await import(functionsFilePath); + + if (typeof functions.init === 'function') { + functions.init(app); + } + } + + // Theme static files + app.use( + currentTheme.themeBaseUri, + express.static(currentTheme.publicFolderPath), + ); } } diff --git a/src/middlewares/currency.middleware.ts b/src/middlewares/currency.middleware.ts index b776042..d556d5c 100644 --- a/src/middlewares/currency.middleware.ts +++ b/src/middlewares/currency.middleware.ts @@ -5,13 +5,24 @@ import CurrencyApi from '@core/api/currency.api'; // TODO: Impliment Get Currency From IP address // TODO: Impliment Get Currency From User Preferences // TODO: Impliment Get Currency From User Session - +// TODO: Handle Errors export const getCurrency = async ( - _req: Request, + req: Request, res: Response, next: NextFunction, ) => { - const currency = await CurrencyApi.getDefaultCurrency(); - res.locals.currency = currency; + if (req.session.currency) { + res.locals.currency = await CurrencyApi.getCurrency({ + code: req.session.currency, + }); + } else if (req.query.cur) { + res.locals.currency = await CurrencyApi.getCurrency({ + code: req.query.cur, + }); + req.session.currency = req.query.cur; + } else { + const currency = await CurrencyApi.getDefaultCurrency(); + res.locals.currency = currency; + } next(); }; diff --git a/src/routes/clientarea/currency.route.ts b/src/routes/clientarea/currency.route.ts new file mode 100644 index 0000000..ae7f8b5 --- /dev/null +++ b/src/routes/clientarea/currency.route.ts @@ -0,0 +1,20 @@ +import BaseRoute from '@routes/base.route'; +import { getCurrency } from '@middlewares/currency.middleware'; + +class HomeRoute extends BaseRoute { + constructor() { + super('/'); + this.init(); + } + + init() { + this.router.use(getCurrency); + this.router.post('/setcurrency', async (req, res) => { + req.session.currency = req.body.currency; + // redirect to referrar + res.redirect(req.headers.referer || '/'); + }); + } +} + +export default HomeRoute; diff --git a/src/routes/clientarea/profile.route.ts b/src/routes/clientarea/profile.route.ts new file mode 100644 index 0000000..17add73 --- /dev/null +++ b/src/routes/clientarea/profile.route.ts @@ -0,0 +1,16 @@ +import { getProfile } from '@controllers/clientarea/profile.controller'; +import BaseRoute from '@routes/base.route'; + +class Profile extends BaseRoute { + constructor() { + super('/clientarea'); + this.init(); + } + + init() { + // TODO - initialize + this.router.route('/profile').get(getProfile); + } +} + +export default Profile; diff --git a/themes/art-of-light/clientarea/profile.hbs b/themes/art-of-light/clientarea/profile.hbs new file mode 100644 index 0000000..93f416e --- /dev/null +++ b/themes/art-of-light/clientarea/profile.hbs @@ -0,0 +1,81 @@ +
+ +
+
+

Edit Profile

+ + +
+
+
+
+ + +
Enter your name here
+
+
+ + +
Enter Your Primary Email
+
+
+ + +
+
+ + +
+ +
+
+
+
+
+ + +
Enter your name here
+
+
+ + +
Enter Address Line 1
+
+
+ + +
Enter Address Line 2
+
+ +
+
+
+
+
+
+
+
+
+ + + +
\ No newline at end of file diff --git a/themes/art-of-light/functions.js b/themes/art-of-light/functions.js index 624f8de..eb9e61d 100644 --- a/themes/art-of-light/functions.js +++ b/themes/art-of-light/functions.js @@ -1,6 +1,7 @@ module.exports = { init(funcs) { - // JavaScript to be fired on all pages + // Runs on theme load to initialize the theme. + console.log('Theme initialized.'); }, }; diff --git a/themes/art-of-light/layouts/main.hbs b/themes/art-of-light/layouts/main.hbs index 60bb5cb..5f23673 100644 --- a/themes/art-of-light/layouts/main.hbs +++ b/themes/art-of-light/layouts/main.hbs @@ -7,7 +7,9 @@ {{#if title }} {{title}} {{/if}} - + + + @@ -20,6 +22,7 @@ + \ No newline at end of file diff --git a/themes/art-of-light/partials/header.hbs b/themes/art-of-light/partials/header.hbs index 5e76431..8df17c2 100644 --- a/themes/art-of-light/partials/header.hbs +++ b/themes/art-of-light/partials/header.hbs @@ -27,7 +27,7 @@ Hi, {{user.name}}