将账本业务页面、表单与审批迁移到 React,并加入旧版对照验收
This commit is contained in:
1 parent
f1a4e69f0d
commit
00dd683116
107 files changed
+31755
-191
No files matched your search
@@ -0,0 +1,310 @@
|
||||
import { useCallback, useEffect, useRef, useState, type FormEvent } from 'react';
|
||||
import { type LedgerClient } from '../../lib/api';
|
||||
import { useClientSnapshot } from '../../lib/useClient';
|
||||
import type { LedgerState, MemberRow, SessionInfo } from '../../shared';
|
||||
import { Button, Field } from './controls';
|
||||
import { errorText, legacyMembers, readForm, type LeaveChecks, type SettingsState, type Shop } from './settingsModel';
|
||||
import { Costs } from './Costs';
|
||||
import { Employees } from './Employees';
|
||||
import { PrintingPanel } from './Printing';
|
||||
import { ShopForm } from './Shop';
|
||||
import './settings.css';
|
||||
|
||||
export interface SettingsProps {
|
||||
client: LedgerClient;
|
||||
state: LedgerState;
|
||||
session: SessionInfo;
|
||||
refreshState: () => Promise<LedgerState>;
|
||||
onAccountChanged: () => void;
|
||||
registerLeaveGuard: (check: () => boolean) => () => void;
|
||||
}
|
||||
export function SettingsPage({
|
||||
client,
|
||||
state: raw,
|
||||
session,
|
||||
refreshState,
|
||||
onAccountChanged,
|
||||
registerLeaveGuard,
|
||||
}: SettingsProps) {
|
||||
const state = raw as SettingsState;
|
||||
const owner = state.user.role === 'owner',
|
||||
demo = state.mode === 'demo';
|
||||
const shop = state.shopSettings as Shop | undefined;
|
||||
const members: MemberRow[] = state.members?.length
|
||||
? state.members
|
||||
: shop?.members?.length
|
||||
? shop.members
|
||||
: legacyMembers;
|
||||
const team = !!(state.user.member || state.members?.some(m => m.member === 'assistant2'));
|
||||
const [shopDirty, setShopDirty] = useState(false),
|
||||
[printDirty, setPrintDirty] = useState(false),
|
||||
[costDirty, setCostDirty] = useState(false);
|
||||
const [shopSaving, setShopSaving] = useState(false),
|
||||
[printSaving, setPrintSaving] = useState(false);
|
||||
const [reloadKey, setReloadKey] = useState(0),
|
||||
[resetKey, setResetKey] = useState(0);
|
||||
const [message, setMessage] = useState({ text: '', error: false });
|
||||
const [serviceBusy, setServiceBusy] = useState(false);
|
||||
const employees = useRef<LeaveChecks>(null);
|
||||
const notice = useCallback((text: string, error = false) => setMessage({ text, error }), []);
|
||||
useClientSnapshot(client);
|
||||
const leave = useCallback(() => {
|
||||
if (employees.current && !employees.current.canLeave()) return false;
|
||||
if (shopSaving || printSaving) {
|
||||
notice('设置正在保存,请稍候。', true);
|
||||
return false;
|
||||
}
|
||||
return (
|
||||
!(shopDirty || printDirty || costDirty) ||
|
||||
window.confirm('店铺或打印设置尚未保存,离开会丢弃这些修改。继续离开?')
|
||||
);
|
||||
}, [shopSaving, printSaving, shopDirty, printDirty, costDirty, notice]);
|
||||
useEffect(() => registerLeaveGuard(leave), [registerLeaveGuard, leave]);
|
||||
useEffect(() => {
|
||||
const unload = (e: BeforeUnloadEvent) => {
|
||||
if (shopSaving || shopDirty || printDirty || costDirty || employees.current?.beforeUnload()) {
|
||||
e.preventDefault();
|
||||
e.returnValue = '';
|
||||
}
|
||||
};
|
||||
window.addEventListener('beforeunload', unload);
|
||||
return () => window.removeEventListener('beforeunload', unload);
|
||||
}, [shopSaving, shopDirty, printDirty, costDirty]);
|
||||
function resetDrafts() {
|
||||
setShopDirty(false);
|
||||
setPrintDirty(false);
|
||||
setCostDirty(false);
|
||||
setResetKey(k => k + 1);
|
||||
}
|
||||
async function savePassword(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
const data = readForm(event.currentTarget);
|
||||
try {
|
||||
if (shopDirty || printDirty) throw new Error('请先保存店铺与打印设置,再修改密码。');
|
||||
await client.mutate('/api/password', data);
|
||||
onAccountChanged();
|
||||
} catch (err) {
|
||||
notice(errorText(err), true);
|
||||
}
|
||||
}
|
||||
async function switchDemo(member: string) {
|
||||
if (!leave()) return;
|
||||
try {
|
||||
await client.api('/api/demo-login', { member });
|
||||
onAccountChanged();
|
||||
} catch (err) {
|
||||
notice(errorText(err), true);
|
||||
}
|
||||
}
|
||||
async function service(action: 'backup' | 'sync') {
|
||||
if (serviceBusy) return;
|
||||
if (action === 'backup' ? employees.current && !employees.current.canLeave() : !leave()) return;
|
||||
setServiceBusy(true);
|
||||
try {
|
||||
if (action === 'backup') {
|
||||
const result = await client.api<{ name: string }>('/api/backup', {});
|
||||
const link = document.createElement('a');
|
||||
link.href = '/api/backup/' + result.name;
|
||||
link.download = result.name;
|
||||
link.click();
|
||||
notice('完整备份已生成');
|
||||
} else {
|
||||
const result = await client.api<{ error?: string }>('/api/sync', {});
|
||||
notice(result.error || '同步重试完成,请查看状态', !!result.error);
|
||||
}
|
||||
await refreshState();
|
||||
resetDrafts();
|
||||
} catch (err) {
|
||||
notice(errorText(err), true);
|
||||
} finally {
|
||||
setServiceBusy(false);
|
||||
}
|
||||
}
|
||||
const account = state.user.member || session.user?.member || (owner ? 'owner' : 'assistant');
|
||||
const password = (
|
||||
<section className="panel">
|
||||
<h2>更改当前账号密码</h2>
|
||||
<form id="password-form" onSubmit={savePassword}>
|
||||
<div className="grid two">
|
||||
<Field label="原密码" name="current" type="password" required autoComplete="current-password" />
|
||||
<Field
|
||||
label="新密码(至少12个字符)"
|
||||
name="password"
|
||||
type="password"
|
||||
required
|
||||
minLength={12}
|
||||
autoComplete="new-password"
|
||||
/>
|
||||
</div>
|
||||
<div className="form-actions">
|
||||
<button disabled={client.busy}>修改并重新登录</button>
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
);
|
||||
return (
|
||||
<div
|
||||
className="settings-page"
|
||||
key={resetKey}
|
||||
onKeyDown={e => {
|
||||
if (
|
||||
e.target instanceof HTMLInputElement &&
|
||||
e.target.type === 'number' &&
|
||||
['ArrowUp', 'ArrowDown'].includes(e.key)
|
||||
)
|
||||
e.preventDefault();
|
||||
}}
|
||||
onWheel={e => {
|
||||
if (e.target instanceof HTMLInputElement && e.target.type === 'number' && document.activeElement === e.target)
|
||||
e.target.blur();
|
||||
}}
|
||||
>
|
||||
{owner && team && (
|
||||
<section className="panel team-settings">
|
||||
<div className="panel-title">
|
||||
<h2>成员与账号</h2>
|
||||
<span className="small muted">当前:{members.find(m => m.member === account)?.label || '待确认'}</span>
|
||||
</div>
|
||||
{demo && (
|
||||
<>
|
||||
<p className="note">三人功能预览·虚构数据,切换身份体验各自订单与账目。</p>
|
||||
<div className="actions">
|
||||
{members
|
||||
.filter(m => m.enabled !== false)
|
||||
.map(m => (
|
||||
<Button key={m.member} action={'demo-' + m.member} onClick={() => switchDemo(m.member)}>
|
||||
切换{m.label}
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
<p className="small muted">员工姓名、登录名、密码及启停状态在「员工管理」中维护。</p>
|
||||
</section>
|
||||
)}
|
||||
<div className="page-heading">
|
||||
<div>
|
||||
<div className="eyebrow">{owner ? 'SHOP & PRINT SETTINGS' : 'ACCOUNT'}</div>
|
||||
<h1>{owner ? '店铺与打印设置' : '修改密码'}</h1>
|
||||
<p className="muted">
|
||||
{owner ? '店铺身份、成员和默认值统一配置;打印布局按实际纸张校准。' : '更改本人登录密码,保存后重新登录。'}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
{owner ? (
|
||||
<>
|
||||
{shop && (
|
||||
<section id="shop-settings-panel" className="panel shop-settings">
|
||||
<ShopForm
|
||||
client={client}
|
||||
shop={shop}
|
||||
today={state.today}
|
||||
members={members}
|
||||
team={team}
|
||||
busy={shopSaving || printSaving}
|
||||
dirty={shopDirty}
|
||||
onDirty={setShopDirty}
|
||||
onBusy={setShopSaving}
|
||||
onSaved={async () => {
|
||||
await refreshState();
|
||||
setReloadKey(k => k + 1);
|
||||
}}
|
||||
notice={notice}
|
||||
/>
|
||||
<Costs
|
||||
client={client}
|
||||
shop={shop}
|
||||
members={members}
|
||||
team={team}
|
||||
busy={shopSaving || printSaving}
|
||||
dirty={costDirty}
|
||||
onDirty={setCostDirty}
|
||||
onBusy={setShopSaving}
|
||||
onSaved={async () => {
|
||||
await refreshState();
|
||||
}}
|
||||
notice={notice}
|
||||
/>
|
||||
</section>
|
||||
)}
|
||||
<Employees
|
||||
ref={employees}
|
||||
client={client}
|
||||
shopDirty={shopDirty || printDirty}
|
||||
reloadKey={reloadKey}
|
||||
onSaved={refreshState}
|
||||
onAccountChanged={onAccountChanged}
|
||||
notice={notice}
|
||||
/>
|
||||
<PrintingPanel
|
||||
client={client}
|
||||
printing={state.printing}
|
||||
revision={shop?.revision}
|
||||
orderId={state.orders[0]?.id}
|
||||
shopDirty={shopDirty}
|
||||
dirty={printDirty}
|
||||
onDirty={setPrintDirty}
|
||||
onBusy={setPrintSaving}
|
||||
refresh={refreshState}
|
||||
notice={notice}
|
||||
/>
|
||||
<div className="grid two">
|
||||
<section className="panel">
|
||||
<h2>数据库与备份</h2>
|
||||
<p>
|
||||
{state.mode === 'production'
|
||||
? '主账保存在部署服务器的独立SQLite数据库。'
|
||||
: '现在运行在本机试用环境,云端正式数据库尚未部署。'}
|
||||
</p>
|
||||
<p className="small muted">
|
||||
启动时和运行期间每6小时生成完整数据库备份。本机同盘备份不能防电脑损坏,正式部署还需异地备份。
|
||||
</p>
|
||||
{state.backup?.last && <p className="small">本次运行最近备份:{state.backup.last.time}</p>}
|
||||
{state.backup?.error && <p className="error">{state.backup.error}</p>}
|
||||
<Button action="backup" disabled={serviceBusy || client.busy} onClick={() => service('backup')}>
|
||||
生成并下载完整备份
|
||||
</Button>
|
||||
</section>
|
||||
<section className="panel">
|
||||
<h2>飞书查看副本</h2>
|
||||
<p>
|
||||
<span className={'badge ' + (state.sync.enabled ? '' : 'warn')}>
|
||||
{state.sync.enabled ? '已配置' : '尚未连接'}
|
||||
</span>
|
||||
</p>
|
||||
<p className="small muted">
|
||||
使用新账专用表单向同步。修改和复核在本系统完成;同步失败不重开订单、不影响本地账务保存。
|
||||
</p>
|
||||
{state.sync.enabled ? (
|
||||
<>
|
||||
<p>
|
||||
待同步 {state.sync.pending} 项 · 失败 {state.sync.failed} 项
|
||||
</p>
|
||||
{state.sync.error && <p className="error">{state.sync.error}</p>}
|
||||
<Button action="sync" disabled={serviceBusy || client.busy} onClick={() => service('sync')}>
|
||||
立即重试同步
|
||||
</Button>
|
||||
</>
|
||||
) : (
|
||||
<p className="note">已预留应用API同步通道。正式连接时再配置新表,未连接原销售表,未试写真实账务。</p>
|
||||
)}
|
||||
</section>
|
||||
</div>
|
||||
{!demo && password}
|
||||
</>
|
||||
) : demo ? (
|
||||
<section className="panel">
|
||||
<p className="muted">演示账号不提供密码修改。</p>
|
||||
</section>
|
||||
) : (
|
||||
password
|
||||
)}
|
||||
{message.text && (
|
||||
<div className={'settings-notice ' + (message.error ? 'error' : '')} role={message.error ? 'alert' : 'status'}>
|
||||
{message.text}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
export default SettingsPage;
|
||||
Reference in new issue
Block a user