Files
fredy/ui/src/views/login/Login.jsx

123 lines
3.5 KiB
React
Raw Normal View History

2025-12-11 10:40:55 +01:00
/*
* Copyright (c) 2026 by Christian Kellner.
2025-12-11 10:40:55 +01:00
* Licensed under Apache-2.0 with Commons Clause and Attribution/Naming Clause
*/
2025-07-23 08:47:26 +02:00
import React, { useEffect } from 'react';
import cityBackground from '../../assets/city_background.jpg';
import Logo from '../../components/logo/Logo';
2025-07-23 08:47:26 +02:00
import { xhrPost } from '../../services/xhr';
import { useLocation, useNavigate } from 'react-router-dom';
import { useActions, useSelector } from '../../services/state/store';
import { Input, Button, Banner } from '@douyinfe/semi-ui-19';
import './login.less';
2025-07-23 08:47:26 +02:00
import { IconUser, IconLock } from '@douyinfe/semi-icons';
2026-06-04 10:35:42 +02:00
import { useTranslation } from '../../services/i18n/i18n.jsx';
export default function Login() {
2026-06-04 10:35:42 +02:00
const t = useTranslation();
const actions = useActions();
2025-07-23 08:47:26 +02:00
const [username, setUserName] = React.useState('');
const [password, setPassword] = React.useState('');
const [error, setError] = React.useState(null);
const demoMode = useSelector((state) => state.demoMode.demoMode || false);
2025-09-02 20:18:37 +02:00
const navigate = useNavigate();
const location = useLocation();
2025-07-23 08:47:26 +02:00
useEffect(() => {
async function init() {
await actions.demoMode.getDemoMode();
2025-07-23 08:47:26 +02:00
}
2025-07-23 08:47:26 +02:00
init();
}, []);
2025-07-23 08:47:26 +02:00
const tryLogin = async () => {
if (!username?.trim() || !password) {
2026-06-04 10:35:42 +02:00
setError(t('login.errorMandatory'));
2025-07-23 08:47:26 +02:00
return;
}
setError(null);
2025-07-23 08:47:26 +02:00
try {
await xhrPost('/api/login', {
username: username.trim(),
2025-07-23 08:47:26 +02:00
password,
});
2025-09-03 14:22:04 +02:00
/* eslint-disable no-unused-vars */
} catch (ignored) {
2026-06-04 10:35:42 +02:00
setError(t('login.errorInvalid'));
2025-07-23 08:47:26 +02:00
return;
}
await actions.user.getCurrentUser();
navigate(location.state?.from?.pathname || '/dashboard');
2025-07-23 08:47:26 +02:00
};
2025-07-23 08:47:26 +02:00
return (
<div className="login">
<div className="login__bgImage" style={{ background: `url("${cityBackground}")` }} />
<div className="login__loginWrapper">
<div className="login__logoWrapper">
<Logo width={250} white />
</div>
2026-01-28 21:25:48 +01:00
{demoMode && (
<Banner
fullMode={true}
type="info"
bordered
closeIcon={null}
2026-06-04 10:35:42 +02:00
description={t('login.demoBanner')}
2026-01-28 21:25:48 +01:00
style={{ marginBottom: '1.5rem' }}
/>
)}
<form onSubmit={(e) => e.preventDefault()}>
{error && <Banner type="danger" closeIcon={null} description={error} style={{ marginBottom: '1rem' }} />}
<div className="login__inputGroup">
<Input
size="large"
prefix={<IconUser />}
2026-06-04 10:35:42 +02:00
placeholder={t('login.usernamePlaceholder')}
value={username}
showClear
autoFocus
onChange={(value) => setUserName(value)}
onKeyPress={async (e) => {
if (e.key === 'Enter') {
await tryLogin();
}
}}
/>
</div>
<div className="login__inputGroup">
<Input
size="large"
mode="password"
prefix={<IconLock />}
value={password}
2026-06-04 10:35:42 +02:00
placeholder={t('login.passwordPlaceholder')}
onChange={(value) => setPassword(value)}
onKeyPress={async (e) => {
if (e.key === 'Enter') {
await tryLogin();
}
}}
/>
</div>
<Button block type="primary" onClick={tryLogin} theme="solid" style={{ marginTop: '1rem' }}>
2026-06-04 10:35:42 +02:00
{t('login.loginButton')}
2025-07-23 08:47:26 +02:00
</Button>
</form>
</div>
2025-07-23 08:47:26 +02:00
</div>
);
}
Login.displayName = 'Login';