add new customer button and route
customers could only be created indirectly via new order. add a standalone /customers/new (GET+POST), reusing edit.njk for both create and edit by keying off customer.id.
This commit is contained in:
@@ -20,6 +20,38 @@ router.get('/', (req, res) => {
|
||||
res.render('customers/list.njk', { customers, q });
|
||||
});
|
||||
|
||||
router.get('/new', (req, res) => {
|
||||
res.render('customers/edit.njk', { customer: {} });
|
||||
});
|
||||
|
||||
router.post('/new', (req, res) => {
|
||||
const b = req.body as Record<string, string>;
|
||||
|
||||
const name = (b.name || '').trim();
|
||||
if (!name) return res.status(400).send('Name is required');
|
||||
|
||||
const info = db
|
||||
.prepare(
|
||||
`INSERT INTO customers
|
||||
(name, phone, alt_phone, email, address_line1, address_line2, city, state, zip, internal_note)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
||||
)
|
||||
.run(
|
||||
name,
|
||||
formatPhone(b.phone),
|
||||
formatPhone(b.alt_phone),
|
||||
b.email || null,
|
||||
b.address_line1 || null,
|
||||
b.address_line2 || null,
|
||||
b.city || null,
|
||||
b.state || null,
|
||||
b.zip || null,
|
||||
b.internal_note || null
|
||||
);
|
||||
|
||||
res.redirect(303, `/customers/${info.lastInsertRowid}/edit`);
|
||||
});
|
||||
|
||||
router.get('/:id/edit', (req, res) => {
|
||||
const customer = db.prepare('SELECT * FROM customers WHERE id = ?').get(req.params.id);
|
||||
if (!customer) return res.status(404).send('Customer not found');
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
{% extends "layout.njk" %}
|
||||
{% block title %}Edit {{ customer.name }} — Harding's Clocks{% endblock %}
|
||||
{% block title %}{% if customer.id %}Edit {{ customer.name }}{% else %}New Customer{% endif %} — Harding's Clocks{% endblock %}
|
||||
{% block content %}
|
||||
<div class="page-header">
|
||||
<h1>Edit Customer</h1>
|
||||
<h1>{% if customer.id %}Edit Customer{% else %}New Customer{% endif %}</h1>
|
||||
</div>
|
||||
|
||||
<form method="post" action="/customers/{{ customer.id }}/edit" class="stacked-form">
|
||||
<form method="post" action="{% if customer.id %}/customers/{{ customer.id }}/edit{% else %}/customers/new{% endif %}" class="stacked-form">
|
||||
<fieldset>
|
||||
<legend>Customer</legend>
|
||||
<div class="grid-2">
|
||||
<label>Name <input type="text" name="name" value="{{ customer.name }}" required></label>
|
||||
<label>Name <input type="text" name="name" value="{{ customer.name or '' }}" required></label>
|
||||
<label>Phone <input type="text" name="phone" value="{{ customer.phone or '' }}"></label>
|
||||
<label>Alt phone <input type="text" name="alt_phone" value="{{ customer.alt_phone or '' }}"></label>
|
||||
<label>Email <input type="email" name="email" value="{{ customer.email or '' }}"></label>
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
{% block content %}
|
||||
<div class="page-header">
|
||||
<h1>Customers</h1>
|
||||
<div class="header-actions">
|
||||
<a class="button-link" href="/customers/new">+ New Customer</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<form class="search-form" method="get" action="/customers">
|
||||
|
||||
Reference in New Issue
Block a user