feat: extract admin (#43)
All checks were successful
it-academy/dry-wash-pl/pipeline/head This commit looks good
All checks were successful
it-academy/dry-wash-pl/pipeline/head This commit looks good
This commit is contained in:
parent
5ef0d5953d
commit
7c157574c8
38
stubs/api/admin.js
Normal file
38
stubs/api/admin.js
Normal 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>`;
|
||||||
|
}
|
@ -2,63 +2,34 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-require-imports */
|
/* eslint-disable @typescript-eslint/no-require-imports */
|
||||||
const router = require('express').Router();
|
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) => {
|
router.get('/arm/masters', (req, res) => {
|
||||||
res
|
res
|
||||||
.status(/error/.test(stubs.masters) ? 500 : 200)
|
.status(/error/.test(STUBS.masters) ? 500 : 200)
|
||||||
.send(
|
.send(
|
||||||
/^error$/.test(stubs.masters)
|
/^error$/.test(STUBS.masters)
|
||||||
? commonError
|
? commonError
|
||||||
: require(`../json/arm-masters/${stubs.masters}.json`),
|
: require(`../json/arm-masters/${STUBS.masters}.json`),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/arm/orders', (req, res) => {
|
router.get('/arm/orders', (req, res) => {
|
||||||
res
|
res
|
||||||
.status(/error/.test(stubs.orders) ? 500 : 200)
|
.status(/error/.test(STUBS.orders) ? 500 : 200)
|
||||||
.send(
|
.send(
|
||||||
/^error$/.test(stubs.orders)
|
/^error$/.test(STUBS.orders)
|
||||||
? commonError
|
? commonError
|
||||||
: require(`../json/arm-orders/${stubs.orders}.json`),
|
: require(`../json/arm-orders/${STUBS.orders}.json`),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/admin', (req, res) => {
|
router.use('/admin', require('./admin'));
|
||||||
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.get('/admin/set/:name/:value', (req, res) => {
|
|
||||||
const { name, value } = req.params;
|
|
||||||
|
|
||||||
stubs[name] = value;
|
|
||||||
|
|
||||||
res.send('ok');
|
|
||||||
});
|
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
Loading…
Reference in New Issue
Block a user