feat(tariffs): configurable purchase order for periods and packages

The order of enabled_periods (period tariffs) and traffic_packages
(traffic tariffs) is now the storefront order everywhere — both the
Telegram keyboard and the web app. Only new tariffs-config tariffs are
affected; legacy subscription/traffic options are untouched.

- Stop sorting periods and traffic packages in the web app plans
  serializer so it follows the configured order, matching the bot
  keyboards that already iterate the lists as-is.
- Preserve the row order through the admin draft (load and save) instead
  of sorting by months.
- Add a reusable Sortable component to the UI library (native HTML5
  drag & drop with a grip handle; bits-ui/shadcn have no such primitive)
  and use it to reorder period rows and traffic package rows in the
  tariff editor.
This commit is contained in:
3252a8
2026-06-03 10:14:40 +03:00
parent 0a294b8bf8
commit 58de153370
13 changed files with 396 additions and 129 deletions
@@ -303,6 +303,24 @@ export function createTariffsStore({ api, onTariffsSaved, flash, at }) {
}));
}
function moveDraftRow(field, fromIndex, toIndex) {
state.update((s) => {
const rows = [...(s.tariffDraft[field] || [])];
if (
fromIndex === toIndex ||
fromIndex < 0 ||
toIndex < 0 ||
fromIndex >= rows.length ||
toIndex >= rows.length
) {
return s;
}
const [moved] = rows.splice(fromIndex, 1);
rows.splice(toIndex, 0, moved);
return { ...s, tariffDraft: { ...s.tariffDraft, [field]: rows } };
});
}
function updateState(updates) {
state.update((s) => ({ ...s, ...updates }));
}
@@ -326,5 +344,6 @@ export function createTariffsStore({ api, onTariffsSaved, flash, at }) {
deleteTariff,
addDraftRow,
removeDraftRow,
moveDraftRow,
};
}
+3 -3
View File
@@ -69,6 +69,8 @@ export function rowsFromPackages(packageSet, currency, valueKey) {
export function draftFromTariff(tariff, defaultCurrency = "rub") {
const currency = normalizeCurrencyKey(defaultCurrency);
const defaultPrices = tariff.prices?.[currency] || {};
// enabled_periods comes first so its order (the configured purchase order)
// is preserved; any extra price-only months are appended afterwards.
const months = new Set([
...(tariff.enabled_periods || []),
...Object.keys(defaultPrices).map(Number),
@@ -77,7 +79,6 @@ export function draftFromTariff(tariff, defaultCurrency = "rub") {
]);
const periodRows = [...months]
.filter((month) => Number.isFinite(month) && month > 0)
.sort((a, b) => a - b)
.map((month) => ({
months: month,
rub:
@@ -231,8 +232,7 @@ export function tariffFromDraft(draft, fallbackCurrency = "rub") {
if (seenMonths.has(row.months)) return false;
seenMonths.add(row.months);
return true;
})
.sort((a, b) => a.months - b.months);
});
tariff.monthly_gb = parseNumber(draft.monthly_gb, 0);
tariff.enabled_periods = rows.map((row) => row.months);
const defaultPrices = Object.fromEntries(rows.map((row) => [String(row.months), row.rub || 0]));
+1
View File
@@ -29,6 +29,7 @@ export {
FileText,
Gift,
Globe2,
GripVertical,
Home,
Info,
Key,
+1
View File
@@ -11,6 +11,7 @@ export { default as RadioGroup } from "./radio-group.svelte";
export { default as RadioGroupItem } from "./radio-group-item.svelte";
export { default as RangeInput } from "./range-input.svelte";
export { default as Skeleton } from "./skeleton.svelte";
export { default as Sortable } from "./sortable.svelte";
export { default as Spinner } from "./spinner.svelte";
export { default as ScrollArea } from "./scroll-area.svelte";
export { default as Textarea } from "./textarea.svelte";
@@ -0,0 +1,119 @@
<script>
import { cn } from "$lib/utils.js";
import { GripVertical } from "./icons.js";
// Reusable drag-to-reorder list. bits-ui / shadcn-svelte have no sortable
// primitive, so this wraps native HTML5 drag & drop with a grip handle.
// Each item is rendered through the default (scoped) slot, which receives
// `item`, `index` and `dragging`. The slot content fills the row alongside
// the leading drag handle, so pass a grid `class` whose first column matches
// the handle width.
export let items = [];
export let onReorder = () => {};
export let getKey = (item) => item;
export let handleLabel = "Drag to reorder";
export let disabled = false;
let className = "";
export { className as class };
export let containerClass = "";
let dragIndex = null;
let dropIndex = null;
function handleDragStart(event, index) {
if (disabled) return;
dragIndex = index;
dropIndex = index;
if (event.dataTransfer) {
event.dataTransfer.effectAllowed = "move";
// Firefox requires data to be set for a drag to start.
event.dataTransfer.setData("text/plain", String(index));
}
}
function handleDragOver(event, index) {
if (dragIndex === null) return;
event.preventDefault();
if (event.dataTransfer) event.dataTransfer.dropEffect = "move";
dropIndex = index;
}
function handleDrop(event, index) {
if (dragIndex === null) return;
event.preventDefault();
if (dragIndex !== index) onReorder(dragIndex, index);
dragIndex = null;
dropIndex = null;
}
function reset() {
dragIndex = null;
dropIndex = null;
}
</script>
<div class={cn("ui-sortable", containerClass)} role="list">
{#each items as item, index (getKey(item, index))}
<div
class={cn("ui-sortable-item", className)}
class:is-dragging={dragIndex === index}
class:is-drop-target={dropIndex === index && dragIndex !== index}
role="listitem"
on:dragover={(event) => handleDragOver(event, index)}
on:drop={(event) => handleDrop(event, index)}
on:dragend={reset}
>
<button
type="button"
class="ui-sortable-handle"
draggable={!disabled}
aria-label={handleLabel}
title={handleLabel}
on:dragstart={(event) => handleDragStart(event, index)}
>
<GripVertical size={14} />
</button>
<slot {item} {index} dragging={dragIndex === index} />
</div>
{/each}
</div>
<style>
.ui-sortable {
display: grid;
gap: 8px;
min-width: 0;
}
.ui-sortable-item.is-dragging {
opacity: 0.5;
}
.ui-sortable-item.is-drop-target {
outline: 2px dashed var(--admin-accent, #4f8cff);
outline-offset: 2px;
border-radius: 8px;
}
.ui-sortable-handle {
display: inline-flex;
align-items: center;
justify-content: center;
width: 24px;
height: 100%;
padding: 0;
border: 0;
background: transparent;
color: var(--admin-muted, inherit);
cursor: grab;
touch-action: none;
}
.ui-sortable-handle:hover {
color: var(--admin-text, inherit);
}
.ui-sortable-handle:active {
cursor: grabbing;
}
</style>