feat: extract admin (#43)
All checks were successful
it-academy/dry-wash-pl/pipeline/head This commit looks good

This commit is contained in:
RustamRu 2024-12-01 12:17:04 +03:00
parent 5ef0d5953d
commit 7c157574c8
2 changed files with 52 additions and 43 deletions

38
stubs/api/admin.js Normal file
View File

@ -0,0 +1,38 @@
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-require-imports */
const router = require('express').Router();
const STUBS = { masters: 'success', orders: 'success' };
router.get('/set/:name/:value', (req, res) => {
const { name, value } = req.params;
STUBS[name] = value;
res.send('ok');
});
router.get('/', (req, res) => {
res.send(`<div>
<fieldset>
<legend>Мастера</legend>
${generateRadioInput('masters', 'success')}
${generateRadioInput('masters', 'error')}
</fieldset>
<fieldset>
<legend>Заказы</legend>
${generateRadioInput('orders', 'success')}
${generateRadioInput('orders', 'error')}
</fieldset>
</div>`);
});
module.exports = router;
module.exports.STUBS = STUBS;
function generateRadioInput(name, type) {
return `<label>
<input ${STUBS[name] === type ? 'checked' : ''} onclick="fetch('/api/admin/set/${name}/${type}')" type="radio" name="${name}">
${type}
</label>`;
}

View File

@ -2,63 +2,34 @@
/* eslint-disable @typescript-eslint/no-require-imports */
const router = require('express').Router();
const commonError = { success: false, message: 'Что то пошло не так' };
const STUBS = require('./admin').STUBS;
const stubs = { masters: 'success', orders: 'success' };
const commonError = { success: false, message: 'Что-то пошло не так' };
const sleep = (duration = 300) => (req, res, next) => setTimeout(next, duration);
router.use(sleep());
router.get('/arm/masters', (req, res) => {
res
.status(/error/.test(stubs.masters) ? 500 : 200)
.status(/error/.test(STUBS.masters) ? 500 : 200)
.send(
/^error$/.test(stubs.masters)
/^error$/.test(STUBS.masters)
? commonError
: require(`../json/arm-masters/${stubs.masters}.json`),
: require(`../json/arm-masters/${STUBS.masters}.json`),
);
});
router.get('/arm/orders', (req, res) => {
res
.status(/error/.test(stubs.orders) ? 500 : 200)
.status(/error/.test(STUBS.orders) ? 500 : 200)
.send(
/^error$/.test(stubs.orders)
/^error$/.test(STUBS.orders)
? commonError
: require(`../json/arm-orders/${stubs.orders}.json`),
: require(`../json/arm-orders/${STUBS.orders}.json`),
);
});
router.get('/admin', (req, res) => {
res.send(`<div>
<fieldset >
<legend>Мастера</legend>
<label>
<input ${stubs.masters === 'success' ? 'checked' : ''} onclick="fetch('/api/admin/set/masters/success')" type="radio" name="masters">
success
</label>
<label>
<input ${stubs.masters === 'error' ? 'checked' : ''} onclick="fetch('/api/admin/set/masters/error')" type="radio" name="masters" >
error
</label>
</fieldset>
<fieldset >
<legend>Заказы</legend>
<label>
<input ${stubs.orders === 'success' ? 'checked' : ''} onclick="fetch('/api/admin/set/orders/success')" type="radio" name="orders">
success
</label>
<label>
<input ${stubs.orders === 'error' ? 'checked' : ''} onclick="fetch('/api/admin/set/orders/error')" type="radio" name="orders" >
error
</label>
</fieldset>
</div>`);
});
router.use('/admin', require('./admin'));
router.get('/admin/set/:name/:value', (req, res) => {
const { name, value } = req.params;
stubs[name] = value;
res.send('ok');
});
module.exports = router;
module.exports = router;