/* global React, Icon, Eyebrow, Button, api */ const { useState: useStateSP, useEffect: useEffectSP } = React; function SettingsPanel({ profile, onSaved }){ const [form, setForm] = useStateSP({ handle: (profile && profile.handle) || "", display_name: (profile && profile.display_name) || "", country: (profile && profile.country) || "", payout_method: (profile && profile.payout_method) || "paypal", paypal_email: (profile && profile.paypal_email) || "", bank_summary: (profile && profile.bank_details && profile.bank_details.summary) || "", bank_iban: (profile && profile.bank_details && profile.bank_details.iban) || "", bank_swift: (profile && profile.bank_details && profile.bank_details.swift) || "", bank_holder: (profile && profile.bank_details && profile.bank_details.account_holder) || "", }); const [busy, setBusy] = useStateSP(false); const [okMsg, setOkMsg] = useStateSP(""); const [errMsg, setErrMsg] = useStateSP(""); const set = (k,v) => setForm(f => ({...f, [k]: v})); const save = async () => { setOkMsg(""); setErrMsg(""); setBusy(true); const patch = { handle: form.handle ? (form.handle.startsWith("@") ? form.handle : "@"+form.handle) : null, display_name: form.display_name || null, country: form.country || null, payout_method: form.payout_method, }; if (form.payout_method === "paypal") { patch.paypal_email = form.paypal_email || null; patch.bank_details = null; } else { patch.paypal_email = null; patch.bank_details = { summary: form.bank_summary || null, iban: form.bank_iban || null, swift: form.bank_swift || null, account_holder: form.bank_holder || null, }; } const r = await api.updateMyProfile(patch); setBusy(false); if (r.error) { setErrMsg(r.error.message || "Save failed."); return; } setOkMsg("Saved."); onSaved && onSaved(r.data); setTimeout(() => setOkMsg(""), 2400); }; return (