2020-07-11 11:46:16 +02:00
|
|
|
import {Router} from "express";
|
2020-04-22 15:52:17 +02:00
|
|
|
|
2021-05-03 19:29:22 +02:00
|
|
|
import ApplicationComponent from "../ApplicationComponent.js";
|
|
|
|
|
2020-09-25 22:03:22 +02:00
|
|
|
export default class FormHelperComponent extends ApplicationComponent {
|
2021-05-13 17:38:48 +02:00
|
|
|
|
2021-05-13 16:03:59 +02:00
|
|
|
public async initRoutes(router: Router): Promise<void> {
|
2020-04-22 15:52:17 +02:00
|
|
|
router.use((req, res, next) => {
|
2021-11-24 22:08:38 +01:00
|
|
|
res.setLazyLocal('validation', () => {
|
|
|
|
const validation = req.flash('validation');
|
|
|
|
return validation.length > 0 ? validation[0] : null;
|
|
|
|
});
|
|
|
|
|
|
|
|
res.setLazyLocal('previousFormData', () => {
|
|
|
|
const previousFormData = req.flash('previousFormData');
|
|
|
|
return previousFormData.length > 0 ? previousFormData[0] : null;
|
|
|
|
});
|
2020-04-22 15:52:17 +02:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
router.use((req, res, next) => {
|
|
|
|
if (['GET', 'POST'].find(m => m === req.method)) {
|
2021-01-25 17:26:29 +01:00
|
|
|
if (typeof req.body === 'object' && Object.keys(req.body).length > 0) {
|
2020-04-22 15:52:17 +02:00
|
|
|
req.flash('previousFormData', req.body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
2020-09-25 23:42:15 +02:00
|
|
|
}
|