import { t, Trans } from "@lingui/macro" import { Anchor, Box, Button, Checkbox, Divider, Group, Input, PasswordInput, Stack, Text, TextInput } from "@mantine/core" import { useForm } from "@mantine/form" import { openConfirmModal } from "@mantine/modals" import { client, errorToStrings } from "app/client" import { redirectToLogin, redirectToSelectedSource } from "app/slices/redirect" import { reloadProfile } from "app/slices/user" import { useAppDispatch, useAppSelector } from "app/store" import { ProfileModificationRequest } from "app/types" import { Alert } from "components/Alert" import { useEffect } from "react" import { useAsyncCallback } from "react-async-hook" import { TbDeviceFloppy, TbTrash } from "react-icons/tb" interface FormData extends ProfileModificationRequest { newPasswordConfirmation?: string } export function ProfileSettings() { const profile = useAppSelector(state => state.user.profile) const dispatch = useAppDispatch() const form = useForm({ validate: { newPasswordConfirmation: (value, values) => (value !== values.newPassword ? t`Passwords do not match` : null), }, }) const { setValues } = form const saveProfile = useAsyncCallback(client.user.saveProfile, { onSuccess: () => { dispatch(reloadProfile()) dispatch(redirectToSelectedSource()) }, }) const deleteProfile = useAsyncCallback(client.user.deleteProfile, { onSuccess: () => { dispatch(redirectToLogin()) }, }) const openDeleteProfileModal = () => openConfirmModal({ title: Delete account, children: ( Are you sure you want to delete your account? There's no turning back! ), labels: { confirm: Confirm, cancel: Cancel }, confirmProps: { color: "red" }, onConfirm: () => deleteProfile.execute(), }) useEffect(() => { if (!profile) return setValues({ currentPassword: "", email: profile.email ?? "", newApiKey: false, }) }, [setValues, profile]) return ( <> {saveProfile.error && ( )} {deleteProfile.error && ( )}
User name} readOnly value={profile?.name} /> API key} readOnly value={profile?.apiKey} /> OPML export} description={ Export your subscriptions and categories as an OPML file that can be imported in other feed reading services } > Download Fever API} description={ CommaFeed is compatible with the Fever API. Use the following URL in your Fever-compatible mobile client. The username is your user name and the password is your API key. } > Fever API URL Current password} description={Enter your current password to change profile settings} required {...form.getInputProps("currentPassword")} /> E-mail} {...form.getInputProps("email")} required /> New password} description={Changing password will generate a new API key} {...form.getInputProps("newPassword")} /> Confirm password} {...form.getInputProps("newPasswordConfirmation")} /> Generate new API key} {...form.getInputProps("newApiKey", { type: "checkbox" })} />
) }