31 lines
1.0 KiB
TypeScript
31 lines
1.0 KiB
TypeScript
import {Router} from "express";
|
|
|
|
import ApplicationComponent from "../ApplicationComponent.js";
|
|
|
|
export default class FormHelperComponent extends ApplicationComponent {
|
|
|
|
public async initRoutes(router: Router): Promise<void> {
|
|
router.use((req, res, next) => {
|
|
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;
|
|
});
|
|
next();
|
|
});
|
|
|
|
router.use((req, res, next) => {
|
|
if (['GET', 'POST'].find(m => m === req.method)) {
|
|
if (typeof req.body === 'object' && Object.keys(req.body).length > 0) {
|
|
req.flash('previousFormData', req.body);
|
|
}
|
|
}
|
|
next();
|
|
});
|
|
}
|
|
}
|