express-sweet-generator 2.0.11 → 2.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +8 -4
  2. package/package.json +1 -1
  3. package/templates/cjs/bin/www.ejs +3 -3
  4. package/templates/cjs/config/view.js +5 -1
  5. package/templates/cjs/models/UserModel.js +9 -1
  6. package/templates/cjs/public/build/login.js +3 -7
  7. package/templates/{esm/public/build/editPersonal.js → cjs/public/build/profile-edit.js} +115 -105
  8. package/templates/cjs/public/build/users.js +185 -185
  9. package/templates/cjs/routes/api/profile.js +29 -0
  10. package/templates/cjs/routes/api/users.js +0 -18
  11. package/templates/cjs/routes/profile.js +16 -0
  12. package/templates/cjs/routes/users.js +1 -12
  13. package/templates/cjs/views/layout/default.hbs +14 -5
  14. package/templates/{esm/views/edit-personal.hbs → cjs/views/profile/edit.hbs} +4 -4
  15. package/templates/{esm/views/personal.hbs → cjs/views/profile/show.hbs} +1 -1
  16. package/templates/client/src/api/ProfileApi.js +11 -0
  17. package/templates/client/src/api/UserApi.js +0 -4
  18. package/templates/client/src/login.js +4 -4
  19. package/templates/client/src/{editPersonal.js → profile-edit.js} +9 -7
  20. package/templates/client/src/users.js +14 -10
  21. package/templates/client/webpack.config.js +1 -1
  22. package/templates/esm/bin/www.ejs +3 -3
  23. package/templates/esm/config/view.js +5 -1
  24. package/templates/esm/models/UserModel.js +9 -1
  25. package/templates/esm/public/build/login.js +3 -7
  26. package/templates/{cjs/public/build/editPersonal.js → esm/public/build/profile-edit.js} +115 -105
  27. package/templates/esm/public/build/users.js +185 -185
  28. package/templates/esm/routes/api/profile.js +29 -0
  29. package/templates/esm/routes/api/users.js +0 -18
  30. package/templates/esm/routes/profile.js +16 -0
  31. package/templates/esm/routes/users.js +1 -12
  32. package/templates/esm/views/layout/default.hbs +14 -5
  33. package/templates/{cjs/views/edit-personal.hbs → esm/views/profile/edit.hbs} +4 -4
  34. package/templates/{cjs/views/personal.hbs → esm/views/profile/show.hbs} +1 -1
  35. /package/templates/cjs/public/build/{editPersonal.css → profile-edit.css} +0 -0
  36. /package/templates/cjs/views/{users.hbs → users/index.hbs} +0 -0
  37. /package/templates/client/src/{editPersonal.css → profile-edit.css} +0 -0
  38. /package/templates/esm/public/build/{editPersonal.css → profile-edit.css} +0 -0
  39. /package/templates/esm/views/{users.hbs → users/index.hbs} +0 -0
@@ -0,0 +1,29 @@
1
+ const express = require('express');
2
+ // const expressExtension = require('express-sweet');
3
+ const {body} = require('express-validator');
4
+ const UserModel = require('../../models/UserModel');
5
+ const NotFoundError = require('../../errors/NotFoundError');
6
+ const isValidImageDataUrl = require('../../validators/isValidImageDataUrl');
7
+ const checkValidationResult = require('../../middlewares/checkValidationResult');
8
+
9
+ const router = express.Router();
10
+
11
+ router.put('/', [
12
+ body('user.email').trim().not().isEmpty().isEmail(),
13
+ body('user.name').trim().not().isEmpty().isLength({max: 30}),
14
+ body('user.password').trim().optional({nullable: true, checkFalsy: true}).isLength({max: 128}),
15
+ body('user.icon').not().isEmpty().custom(isValidImageDataUrl),
16
+ checkValidationResult,
17
+ ], async (req, res, next) => {
18
+ try {
19
+ await UserModel.updateUser(req.user.id, req.body.user);
20
+ res.json(true);
21
+ } catch (error) {
22
+ if (error instanceof NotFoundError)
23
+ res.json({error: error.name});
24
+ else
25
+ next(error);
26
+ }
27
+ });
28
+
29
+ module.exports = router;
@@ -84,22 +84,4 @@ router.delete('/:userId(\\d+)', async (req, res) => {
84
84
  res.json(true);
85
85
  });
