(props)
| 43 | }; |
| 44 | |
| 45 | export const AddSecureNoteForm: Component<AddSecureNoteFormProps> = (props) => { |
| 46 | const toast = useToast(); |
| 47 | const [form, setForm] = createStore<FormState>({ ...INITIAL_FORM }); |
| 48 | const [folders] = createResource(() => props.open ? true : undefined, listFolders); |
| 49 | |
| 50 | const resetForm = () => { |
| 51 | setForm({ ...INITIAL_FORM }); |
| 52 | }; |
| 53 | |
| 54 | const clearError = (field: string) => { |
| 55 | if (form.errors[field]) { |
| 56 | setForm("errors", field, undefined!); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | const validateForm = (): Record<string, string> => { |
| 61 | const errors: Record<string, string> = {}; |
| 62 | const trimmedName = form.name.trim(); |
| 63 | if (!trimmedName) { |
| 64 | errors.name = t("notes.add.errors.titleRequired"); |
| 65 | } else if (trimmedName.length > 100) { |
| 66 | errors.name = t("notes.add.errors.titleTooLong"); |
| 67 | } |
| 68 | if (!form.body.trim()) { |
| 69 | errors.body = t("notes.add.errors.bodyRequired"); |
| 70 | } |
| 71 | return errors; |
| 72 | }; |
| 73 | |
| 74 | const handleSubmit = async () => { |
| 75 | if (form.isSubmitting) return; |
| 76 | |
| 77 | const errors = validateForm(); |
| 78 | if (Object.keys(errors).length > 0) { |
| 79 | setForm("errors", errors); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | setForm("isSubmitting", true); |
| 84 | try { |
| 85 | await addEntry({ |
| 86 | entryType: "secure_note", |
| 87 | name: form.name.trim(), |
| 88 | secret: form.body, |
| 89 | tags: form.tags.length > 0 ? form.tags : undefined, |
| 90 | folderId: form.folderId || undefined, |
| 91 | }); |
| 92 | toast.success(t("notes.add.success")); |
| 93 | resetForm(); |
| 94 | props.onSuccess(); |
| 95 | props.onClose(); |
| 96 | } catch (err) { |
| 97 | toast.error(typeof err === "string" ? err : t("notes.add.error")); |
| 98 | } finally { |
| 99 | setForm("isSubmitting", false); |
| 100 | } |
| 101 | }; |
| 102 |
nothing calls this directly
no test coverage detected