Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
415 lines
16 KiB
HTML
415 lines
16 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>Mini CRM</title>
|
|
<style>
|
|
:root {
|
|
--bg: #f5f6f8; --panel: #ffffff; --ink: #1e2430; --muted: #6b7480;
|
|
--line: #dfe3e9; --accent: #2f6feb; --danger: #c0392b; --radius: 8px;
|
|
}
|
|
* { box-sizing: border-box; }
|
|
body { margin: 0; background: var(--bg); color: var(--ink);
|
|
font: 15px/1.45 -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
|
|
header { background: var(--panel); border-bottom: 1px solid var(--line);
|
|
padding: 12px 18px; display: flex; gap: 14px; align-items: center; flex-wrap: wrap; }
|
|
h1 { font-size: 18px; margin: 0; }
|
|
main { padding: 18px; display: grid; gap: 18px; }
|
|
section { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
|
|
h2 { font-size: 15px; margin: 0 0 10px; }
|
|
form { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; align-items: center; }
|
|
input, select, textarea, button { font: inherit; padding: 7px 9px; border: 1px solid var(--line);
|
|
border-radius: 6px; background: #fff; color: var(--ink); }
|
|
input[type="search"] { min-width: 260px; }
|
|
button { cursor: pointer; background: var(--accent); border-color: var(--accent); color: #fff; }
|
|
button.ghost { background: #fff; color: var(--ink); border-color: var(--line); }
|
|
button.danger { background: #fff; color: var(--danger); border-color: var(--line); }
|
|
ul { list-style: none; margin: 0; padding: 0; }
|
|
li { display: flex; gap: 10px; align-items: center; padding: 8px 0; border-top: 1px solid var(--line); }
|
|
li span.grow { flex: 1; }
|
|
.muted { color: var(--muted); font-size: 13px; }
|
|
.board { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 10px; }
|
|
.column { background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px; }
|
|
.column h3 { margin: 0 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
|
|
.card { background: #fff; border: 1px solid var(--line); border-radius: 6px; padding: 8px; margin-bottom: 8px; }
|
|
.card .actions { display: flex; gap: 6px; margin-top: 6px; }
|
|
.card .actions button { padding: 3px 7px; font-size: 12px; }
|
|
#status { color: var(--muted); font-size: 13px; }
|
|
#status.error { color: var(--danger); }
|
|
.editing { outline: 2px solid var(--accent); }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<header>
|
|
<h1>Mini CRM</h1>
|
|
<input id="search" type="search" placeholder="Search companies, contacts, deals" aria-label="Search">
|
|
<span id="status"></span>
|
|
</header>
|
|
<main>
|
|
<section>
|
|
<h2>Companies</h2>
|
|
<form id="company-form" data-resource="companies">
|
|
<input name="name" placeholder="Name" required maxlength="200">
|
|
<input name="website" placeholder="Website" maxlength="200">
|
|
<input name="industry" placeholder="Industry" maxlength="200">
|
|
<button type="submit">Save</button>
|
|
<button type="button" class="ghost" data-action="reset">Clear</button>
|
|
</form>
|
|
<ul id="companies"></ul>
|
|
</section>
|
|
|
|
<section>
|
|
<h2>Contacts</h2>
|
|
<form id="contact-form" data-resource="contacts">
|
|
<input name="name" placeholder="Name" required maxlength="200">
|
|
<input name="email" placeholder="Email" maxlength="200">
|
|
<input name="phone" placeholder="Phone" maxlength="200">
|
|
<select name="company_id"></select>
|
|
<button type="submit">Save</button>
|
|
<button type="button" class="ghost" data-action="reset">Clear</button>
|
|
</form>
|
|
<ul id="contacts"></ul>
|
|
</section>
|
|
|
|
<section>
|
|
<h2>Deals</h2>
|
|
<form id="deal-form" data-resource="deals">
|
|
<input name="title" placeholder="Title" required maxlength="200">
|
|
<input name="amount" placeholder="Amount" type="number" min="0" step="0.01" value="0">
|
|
<select name="stage"></select>
|
|
<select name="company_id"></select>
|
|
<select name="contact_id"></select>
|
|
<button type="submit">Save</button>
|
|
<button type="button" class="ghost" data-action="reset">Clear</button>
|
|
</form>
|
|
<div id="board" class="board"></div>
|
|
</section>
|
|
|
|
<section id="deal-panel" hidden>
|
|
<h2 id="deal-panel-title">Deal</h2>
|
|
<button type="button" class="ghost" id="deal-close">Close (Esc)</button>
|
|
<div id="deal-panel-meta" class="muted"></div>
|
|
<h3>Notes</h3>
|
|
<ul id="deal-notes"></ul>
|
|
<form id="note-form">
|
|
<input name="body" placeholder="Add a note" required maxlength="4000" style="flex:1">
|
|
<button type="submit">Add note</button>
|
|
</form>
|
|
<h3>Activity</h3>
|
|
<ul id="deal-activity"></ul>
|
|
</section>
|
|
</main>
|
|
|
|
<script>
|
|
"use strict";
|
|
const STAGES = ["lead", "qualified", "proposal", "won", "lost"];
|
|
const state = {
|
|
companies: [], contacts: [], deals: [], query: "", editing: {},
|
|
selectedDeal: null, notes: [], activity: []
|
|
};
|
|
|
|
function status(message, isError) {
|
|
const node = document.getElementById("status");
|
|
node.textContent = message || "";
|
|
node.className = isError ? "error" : "";
|
|
if (message) setTimeout(() => { if (node.textContent === message) node.textContent = ""; }, 4000);
|
|
}
|
|
|
|
async function api(path, options) {
|
|
const response = await fetch(path, Object.assign({ headers: { "Content-Type": "application/json" } }, options));
|
|
if (response.status === 204) return null;
|
|
const data = await response.json().catch(() => null);
|
|
if (!response.ok) throw new Error((data && data.error) || ("HTTP " + response.status));
|
|
return data;
|
|
}
|
|
|
|
function formValues(form) {
|
|
const values = {};
|
|
new FormData(form).forEach((value, key) => { values[key] = value; });
|
|
["company_id", "contact_id"].forEach((key) => {
|
|
if (key in values && values[key] === "") values[key] = null;
|
|
});
|
|
if ("amount" in values) values.amount = values.amount === "" ? 0 : Number(values.amount);
|
|
return values;
|
|
}
|
|
|
|
function fillForm(form, record) {
|
|
for (const field of form.elements) {
|
|
if (!field.name) continue;
|
|
const value = record ? record[field.name] : "";
|
|
field.value = value === null || value === undefined ? (field.name.endsWith("_id") ? "" : value === 0 ? 0 : "") : value;
|
|
}
|
|
}
|
|
|
|
function setEditing(resource, form, record) {
|
|
state.editing[resource] = record ? record.id : null;
|
|
fillForm(form, record || null);
|
|
form.classList.toggle("editing", Boolean(record));
|
|
form.querySelector("button[type=submit]").textContent = record ? "Update" : "Save";
|
|
}
|
|
|
|
async function load() {
|
|
const query = state.query ? "?q=" + encodeURIComponent(state.query) : "";
|
|
const [companies, contacts, deals] = await Promise.all([
|
|
api("/api/companies" + query), api("/api/contacts" + query), api("/api/deals" + query)
|
|
]);
|
|
state.companies = companies; state.contacts = contacts; state.deals = deals;
|
|
render();
|
|
}
|
|
|
|
function option(select, value, label) {
|
|
const node = document.createElement("option");
|
|
node.value = value; node.textContent = label;
|
|
select.appendChild(node);
|
|
}
|
|
|
|
function renderSelects() {
|
|
const stageSelect = document.querySelector('#deal-form [name="stage"]');
|
|
stageSelect.innerHTML = "";
|
|
STAGES.forEach((stage) => option(stageSelect, stage, stage));
|
|
for (const selector of ['#contact-form [name="company_id"]', '#deal-form [name="company_id"]']) {
|
|
const select = document.querySelector(selector);
|
|
const current = select.value;
|
|
select.innerHTML = "";
|
|
option(select, "", "no company");
|
|
state.companies.forEach((company) => option(select, company.id, company.name));
|
|
select.value = current;
|
|
}
|
|
const contactSelect = document.querySelector('#deal-form [name="contact_id"]');
|
|
const currentContact = contactSelect.value;
|
|
contactSelect.innerHTML = "";
|
|
option(contactSelect, "", "no contact");
|
|
state.contacts.forEach((contact) => option(contactSelect, contact.id, contact.name));
|
|
contactSelect.value = currentContact;
|
|
}
|
|
|
|
function makeRow(label, detail, actions) {
|
|
const row = document.createElement("li");
|
|
const text = document.createElement("span");
|
|
text.className = "grow";
|
|
text.textContent = label;
|
|
row.appendChild(text);
|
|
if (detail) {
|
|
const extra = document.createElement("span");
|
|
extra.className = "muted";
|
|
extra.textContent = detail;
|
|
row.appendChild(extra);
|
|
}
|
|
actions.forEach((action) => row.appendChild(action));
|
|
return row;
|
|
}
|
|
|
|
function actionButton(label, className, handler) {
|
|
const button = document.createElement("button");
|
|
button.type = "button";
|
|
button.textContent = label;
|
|
button.className = className;
|
|
button.addEventListener("click", handler);
|
|
return button;
|
|
}
|
|
|
|
function renderCompanies() {
|
|
const list = document.getElementById("companies");
|
|
list.innerHTML = "";
|
|
state.companies.forEach((company) => {
|
|
const form = document.getElementById("company-form");
|
|
list.appendChild(makeRow(company.name, [company.industry, company.website].filter(Boolean).join(" · "), [
|
|
actionButton("Edit", "ghost", () => setEditing("companies", form, company)),
|
|
actionButton("Delete", "danger", async () => {
|
|
await api("/api/companies/" + company.id, { method: "DELETE" });
|
|
status("Deleted company " + company.name);
|
|
await load();
|
|
})
|
|
]));
|
|
});
|
|
}
|
|
|
|
function renderContacts() {
|
|
const list = document.getElementById("contacts");
|
|
list.innerHTML = "";
|
|
state.contacts.forEach((contact) => {
|
|
const company = state.companies.find((item) => item.id === contact.company_id);
|
|
const form = document.getElementById("contact-form");
|
|
list.appendChild(makeRow(contact.name, [contact.email, contact.phone, company ? company.name : null].filter(Boolean).join(" · "), [
|
|
actionButton("Edit", "ghost", () => setEditing("contacts", form, contact)),
|
|
actionButton("Delete", "danger", async () => {
|
|
await api("/api/contacts/" + contact.id, { method: "DELETE" });
|
|
status("Deleted contact " + contact.name);
|
|
await load();
|
|
})
|
|
]));
|
|
});
|
|
}
|
|
|
|
function renderBoard() {
|
|
const board = document.getElementById("board");
|
|
board.innerHTML = "";
|
|
STAGES.forEach((stage) => {
|
|
const column = document.createElement("div");
|
|
column.className = "column";
|
|
const heading = document.createElement("h3");
|
|
const deals = state.deals.filter((deal) => deal.stage === stage);
|
|
const total = deals.reduce((sum, deal) => sum + Number(deal.amount || 0), 0);
|
|
heading.textContent = stage + " (" + deals.length + " · " + total.toFixed(2) + ")";
|
|
column.appendChild(heading);
|
|
deals.forEach((deal) => {
|
|
const card = document.createElement("div");
|
|
card.className = "card";
|
|
const title = document.createElement("div");
|
|
title.textContent = deal.title;
|
|
card.appendChild(title);
|
|
const meta = document.createElement("div");
|
|
meta.className = "muted";
|
|
const company = state.companies.find((item) => item.id === deal.company_id);
|
|
meta.textContent = Number(deal.amount || 0).toFixed(2) + (company ? " · " + company.name : "");
|
|
card.appendChild(meta);
|
|
const actions = document.createElement("div");
|
|
actions.className = "actions";
|
|
const form = document.getElementById("deal-form");
|
|
actions.appendChild(actionButton("Edit", "ghost", () => setEditing("deals", form, deal)));
|
|
actions.appendChild(actionButton("Delete", "danger", async () => {
|
|
await api("/api/deals/" + deal.id, { method: "DELETE" });
|
|
if (state.selectedDeal && state.selectedDeal.id === deal.id) closeDeal();
|
|
status("Deleted deal " + deal.title);
|
|
await load();
|
|
}));
|
|
actions.appendChild(actionButton("Notes", "ghost", () => openDeal(deal)));
|
|
card.appendChild(actions);
|
|
column.appendChild(card);
|
|
});
|
|
board.appendChild(column);
|
|
});
|
|
}
|
|
|
|
function render() {
|
|
renderSelects();
|
|
renderCompanies();
|
|
renderContacts();
|
|
renderBoard();
|
|
renderDealPanel();
|
|
}
|
|
|
|
function renderDealPanel() {
|
|
const panel = document.getElementById("deal-panel");
|
|
const deal = state.selectedDeal;
|
|
if (!deal) {
|
|
panel.hidden = true;
|
|
return;
|
|
}
|
|
panel.hidden = false;
|
|
document.getElementById("deal-panel-title").textContent = "Deal #" + deal.id + ": " + deal.title;
|
|
const company = state.companies.find((item) => item.id === deal.company_id);
|
|
const contact = state.contacts.find((item) => item.id === deal.contact_id);
|
|
document.getElementById("deal-panel-meta").textContent = [
|
|
deal.stage, Number(deal.amount || 0).toFixed(2),
|
|
company ? "company: " + company.name : null,
|
|
contact ? "contact: " + contact.name : null
|
|
].filter(Boolean).join(" · ");
|
|
const list = document.getElementById("deal-notes");
|
|
list.innerHTML = "";
|
|
state.notes.forEach((note) => {
|
|
list.appendChild(makeRow(note.body, note.created_at, []));
|
|
});
|
|
if (!state.notes.length) {
|
|
const empty = document.createElement("li");
|
|
empty.className = "muted";
|
|
empty.textContent = "No notes yet.";
|
|
list.appendChild(empty);
|
|
}
|
|
const activityList = document.getElementById("deal-activity");
|
|
activityList.innerHTML = "";
|
|
state.activity.forEach((entry) => {
|
|
activityList.appendChild(makeRow(entry.action + " " + entry.summary, entry.created_at, []));
|
|
});
|
|
if (!state.activity.length) {
|
|
const empty = document.createElement("li");
|
|
empty.className = "muted";
|
|
empty.textContent = "No activity yet.";
|
|
activityList.appendChild(empty);
|
|
}
|
|
}
|
|
|
|
async function openDeal(deal) {
|
|
state.selectedDeal = deal;
|
|
state.notes = await api("/api/deals/" + deal.id + "/notes");
|
|
state.activity = await api("/api/activity?deal_id=" + deal.id + "&limit=20");
|
|
renderDealPanel();
|
|
}
|
|
|
|
function closeDeal() {
|
|
state.selectedDeal = null;
|
|
state.notes = [];
|
|
state.activity = [];
|
|
renderDealPanel();
|
|
}
|
|
|
|
document.getElementById("note-form").addEventListener("submit", async (event) => {
|
|
event.preventDefault();
|
|
if (!state.selectedDeal) return;
|
|
const form = event.target;
|
|
try {
|
|
const values = formValues(form);
|
|
await api("/api/deals/" + state.selectedDeal.id + "/notes", { method: "POST", body: JSON.stringify(values) });
|
|
form.reset();
|
|
await openDeal(state.selectedDeal);
|
|
status("Note added");
|
|
} catch (error) {
|
|
status(error.message, true);
|
|
}
|
|
});
|
|
|
|
document.getElementById("deal-close").addEventListener("click", closeDeal);
|
|
|
|
document.addEventListener("keydown", (event) => {
|
|
const target = event.target;
|
|
const typing = target && (target.tagName === "INPUT" || target.tagName === "TEXTAREA" || target.tagName === "SELECT");
|
|
if (event.key === "/" && !typing) {
|
|
event.preventDefault();
|
|
document.getElementById("search").focus();
|
|
} else if (event.key === "Escape" && state.selectedDeal) {
|
|
closeDeal();
|
|
}
|
|
});
|
|
|
|
function wireForm(form, resource) {
|
|
form.addEventListener("submit", async (event) => {
|
|
event.preventDefault();
|
|
const values = formValues(form);
|
|
const id = state.editing[resource];
|
|
try {
|
|
if (id) {
|
|
await api("/api/" + resource + "/" + id, { method: "PUT", body: JSON.stringify(values) });
|
|
status("Updated " + resource.slice(0, -1));
|
|
} else {
|
|
await api("/api/" + resource, { method: "POST", body: JSON.stringify(values) });
|
|
status("Created " + resource.slice(0, -1));
|
|
}
|
|
setEditing(resource, form, null);
|
|
await load();
|
|
} catch (error) {
|
|
status(error.message, true);
|
|
}
|
|
});
|
|
form.querySelector('[data-action="reset"]').addEventListener("click", () => setEditing(resource, form, null));
|
|
}
|
|
|
|
let searchTimer = null;
|
|
document.getElementById("search").addEventListener("input", (event) => {
|
|
clearTimeout(searchTimer);
|
|
const value = event.target.value;
|
|
searchTimer = setTimeout(() => {
|
|
state.query = value.trim();
|
|
load().catch((error) => status(error.message, true));
|
|
}, 200);
|
|
});
|
|
|
|
wireForm(document.getElementById("company-form"), "companies");
|
|
wireForm(document.getElementById("contact-form"), "contacts");
|
|
wireForm(document.getElementById("deal-form"), "deals");
|
|
|
|
load().catch((error) => status(error.message, true));
|
|
</script>
|
|
</body>
|
|
</html>
|