86
86
 
87
- router.put('/profile', [
88
- body('user.email').trim().not().isEmpty().isEmail(),
89
- body('user.name').trim().not().isEmpty().isLength({max: 30}),
90
- body('user.password').trim().optional({nullable: true, checkFalsy: true}).isLength({max: 128}),
91
- body('user.icon').not().isEmpty().custom(isValidImageDataUrl),
92
- checkValidationResult,
93
- ], async (req, res, next) => {
94
- try {
95
- await UserModel.updateUser(req.user.id, req.body.user);
96
- res.json(true);
97
- } catch (error) {
98
- if (error instanceof NotFoundError)
99
- res.json({error: error.name});
100
- else
101
- next(error);
102
- }
103
- });
104
-
105
87
  module.exports = router;
@@ -0,0 +1,16 @@
1
+ const express = require('express');
2
+ const UserModel = require('../models/UserModel');
3
+
4
+ const router = express.Router();
5
+
6
+ router.get('/', async (req, res) => {
7
+ const user = await UserModel.getUser(req.user.id);
8
+ res.render('profile/show', {user: user.toJSON()});
9
+ });
10
+
11
+ router.get('/edit', async (req, res) => {
12
+ const user = await UserModel.getUser(req.user.id);
13
+ res.render('profile/edit', {user: user.toJSON()});
14
+ });
15
+
16
+ module.exports = router;
@@ -1,20 +1,9 @@
1
1
  const express = require('express');
2
- const UserModel = require('../models/UserModel');
3
2
 
4
3
  const router = express.Router();
5
4
 
6
5
  router.get('/', (req, res) => {
7
- res.render('users');
8
- });
9
-
10
- router.get('/personal', async (req, res) => {
11
- const user = await UserModel.getUser(req.user.id);
12
- res.render('personal', {user: user.toJSON()});
13
- });
14
-
15
- router.get('/edit-personal', async (req, res) => {
16
- const user = await UserModel.getUser(req.user.id);
17
- res.render('edit-personal', {user: user.toJSON()});
6
+ res.render('users/index');
18
7
  });
19
8
 
20
9
  module.exports = router;
@@ -33,6 +33,9 @@
33
33
  data-kt-app-sidebar-push-toolbar="true"
34
34
  data-kt-app-sidebar-push-footer="true"
35
35
  data-kt-app-toolbar-enabled="true"
