import { convertObjectToFormData, helpEmail } from '/lib/utils/misc.ts'; import { FormField, generateFieldHtml, getFormDataField } from '/lib/form-utils.tsx'; import { currencyMap, SupportedCurrencySymbol } from '/lib/types.ts'; import { isAppEnabled } from '/lib/config.ts'; interface SettingsProps { formData: Record; error?: { title: string; message: string; }; notice?: { title: string; message: string; }; currency?: SupportedCurrencySymbol; } export type Action = | 'change-email' | 'verify-change-email' | 'change-password' | 'change-dav-password' | 'delete-account' | 'change-currency'; export const actionWords = new Map([ ['change-email', 'change email'], ['verify-change-email', 'change email'], ['change-password', 'change password'], ['change-dav-password', 'change WebDav password'], ['delete-account', 'delete account'], ['change-currency', 'change currency'], ]); function formFields(action: Action, formData: FormData, currency?: SupportedCurrencySymbol) { const fields: FormField[] = [ { name: 'action', label: '', type: 'hidden', value: action, overrideValue: action, required: true, readOnly: true, }, ]; if (action === 'change-email') { fields.push({ name: 'email', label: 'Email', type: 'email', placeholder: 'jane.doe@example.com', value: getFormDataField(formData, 'email'), required: true, }); } else if (action === 'verify-change-email') { fields.push({ name: 'email', label: 'Email', type: 'email', placeholder: 'jane.doe@example.com', value: getFormDataField(formData, 'email'), required: true, }, { name: 'verification-code', label: 'Verification Code', description: `The verification code to validate your new email.`, type: 'text', placeholder: '000000', required: true, }); } else if (action === 'change-password') { fields.push({ name: 'current-password', label: 'Current Password', type: 'password', placeholder: 'super-SECRET-passphrase', required: true, }, { name: 'new-password', label: 'New Password', type: 'password', placeholder: 'super-SECRET-passphrase', required: true, }); } else if (action === 'change-dav-password') { fields.push({ name: 'new-dav-password', label: 'New WebDav Password', type: 'password', placeholder: 'super-SECRET-passphrase', required: true, description: 'Alternative password used for WebDav access and/or HTTP Basic Auth.', }); } else if (action === 'delete-account') { fields.push({ name: 'current-password', label: 'Password', type: 'password', placeholder: 'super-SECRET-passphrase', description: 'You need to input your password in order to delete your account.', required: true, }); } else if (action === 'change-currency') { fields.push({ name: 'currency', label: 'Currency', type: 'select', options: Array.from(currencyMap.keys()).map((currencySymbol) => ({ value: currencySymbol, label: `${currencySymbol} (${currencyMap.get(currencySymbol)})`, })), value: getFormDataField(formData, 'currency') || currency, required: true, }); } return fields; } export default function Settings({ formData: formDataObject, error, notice, currency }: SettingsProps) { const formData = convertObjectToFormData(formDataObject); const action = getFormDataField(formData, 'action') as Action; return ( <>
{error ? (

{error.title}

{error.message}

) : null} {notice ? (

{notice.title}

{notice.message}

) : null}

Change your email

{formFields( action === 'change-email' && notice?.message.includes('verify') ? 'verify-change-email' : 'change-email', formData, ).map((field) => generateFieldHtml(field, formData))}

Change your password

{formFields('change-password', formData).map((field) => generateFieldHtml(field, formData))}

Change your WebDav password

{formFields('change-dav-password', formData).map((field) => generateFieldHtml(field, formData))}
{isAppEnabled('expenses') ? ( <>

Change your currency

This is only used in the expenses app, visually. It changes nothing about the stored data or values.

{formFields('change-currency', formData, currency).map((field) => generateFieldHtml(field, formData))}
) : null}

Delete your account

Deleting your account is instant and deletes all your data. If you need help, please{' '} reach out.

{formFields('delete-account', formData).map((field) => generateFieldHtml(field, formData))}
); }