← back to Interiordesignershowroom

lib/adminview.js

33 lines

// Shared admin-view helpers: dependency-free cookie parsing + the backend
// "see more detail" preference. Express can SET cookies natively (res.cookie);
// only READING them needs parsing, so we keep the repo's "no deps beyond
// express" rule and parse the Cookie header by hand.

function parseCookies(req) {
  const out = {};
  const raw = req.headers.cookie || '';
  for (const part of raw.split(';')) {
    const i = part.indexOf('=');
    if (i < 0) continue;
    const k = part.slice(0, i).trim();
    if (!k) continue;
    try { out[k] = decodeURIComponent(part.slice(i + 1).trim()); }
    catch (_) { out[k] = part.slice(i + 1).trim(); }
  }
  return out;
}

// Backend "Show details" preference. A ?details=1|0 query overrides AND persists
// to a cookie so the choice sticks across admin pages. ABSENCE = collapsed — Steve
// gets the tight view by default and expands to the raw fields when he wants them.
function detailPref(req, res) {
  if (req.query && req.query.details != null) {
    const on = String(req.query.details) === '1';
    res.cookie('ids_details', on ? '1' : '0', { maxAge: 180 * 24 * 3600e3, sameSite: 'lax', path: '/admin' });
    return on;
  }
  return parseCookies(req).ids_details === '1';
}

module.exports = { parseCookies, detailPref };