36
+ {{#if (eq sidebar_minimize_state "on")}}
37
+ data-kt-app-sidebar-minimize="on"
38
+ {{/if}}
36
39
  class="app-default"
37
40
  {{else}}
38
41
  class="app-blank"
@@ -109,9 +112,9 @@
109
112
  </div>
110
113
  <!--end:Menu item-->
111
114
  <!--begin:Menu item-->
112
- <div class="menu-item menu-here-bg me-0 me-lg-2 {{#if (eq currentPath '/users/personal')}}here{{/if}}">
115
+ <div class="menu-item menu-here-bg me-0 me-lg-2 {{#if (eq currentPath '/profile')}}here{{/if}}">
113
116
  <!--begin:Menu link-->
114
- <a class="menu-link" href="/users/personal">
117
+ <a class="menu-link" href="/profile">
115
118
  <span class="menu-title">Profile Settings</span>
116
119
  </a>
117
120
  <!--end:Menu link-->
@@ -230,7 +233,7 @@
230
233
  <!--end::Menu separator-->
231
234
  <!--begin::Menu item-->
232
235
  <div class="menu-item px-5">
233
- <a href="/users/personal" class="menu-link px-5">Profile Settings</a>
236
+ <a href="/profile" class="menu-link px-5">Profile Settings</a>
234
237
  </div>
235
238
  <!--end::Menu item-->
236
239
  <!--begin::Menu item-->
@@ -283,7 +286,13 @@
283
286
  4. Add "active" class to to sidebar toggle element with "kt_app_sidebar_toggle" id.
284
287
  }
285
288
  -->
286
- <div id="kt_app_sidebar_toggle" class="app-sidebar-toggle btn btn-icon btn-shadow btn-sm btn-color-muted btn-active-color-primary body-bg h-30px w-30px position-absolute top-50 start-100 translate-middle rotate" data-kt-toggle="true" data-kt-toggle-state="active" data-kt-toggle-target="body" data-kt-toggle-name="app-sidebar-minimize">
289
+ <div
290
+ id="kt_app_sidebar_toggle"
291
+ class="app-sidebar-toggle btn btn-icon btn-shadow btn-sm btn-color-muted btn-active-color-primary body-bg h-30px w-30px position-absolute top-50 start-100 translate-middle rotate {{#if (eq sidebar_minimize_state "on")}}active{{/if}}"
292
+ data-kt-toggle="true"
293
+ data-kt-toggle-state="active"
294
+ data-kt-toggle-target="body"
295
+ data-kt-toggle-name="app-sidebar-minimize">
287
296
  <i class="ki-duotone ki-double-left fs-2 rotate-180">
288
297
  <span class="path1"></span>
289
298
  <span class="path2"></span>
@@ -328,7 +337,7 @@
328
337
  <!--begin:Menu item-->
329
338
  <div class="menu-item">
330
339
  <!--begin:Menu link-->
331
- <a class="menu-link {{#if (or (eq currentPath '/users/personal') (eq currentPath '/users/edit-personal'))}}active{{/if}}" href="/users/personal">
340
+ <a class="menu-link {{#if (or (eq currentPath '/profile') (eq currentPath '/profile/edit'))}}active{{/if}}" href="/profile">
332
341
  <span class="menu-icon">
333
342
  <i class="ki-duotone ki-address-book fs-2">
334
343
  <span class="path1"></span>
@@ -1,14 +1,14 @@
1
1
  {{!< default}}
2
2
  {{#contentFor 'title'}}Edit personal settings{{/contentFor}}
3
- {{#contentFor 'style'}}<link rel="stylesheet" type="text/css" href="{{cacheBusting '/build/editPersonal.css' ''}}">{{/contentFor}}
4
- {{#contentFor 'script'}}<script src="{{cacheBusting '/build/editPersonal.js' ''}}"></script>{{/contentFor}}
3
+ {{#contentFor 'style'}}<link rel="stylesheet" type="text/css" href="{{cacheBusting '/build/profile-edit.css' ''}}">{{/contentFor}}
4
+ {{#contentFor 'script'}}<script src="{{cacheBusting '/build/profile-edit.js' ''}}"></script>{{/contentFor}}
5
5
  <input data-ref="user.iconPath" type="hidden" value="{{user.icon}}">
6
6
  <input data-ref="user.modified" type="hidden" value="{{formatDate 'x' user.modified}}">
7
7
 
8
8
  <!--begin::Basic info-->
9
9
  <div class="card mb-5 mb-xl-10">
10
10
  <!--begin::Form-->
11
- <form data-ref="personalForm" class="form" novalidate="novalidate">
11
+ <form data-ref="profileEditForm" class="form" novalidate="novalidate">
12
12
  <!--begin::Card body-->
13
13
  <div class="card-body p-9">
14
14
  <!--begin::Input group-->
@@ -85,7 +85,7 @@
85
85
  <!--end::Card body-->
86
86
  <!--begin::Actions-->
87
87
  <div class="card-footer d-flex justify-content-end py-6 px-9">
88
- <a href="/users/personal" class="btn btn-light btn-active-light-primary me-2">Back to Profile</a>
88
+ <a href="/profile" class="btn btn-light btn-active-light-primary me-2">Back to Profile</a>
89
89
  <button type="submit" class="btn btn-primary">Update Profile</button>
90
90
  </div>
91
91
  <!--end::Actions-->
@@ -5,7 +5,7 @@
5
5
  <!--begin::Card header-->
6
6
  <div class="card-header cursor-pointer justify-content-end">
7
7
  <!--begin::Action-->
8
- <a href="/users/edit-personal" class="btn btn-primary align-self-center">Edit Profile</a>
8
+ <a href="/profile/edit" class="btn btn-primary align-self-center">Edit Profile</a>
9
9
  <!--end::Action-->
10
10
  </div>
11
11
  <!--end::Card header-->
@@ -0,0 +1,11 @@
1
+ import Api from '~/shared/Api';
2
+
3
+ export default class extends Api {
4
+ constructor() {
5
+ super('/api/profile');
6
+ }
7
+
8
+ async updateProfile(formData) {
9
+ return this.client.put('/', formData);
10
+ }
11
+ }
@@ -28,8 +28,4 @@ export default class extends Api {
28
28
  async deleteUser(userId) {
29
29
  return this.client.delete(`/${userId}`);
30
30
  }
31
-
32
- async updateProfile(formData) {
33
- return this.client.put('/profile', formData);
34
- }
35
31
  }
@@ -1,8 +1,8 @@
1
- import '~/login.css';
2
1
  import {components} from 'metronic-extension';
3
2
  import UserApi from '~/api/UserApi';
3
+ import '~/login.css';
4
4
 
5
- function initValidation() {
5
+ const initValidation = () => {
6
6
  validation = new components.Validation(ref.loginForm.get(0), {
7
7
  email: {
8
8
  validators: {
@@ -19,7 +19,7 @@ function initValidation() {
19
19
  });
20
20
  }
21
21
 
22
- function initForm() {
22
+ const handleForm = () => {
23
23
  validation.onValid(async () => {
24
24
  try {
25
25
  validation.onIndicator();
@@ -40,4 +40,4 @@ const userApi = new UserApi();
40
40
  const ref = components.selectRef();
41
41
  let validation;
42
42
  initValidation();
43
- initForm();
43
+ handleForm();
@@ -1,9 +1,10 @@
1
- import '~/editPersonal.css';
2
1
  import {components, utils} from 'metronic-extension';
2
+ import ProfileApi from '~/api/ProfileApi';
3
3
  import UserApi from '~/api/UserApi';
4
+ import '~/profile-edit.css';
4
5
 
5
- function initValidation() {
6
- validation = new components.Validation(ref.personalForm.get(0), {
6
+ const initValidation = () => {
7
+ validation = new components.Validation(ref.profileEditForm.get(0), {
7
8
  'user[email]': {
8
9
  validators: {
9
10
  notEmpty: {message: 'Email is required.'},
@@ -43,11 +44,11 @@ function initValidation() {
43
44
  });
44
45
  }
45
46
 
46
- function initForm() {
47
+ const handleForm = () => {
47
48
  validation.onValid(async () => {
48
49
  try {
49
50
  validation.onIndicator();
50
- const {data} = await userApi.updateProfile(new FormData(validation.form));
51
+ const {data} = await profileApi.updateProfile(new FormData(validation.form));
51
52
  validation.offIndicator();
52
53
  if (data.error)
53
54
  if (data.error === 'NotFoundError') {
@@ -67,7 +68,7 @@ function initForm() {
67
68
  throw error;
68
69
  }
69
70
  });
70
- ref.personalForm
71
+ ref.profileEditForm
71
72
  .on('show.bs.collapse hide.bs.collapse', '#passwordCollapse', evnt => {
72
73
  if (evnt.type === 'show') {
73
74
  validation.enableValidator('user[password]');
@@ -96,9 +97,10 @@ function initForm() {
96
97
  });
97
98
  }
98
99
 
100
+ const profileApi = new ProfileApi();
99
101
  const userApi = new UserApi();
100
102
  const ref = components.selectRef('#kt_app_content_container');
101
103
  const originalEmail = utils.trim(ref.user.email.val(), true);
102
104
  let validation;
103
105
  initValidation();
104
- initForm();
106
+ handleForm();
@@ -1,30 +1,32 @@
1
- import '~/users.css';
2
1
  import hbs from 'handlebars-extd';
3
2
  import {components} from 'metronic-extension';
4
3
  import Datatable from '~/shared/Datatable';
5
4
  import reloadDataTableWithDelay from '~/shared/reloadDataTableWithDelay';
6
5
  import UserApi from '~/api/UserApi';
7
6
  import UserModal from '~/modals/UserModal';
7
+ import '~/users.css';
8
8
 
9
- function initTable() {
9
+ const initDatatable = () => {
10
+ let targetIndex = 0;
10
11
  userTable = new Datatable(ref.userTable, {
11
12
  ajax: {
12
13
  url: '/api/users',
13
14
  data: d => {
15
+ // Set filter parameters.
14
16
  d.search = ref.keyword.val()
15
17
  }
16
18
  },
17
19
  dom: `<'row'<'col-12 dataTables_pager'p>><'row'<'col-12'tr>><'row'<'col-12 dataTables_pager'p>>`,
18
20
  columnDefs: [
19
- {targets: 0, data: 'name', className: 'd-flex align-items-center', render: (data, type, row) => hbs.compile(
21
+ {targets: targetIndex++, data: 'name', className: 'd-flex align-items-center', render: (data, type, row) => hbs.compile(
20
22
  `<div class="symbol symbol-35px me-3">
21
23
  <img src="{{row.icon}}?{{formatDate 'x' row.modified}}">
22
24
  </div>
23
25
  <span class="text-gray-800">{{row.name}}</span>`)({row})
24
26
  },
25
- {targets: 1, data: 'email'},
26
- {targets: 2, data: 'modified', render: data => hbs.compile(`{{formatDate 'YYYY/M/D HH:mm:ss' data}}`)({data})},
27
- {targets: -1, className: 'text-end', data: 'actions', orderable: false, render: (data, type, row) =>
27
+ {targets: targetIndex++, data: 'email'},
28
+ {targets: targetIndex++, data: 'modified', render: data => hbs.compile(`{{formatDate 'YYYY/M/D HH:mm:ss' data}}`)({data})},
29
+ {targets: targetIndex++, className: 'text-end', data: 'actions', orderable: false, render: (data, type, row) =>
28
30
  hbs.compile(
29
31
  `<button data-on-update-user data-id="{{row.id}}" data-bs-toggle="tooltip" data-bs-dismiss="click" title="Update user." type="button" class="btn btn-sm btn-icon btn-light btn-icon-gray-800 btn-active-light-primary me-3">
30
32
  <!--begin::Svg Icon | path: icons/duotune/general/gen019.svg-->
@@ -51,11 +53,13 @@ function initTable() {
51
53
  {{/if}}`)({row})
52
54
  }
53
55
  ],
54
- order: [[0, 'asc']]
56
+ order: [
57
+ [0, 'asc'],
58
+ ]
55
59
  });
56
60
  }
57
61
 
58
- function initForm() {
62
+ const handleForm = () => {
59
63
  $('body')
60
64
  .on('click', '[data-on-create-user]', async () => {
61
65
  if (await userModal.show('create') !== false)
@@ -100,5 +104,5 @@ const userApi = new UserApi();
100
104
  const userModal = new UserModal();
101
105
  const ref = components.selectRef('#kt_app_content_container');
102
106
  let userTable;
103
- initTable();
104
- initForm();
107
+ initDatatable();
108
+ handleForm();
@@ -17,7 +17,7 @@ module.exports = {
17
17
  app: './src/app.js',
18
18
  login: './src/login.js',
19
19
  users: './src/users.js',
20
- editPersonal: './src/editPersonal.js'
20
+ 'profile-edit': './src/profile-edit.js'
21
21
  },
22
22
  output: {
23
23
  path: path.resolve(__dirname, '../public/build'),
@@ -9,7 +9,7 @@ import app from '../app.js';
9
9
  /**
10
10
  * Normalize a port into a number, string, or false.
11
11
  */
12
- function normalizePort(val) {
12
+ const normalizePort = val => {
13
13
  var port = parseInt(val, 10);
14
14
  // named pipe
15
15
  if (isNaN(port)) return val;
@@ -22,7 +22,7 @@ function normalizePort(val) {
22
22
  /**
23
23
  * Event listener for HTTP server "error" event.
24
24
  */
25
- function onError(error) {
25
+ const onError = error => {
26
26
  if (error.syscall !== 'listen')
27
27
  throw error;
28
28
  const bind = typeof port === 'string' ? `Pipe ${port}` : `Port ${port}`;
@@ -45,7 +45,7 @@ function onError(error) {
45
45
  /**
46
46
  * Event listener for HTTP server "listening" event.
47
47
  */
48
- function onListening() {
48
+ const onListening = () => {
49
49
  const addr = server.address();
50
50
  const bind = typeof addr === 'string' ? `pipe ${addr}` : `port ${addr.port}`;
51
51
  debug(`Listening on ${bind}`);
@@ -44,5 +44,9 @@ export default {
44
44
  * res.locals.extra = 'Extra';
45
45
  * }
46
46
  */
47
- beforeRender: (req, res) => {}
47
+ beforeRender: (req, res) => {
48
+ // Set the initial sidebar state from the cookie, defaulting to 'off'.
49
+ res.locals.sidebar_minimize_state = req.cookies.sidebar_minimize_state || 'off';
50
+ // console.log('Sidebar minimize state from cookie:', req.cookies.sidebar_minimize_state);
51
+ }
48
52
  }
@@ -68,8 +68,10 @@ export default class extends expressExtension.database.Model {
68
68
  search: null,
69
69
  }, paginationOptions);
70
70
 
71
+ // Calculate total records.
71
72
  const recordsTotal = await super.count();
72
73
 
74
+ // Where clause for filtering data.
73
75
  const whereClause = {};
74
76
  if (paginationOptions.search)
75
77
  whereClause[super.Op.or] = [
@@ -77,12 +79,16 @@ export default class extends expressExtension.database.Model {
77
79
  {name: {[super.Op.like]: `%${paginationOptions.search}%`}}
78
80
  ];
79
81
 
82
+ // Calculate filtered records.
80
83
  const recordsFiltered = await super.count({where: whereClause});
81
84
 
85
+ // Fetch paginated data.
82
86
  const data = await super.findAll({
83
87
  attributes: ['id', 'name', 'email', 'icon', 'modified'],
84
88
  where: whereClause,
85
- order: [[paginationOptions.order, paginationOptions.dir]],
89
+ order: [
90
+ [super.col(paginationOptions.order), paginationOptions.dir],
91
+ ],
86
92
  offset: parseInt(paginationOptions.start, 10),
87
93
  limit: parseInt(paginationOptions.length),
88
94
  raw: true
@@ -163,6 +169,7 @@ export default class extends expressExtension.database.Model {
163
169
  }
164
170
 
165
171
  static async emailExists(email, excludeUserId = null) {
172
+ // Where clause for filtering data.
166
173
  const whereClause = {email};
167
174
  if (excludeUserId)
168
175
  whereClause.id = {[super.Op.ne]: excludeUserId};
@@ -170,6 +177,7 @@ export default class extends expressExtension.database.Model {
170
177
  }
171
178
 
172
179
  static async userIdExists(userId) {
180
+ // Where clause for filtering data.
173
181
  const whereClause = {email};
174
182
  if (excludeUserId)
175
183
  whereClause.id = {[super.Op.ne]: excludeUserId};
@@ -169,17 +169,13 @@ var build = __webpack_require__(236);
169
169
  async deleteUser(userId) {
170
170
  return this.client.delete(`/${userId}`);
171
171
  }
172
-
173
- async updateProfile(formData) {
174
- return this.client.put('/profile', formData);
175
- }
176
172
  });
177
173
  ;// CONCATENATED MODULE: ./src/login.js
178
174
 
179
175
 
180
176
 
181
177
 
182
- function initValidation() {
178
+ const initValidation = () => {
183
179
  validation = new build.components.Validation(ref.loginForm.get(0), {
184
180
  email: {
185
181
  validators: {
@@ -196,7 +192,7 @@ function initValidation() {
196
192
  });
197
193
  }
198
194
 
199
- function initForm() {
195
+ const handleForm = () => {
200
196
  validation.onValid(async () => {
201
197
  try {
202
198
  validation.onIndicator();
@@ -217,7 +213,7 @@ const userApi = new UserApi();
217
213
  const ref = build.components.selectRef();
218
214
  let validation;
219
215
  initValidation();
220
- initForm();
216
+ handleForm();
221
217
  })();
222
218
 
223
219
  /******/ })()