summaryrefslogtreecommitdiff
path: root/src/components/settings/ChangePassword.tsx
blob: d437b5e483d55d11a3502e6bc91cb48a9f375ba5 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'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 { changePassword } from '@/lib/api/ChangePassword';

export const ChangePasswordField = () => {
    const [currentPassword, setCurrentPassword] = useState('');
    const [newPassword, setNewPassword] = useState('');
    const [confirmPassword, setConfirmPassword] = useState('');
    const [loading, setLoading] = useState(false);
    const [error, setError] = useState<string | null>(null);

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

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

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

        setLoading(true);

        const res = await changePassword(
            currentPassword,
            newPassword,
            confirmPassword,
        );

        setLoading(false);

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

        setCurrentPassword('');
        setNewPassword('');
        setConfirmPassword('');
    };

    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="currentPassword"
                    value={currentPassword}
                    onChange={(e: any) => setCurrentPassword(e.target.value)}
                />

                <InputField
                    placeholder="Новый пароль"
                    isPassword
                    type="password"
                    name="newPassword"
                    value={newPassword}
                    onChange={(e: any) => setNewPassword(e.target.value)}
                />

                <InputField
                    placeholder="Повторите пароль"
                    isPassword
                    type="password"
                    name="confirmPassword"
                    value={confirmPassword}
                    onChange={(e: any) => setConfirmPassword(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]" />
        </div>
    );
};