第5项:按页面管理导航、历史草稿与操作回执

This commit is contained in:
黄健铭 committed 2026-12-05 08:14:03 -08:00
1 parent f570929cfe
commit ff8b05973a
48 files changed
+1577 -574

No files matched your search

@@ -1,3 +1,5 @@
import { activeOrderEditKey, orderEditKey } from './receipts';
import { saveWithDraft } from '../../lib/receipt';
import { useOrderAccess, ownershipNoticeKey } from './useOrderAccess';
import { useCallback, useEffect, useEffectEvent, useRef, useState } from 'react';
import type { ReactNode, FormEvent, ComponentType } from 'react';
@@ -204,9 +206,9 @@ function ConflictPanel({
}
export function OrderEditPage({ client, state, session, refreshState, registerLeaveGuard, LockEditor }: PageProps) {
const activeKey = 'mm-order-edit-active-' + state.user.id;
const activeKey = activeOrderEditKey(state.user.id);
const id = searchParam('order') || searchParam('id') || localStorage.getItem(activeKey) || '';
const key = 'mm-order-edit-' + state.user.id + '-' + id;
const key = orderEditKey(state.user.id, id);
const [entry, setEntry] = useState<EditEntry | null>(null),
[epoch, reset] = useState(0),
[error, setError] = useState(''),
@@ -214,10 +216,12 @@ export function OrderEditPage({ client, state, session, refreshState, registerLe
[reading, setReading] = useState(false);
const live = useRef(false),
generation = useRef(0);
const ownDraft = useRef<string | undefined>(undefined);
const onDraft = useCallback<SaveDraft>(
(inputs, reason, verified) => {
if (!live.current || client.user !== state.user.id) return;
localStorage.setItem(key, JSON.stringify({ id, revision: inputs.revision, inputs, reason, verified }));
ownDraft.current = JSON.stringify({ id, revision: inputs.revision, inputs, reason, verified });
localStorage.setItem(key, ownDraft.current);
localStorage.setItem(activeKey, id);
},
[id, key, activeKey, client, state.user.id],
@@ -281,9 +285,10 @@ export function OrderEditPage({ client, state, session, refreshState, registerLe
return () => {
disposed = true;
cancelRead();
if (localStorage.getItem(activeKey) === id) localStorage.removeItem(activeKey);
if (localStorage.getItem(activeKey) === id && localStorage.getItem(key) === ownDraft.current)
localStorage.removeItem(activeKey);
};
}, [id, activeKey]);
}, [id, activeKey, key]);
const go = () => navigate('orders', { search: '?order=' + encodeURIComponent(id) });
async function save(inputs: EditPayload, reason: string, verified: boolean, approval: boolean) {
if (!entry) return;
@@ -291,12 +296,15 @@ export function OrderEditPage({ client, state, session, refreshState, registerLe
if (entry.conflicted) throw new Error('订单已有新记录,请先读取最新订单并核对,填写内容已经保留。');
if (!reason.trim() || reason.trim().length > 200) throw new Error('请填写1~200字的修改原因。');
if (!verified) throw new Error('请先核实本次资料更正,并勾选确认。');
await client.mutate('/api/orders/' + encodeURIComponent(id) + (approval ? '/change' : '/edit'), {
const url = '/api/orders/' + encodeURIComponent(id) + (approval ? '/change' : '/edit');
const payload = {
...inputs,
revision: entry.revision,
reason: reason.trim(),
verified: true,
});
};
if (ownDraft.current !== undefined) await saveWithDraft(client, url, payload, { key, value: ownDraft.current });
else await client.mutate(url, payload);
} catch (err) {
if (!active() || (await checkFailure(err)) || !active()) return;
let message = errorMessage(err).replace('不要重新开单', '不要重复保存修改');
@@ -322,8 +330,8 @@ export function OrderEditPage({ client, state, session, refreshState, registerLe
}
if (!active()) return;
live.current = false;
localStorage.removeItem(key);
localStorage.removeItem(activeKey);
if (localStorage.getItem(key) === null && localStorage.getItem(activeKey) === id)
localStorage.removeItem(activeKey);
if (approval) navigate('orders', { search: '?order=' + encodeURIComponent(id), fromPage: true });
else {
setSuccess(true);
@@ -0,0 +1,46 @@
import { clearRecovered, detailTarget, record, type ReceiptHandler } from '../../lib/receipt';
export const orderEditKey = (user: string, id: string) => `mm-order-edit-${user}-${id}`;
export const activeOrderEditKey = (user: string) => 'mm-order-edit-active-' + user;
export const orderReceipt: ReceiptHandler = {
accepts: url =>
/^\/api\/(?:orders\/[^/]+\/|order-(?:changes|cancellations|ownership-changes)\/)/.test(url) &&
!url.endsWith('/issue'),
finish: ({ job, result, state, storage }) => {
const path = job.url.split('/'),
direct = path[2] === 'orders',
id = direct
? decodeURIComponent(path[3])
: result.order_id || (record(result.order) && result.order.id) || result.id;
const data = record(job.data) ? job.data : {};
if (typeof id === 'string' && direct && (path[4] === 'edit' || (path[4] === 'change' && !data.action))) {
const cleared = clearRecovered(storage, orderEditKey(state.user.id, id), job, d =>
d.id === id && record(d.inputs)
? {
...d.inputs,
revision: d.revision,
reason: typeof d.reason === 'string' ? d.reason.trim() : d.reason,
verified: true,
}
: undefined,
);
const active = activeOrderEditKey(state.user.id);
if (cleared && storage.getItem(active) === id) storage.removeItem(active);
}
return detailTarget('orders', 'order', id);
},
};
export const afterSaleReceipt: ReceiptHandler = {
accepts: url => /^\/api\/after-sales(?:\/|$)/.test(url),
finish: ({ job, result }) => {
const action = job.url.split('/').at(-1),
data = record(job.data) ? job.data : {};
if (action === 'update')
return { message: result.applied === true ? '售后修改已保存' : '售后修改申请已提交,待店主审批' };
if (action === 'approve' || action === 'reject')
return { message: action === 'approve' ? '已同意售后修改' : '已驳回售后修改申请' };
return {
message: typeof data.date === 'string' ? '售后已保存,已计入 ' + data.date + ' 的费用与利润' : '售后已保存',
};
},
};
@@ -1,3 +1,4 @@
import { activeOrderEditKey, orderEditKey } from './receipts';
import { useCallback, useEffect, useRef } from 'react';
import type { LedgerState } from '../../shared';
@@ -39,8 +40,8 @@ export function useOrderAccess({ client, state, id, watch, onLost }: OrderAccess
(error: unknown, current = scope.current) => {
if (state.user.role !== 'assistant' || !denied(error)) return false;
if (!current || scope.current !== current || client.user !== current.user) return true;
localStorage.removeItem('mm-order-edit-' + user + '-' + id);
const active = 'mm-order-edit-active-' + user;
localStorage.removeItem(orderEditKey(user, id));
const active = activeOrderEditKey(user);
if (localStorage.getItem(active) === id) localStorage.removeItem(active);
scope.current = null;
lost.current(ownershipNotice);