summaryrefslogtreecommitdiff
path: root/src/components/settings/SecurityPage.tsx
blob: dd650c4163d47a6fd15140bb673a47e71fc82d55 (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
92
93
94
95
96
97
98
99
100
101
import { InputField } from '@/components/ui/inputfield';
import { Button } from '@/components/ui';
import { Separator } from '@/components/ui';
import { useAuthContext } from '@/lib/contexts/Auth.context';
import LogoutButton from './LogoutButton';
import { changeEmail } from '@/lib/api/ChangeEmail';
import { useState } from 'react';
import { ChangePasswordField } from './ChangePassword';
export default function SecurityPage() {
    const { user } = useAuthContext();

    if (!user) return;

    const [email, setEmail] = useState('');
    const [password, setPassword] = useState('');
    const [loading, setLoading] = useState(false);

    if (!user) return null;

    const hasGoogle = !!user.google_id;
    const hasPassword = user.has_password;
    const showSetPassword = hasGoogle && !hasPassword;

    const handleChangeEmail = async () => {
        if (!email || !password) return;

        setLoading(true);

        const res = await changeEmail(email, password);

        setLoading(false);

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

        setEmail('');
        setPassword('');
    };
    return (
        <>
            <div className="flex flex-col flex-start gap-[40px] w-[900px] h-[816px]">
                <div className="flex flex-col gap-[20px] w-[900px] ">
                    {showSetPassword && (
                        <div className="flex flex-col flex-start gap-[20px] w-[310px] h-[241px]">
                            <p className="text-light-violet font-medium">
                                ЗАДАТЬ ПАРОЛЬ
                            </p>
                            <div className="flex flex-col flex-start gap-[10px] w-[310px] h-[137px]">
                                <InputField
                                    placeholder="Введите пароль"
                                    type="password"
                                    name="password"
                                />
                                <InputField
                                    isPassword
                                    placeholder="Повторите пароль"
                                    type="password"
                                    name="repeat_password"
                                />
                            </div>
                            <Button>Сменить</Button>
                            <Separator className="bg-violet/30 h-[1px]" />
                        </div>
                    )}

                    <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="Новая почта"
                                type="email"
                                name="email"
                                value={email}
                                onChange={(e) => setEmail(e.target.value)}
                            />

                            <InputField
                                isPassword
                                placeholder="Введите пароль"
                                type="password"
                                name="password"
                                value={password}
                                onChange={(e) => setPassword(e.target.value)}
                            />
                        </div>

                        <Button onClick={handleChangeEmail}>Сменить</Button>
                    </div>
                    <Separator className="bg-violet/30 h-[1px]" />
                    {hasPassword && <ChangePasswordField />}
                    <LogoutButton />
                </div>
            </div>
        </>
    );
}