'use client' import { useEffect, useState } from 'react' import { getSessionUser, updateProfile, changePassword, getSettings, getUserNotificationConfig, updateUserNotificationConfig } from '@/app/actions' import { Card, CardContent, CardHeader, CardTitle, CardFooter } from '@/components/ui/card' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, } from '@/components/ui/dialog' import { Loader2, Save, CheckCircle, ShieldCheck, UserCircle, KeyRound, LockKeyhole, MessageCircle, } from 'lucide-react' export default function ProfilePage() { const [user, setUser] = useState(null) const [name, setName] = useState('') const [photo, setPhoto] = useState('') const [loading, setLoading] = useState(true) // Notification Config state const [globalNotifEnabled, setGlobalNotifEnabled] = useState(false) const [mmChannelId, setMmChannelId] = useState('') const [mmActive, setMmActive] = useState(true) const [savingNotif, setSavingNotif] = useState(false) const [notifSuccess, setNotifSuccess] = useState(false) // Profile state const [savingProfile, setSavingProfile] = useState(false) const [profileError, setProfileError] = useState('') const [profileSuccess, setProfileSuccess] = useState(false) // Password state const [oldPassword, setOldPassword] = useState('') const [newPassword, setNewPassword] = useState('') const [confirmPassword, setConfirmPassword] = useState('') const [savingPassword, setSavingPassword] = useState(false) const [passwordError, setPasswordError] = useState('') const [passwordSuccess, setPasswordSuccess] = useState(false) const [isPasswordModalOpen, setIsPasswordModalOpen] = useState(false) useEffect(() => { loadUser() }, []) const loadUser = async () => { setLoading(true) const sessionUser = await getSessionUser() if (sessionUser) { setUser(sessionUser) setName(sessionUser.name) setPhoto(sessionUser.photo || '') const [globalSettings, notifConfig] = await Promise.all([ getSettings(), getUserNotificationConfig(sessionUser.id) ]) if (globalSettings?.MATTERMOST_NOTIF_ENABLED === 'true') { setGlobalNotifEnabled(true) } if (notifConfig?.success && notifConfig.config) { setMmChannelId(notifConfig.config.mattermost_channel_id || '') setMmActive(notifConfig.config.is_active ?? true) } } setLoading(false) } const handleSaveProfile = async (e: React.FormEvent) => { e.preventDefault() if (!name.trim()) { setProfileError('Nama wajib diisi') return } setProfileError('') setProfileSuccess(false) setSavingProfile(true) const [res, notifRes] = await Promise.all([ updateProfile(user.id, name.trim(), photo.trim() || null), globalNotifEnabled ? updateUserNotificationConfig(user.id, { mattermost_channel_id: mmChannelId, is_active: mmActive }) : Promise.resolve({ success: true, error: null }) ]) if (res.success && notifRes.success) { setUser(res.user) setProfileSuccess(true) setTimeout(() => setProfileSuccess(false), 3000) } else { setProfileError(res.error || notifRes.error || 'Terjadi kesalahan') } setSavingProfile(false) } const handleChangePassword = async (e: React.FormEvent) => { e.preventDefault() setPasswordError('') setPasswordSuccess(false) if (!oldPassword || !newPassword || !confirmPassword) { setPasswordError('Semua field wajib diisi') return } if (newPassword !== confirmPassword) { setPasswordError('Konfirmasi password tidak cocok') return } if (newPassword.length < 6) { setPasswordError('Password baru minimal 6 karakter') return } setSavingPassword(true) const res = await changePassword(user.id, oldPassword, newPassword) if (res.success) { setPasswordSuccess(true) setOldPassword('') setNewPassword('') setConfirmPassword('') setTimeout(() => { setPasswordSuccess(false) setIsPasswordModalOpen(false) }, 2000) } else { setPasswordError(res.error || 'Gagal mengubah password') } setSavingPassword(false) } if (loading) { return (
Memuat profil...
) } if (!user) { return (
Anda belum login.
) } return (
{/* Left 2 Cols: Forms */}
{/* PROFILE FORM */}

Informasi Akun

Ubah nama tampilan dan foto profil Anda.

{user.role}
{photo ? ( {name} ) : (
{name ? name.charAt(0).toUpperCase() : }
)}

Pratinjau Avatar

Masukkan tautan URL foto gambar di bawah untuk memperbarui gambar profil.

setName(e.target.value)} placeholder="Contoh: Budi Santoso" className="h-11 rounded-xl" />
setPhoto(e.target.value)} placeholder="https://..." className="h-11 rounded-xl" />
{globalNotifEnabled && (

Notifikasi Mattermost

Terima update pesanan PO langsung di DM Mattermost Anda.

setMmActive(e.target.checked)} className="mt-1 h-4 w-4 rounded border-slate-300 text-[#1B2CC1] focus:ring-[#1B2CC1]" />

Kirim notifikasi setiap kali ada yang merubah pesanannya di PO Anda.

setMmChannelId(e.target.value)} placeholder="Contoh: @firman atau 9dpxnitm..." className="h-11 rounded-xl" />
)} {profileError && (
{profileError}
)} {profileSuccess && (
Perubahan berhasil disimpan!
)}
{/* Right 1 Col: Account Details */}
Detail Akun
Username
{user.username}
Role Akses
{user.role}
{/* PASSWORD CHANGE MODAL BUTTON */}
Ganti Password Ganti Password

Lindungi akun Anda dengan mengubah password secara berkala.

setOldPassword(e.target.value)} className="h-11 rounded-xl pl-9" placeholder="Masukkan password lama" />
setNewPassword(e.target.value)} className="h-11 rounded-xl pl-9" placeholder="Minimal 6 karakter" />
setConfirmPassword(e.target.value)} className="h-11 rounded-xl pl-9" placeholder="Ketik ulang password baru" />
{passwordError && (
{passwordError}
)} {passwordSuccess && (
Password berhasil diubah!
)}
) }