summaryrefslogtreecommitdiff
path: root/src/components/settings/SetPasswordField.tsx
blob: 6030c8fff97f81c8901091ac2bfb5e8bd29ded0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use client';

import { useState } from 'react';
import { InputField } from '@/components/ui/inputfield';
import { Button } from '@/components/ui/button';
import { Separator } from '@/components/ui/separator';
import { setPassword } from '@/lib/api/SetPassword';

export const SetPasswordField = () => {
    const [newPassword, setNewPassword] = useState('');
    const [repeatPassword, setRepeatPassword] = useState('');
    const [loading, setLoading] = useState(false);
    const [error, setError] = useState<string | null>(null);

    const handleSubmit = async () => {
        setError(null);

        if (newPassword !== repeatPassword) {
            setError('Пароли не совпадают');
            return;
        }

        if (newPassword.length < 8) {
            setError('Пароль должен быть минимум 8 символов');
            return;
        }

        setLoading(true);

        const res = await setPassword(newPassword, repeatPassword);

        setLoading(false);

        if (res.error) {
            setError(res.error.general);
            return;
        }

        setNewPassword('');
        setRepeatPassword('');
    };

    return (
        <div className="flex flex-col gap-[20px] w-[310px]">
            <p className="text-light-violet font-medium">ЗАДАТЬ ПАРОЛЬ</p>

            <div className="flex flex-col gap-[10px]">
                <InputField
                    placeholder="Введите пароль"
                    isPassword
                    type="password"
                    name="new_password"
                    value={newPassword}
                    onChange={(e: any) => setNewPassword(e.target.value)}
                />

                <InputField
                    placeholder="Повторите пароль"
                    isPassword
                    type="password"
                    name="repeat_password"
                    value={repeatPassword}
                    onChange={(e: any) => setRepeatPassword(e.target.value)}
                />
            </div>

            {error && <p className="text-red text-sm">{error}</p>}

            <Button onClick={handleSubmit} disabled={loading}>
                {loading ? 'Сохранение...' : 'Сменить'}
            </Button>

            <Separator className="bg-violet/30 h-[1px] w-[900px]" />
        </div>
    );
};