fix: preserve form values on error + rename Дата→Добавлена, Изменён→Изменена + bump 0.5.1

This commit is contained in:
2026-05-31 12:48:08 +03:00
parent bfeae74e71
commit 0abaa65bf8
3 changed files with 20 additions and 8 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "ipwhitelist", "name": "ipwhitelist",
"version": "0.5.0", "version": "0.5.1",
"description": "IP WhiteList microservice for cloud provider", "description": "IP WhiteList microservice for cloud provider",
"main": "server.js", "main": "server.js",
"scripts": { "scripts": {
+15 -3
View File
@@ -76,6 +76,8 @@ function createRouter() {
selectedCompany, selectedCompany,
error: req.query.error || null, error: req.query.error || null,
success: req.query.success || null, success: req.query.success || null,
lastValue: req.query.lastValue || '',
lastComment: req.query.lastComment || '',
csrfToken: '', csrfToken: '',
}); });
} catch (e) { } catch (e) {
@@ -84,7 +86,7 @@ function createRouter() {
user: req.user, isAdmin: req.user.isAdmin, user: req.user, isAdmin: req.user.isAdmin,
companies: [], selectedCompany: null, companies: [], selectedCompany: null,
error: 'Ошибка загрузки: ' + e.message, error: 'Ошибка загрузки: ' + e.message,
success: null, csrfToken: '', success: null, lastValue: '', lastComment: '', csrfToken: '',
}); });
} }
}); });
@@ -103,7 +105,12 @@ function createRouter() {
: 'Запись добавлена'; : 'Запись добавлена';
return res.redirect(back + (back.includes('?') ? '&' : '?') + 'success=' + encodeURIComponent(msg)); return res.redirect(back + (back.includes('?') ? '&' : '?') + 'success=' + encodeURIComponent(msg));
} }
res.redirect(back + (back.includes('?') ? '&' : '?') + 'error=' + encodeURIComponent((r.data && r.data.error) || 'Ошибка')); // Ошибка — возвращаем введённые значения чтобы не перепечатывать
const err = (r.data && r.data.error) || 'Ошибка';
res.redirect(back + (back.includes('?') ? '&' : '?') +
'error=' + encodeURIComponent(err) +
'&lastValue=' + encodeURIComponent(value || '') +
'&lastComment=' + encodeURIComponent(comment || ''));
} catch (e) { } catch (e) {
res.redirect(back + (back.includes('?') ? '&' : '?') + 'error=' + encodeURIComponent(e.message)); res.redirect(back + (back.includes('?') ? '&' : '?') + 'error=' + encodeURIComponent(e.message));
} }
@@ -120,7 +127,12 @@ function createRouter() {
if (r.status === 200) { if (r.status === 200) {
return res.redirect(back + (back.includes('?') ? '&' : '?') + 'success=' + encodeURIComponent('Запись обновлена')); return res.redirect(back + (back.includes('?') ? '&' : '?') + 'success=' + encodeURIComponent('Запись обновлена'));
} }
res.redirect(back + (back.includes('?') ? '&' : '?') + 'error=' + encodeURIComponent((r.data && r.data.error) || 'Ошибка')); // Ошибка — возвращаем введённые значения в модалку
const err = (r.data && r.data.error) || 'Ошибка';
res.redirect(back + (back.includes('?') ? '&' : '?') +
'error=' + encodeURIComponent(err) +
'&lastValue=' + encodeURIComponent(value || '') +
'&lastComment=' + encodeURIComponent(comment || ''));
} catch (e) { } catch (e) {
res.redirect(back + (back.includes('?') ? '&' : '?') + 'error=' + encodeURIComponent(e.message)); res.redirect(back + (back.includes('?') ? '&' : '?') + 'error=' + encodeURIComponent(e.message));
} }
+4 -4
View File
@@ -245,11 +245,11 @@
<div class="form-grid"> <div class="form-grid">
<div class="field"> <div class="field">
<label>IPv4 адрес или подсеть CIDR</label> <label>IPv4 адрес или подсеть CIDR</label>
<input name="value" placeholder="Например: 203.0.113.10 или 203.0.113.0/24" pattern="^(\d{1,3}\.){3}\d{1,3}(/\d{1,2})?$" maxlength="18" title="IPv4 адрес или CIDR /22/32" required <%= used >= limit ? 'disabled' : '' %>> <input name="value" placeholder="Например: 203.0.113.10 или 203.0.113.0/24" pattern="^(\d{1,3}\.){3}\d{1,3}(/\d{1,2})?$" maxlength="18" title="IPv4 адрес или CIDR /22/32" value="<%= lastValue || '' %>" required <%= used >= limit ? 'disabled' : '' %>>
</div> </div>
<div class="field"> <div class="field">
<label>Комментарий</label> <label>Комментарий</label>
<input name="comment" placeholder="Необязательно" maxlength="255"> <input name="comment" placeholder="Необязательно" maxlength="255" value="<%= lastComment || '' %>">
</div> </div>
<button class="btn btn-primary" type="submit" <%= used >= limit ? 'disabled' : '' %> style="align-self:end"> <button class="btn btn-primary" type="submit" <%= used >= limit ? 'disabled' : '' %> style="align-self:end">
<%= used >= limit ? 'Лимит исчерпан' : 'Добавить' %> <%= used >= limit ? 'Лимит исчерпан' : 'Добавить' %>
@@ -272,8 +272,8 @@
<th>Адрес / Подсеть</th> <th>Адрес / Подсеть</th>
<th>Комментарий</th> <th>Комментарий</th>
<th>Добавил</th> <th>Добавил</th>
<th>Дата</th> <th>Добавлена</th>
<th>Изменён</th> <th>Изменена</th>
<th class="text-center">Действия</th> <th class="text-center">Действия</th>
</tr> </tr>
</thead> </thead>