Skip to content

Commit

Permalink
Improved form implementation for contacts import list
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurczewski committed Dec 9, 2024
1 parent 1619404 commit 5462388
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 42 deletions.
1 change: 0 additions & 1 deletion libs/generic-view/models/src/lib/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const configValidator: z.ZodType<Config | undefined> = z
reValidateMode: z.enum(["onChange", "onBlur", "onSubmit"]).optional(),
defaultValues: z.record(z.string(), z.any()).optional(),
}),
defaultValues: z.record(z.string(), z.any()).optional(),
})
.optional()

Expand Down
5 changes: 0 additions & 5 deletions libs/generic-view/ui/src/lib/generated/mc-contacts-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ export const generateMcContactsView: ComponentGenerator<McContactsView> = (
activeContactId: null,
},
},
defaultValues: {
selectedContacts: [],
allContacts: [],
searchedContact: "",
},
},
childrenKeys: ["contactsLoader"],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ export const CheckboxInput: APIFC<undefined, Config> = ({
[config, fieldRegistrar, getValues, inputName, multiSelect, setValue]
)

const handleRef = useCallback(
(e: HTMLInputElement) => {
inputRef.current = e
fieldRegistrar.ref(e)
},
[fieldRegistrar]
)

useEffect(() => {
if (multiSelect) {
if (multiSelect.selectedValues.length === multiSelect.allValues.length) {
Expand Down Expand Up @@ -108,10 +116,7 @@ export const CheckboxInput: APIFC<undefined, Config> = ({
disabled={config.disabled}
{...fieldRegistrar}
onChange={handleChange}
ref={(e) => {
inputRef.current = e
fieldRegistrar.ref(e)
}}
ref={handleRef}
/>
<Label htmlFor={"checkbox-" + id}>
<InputBox>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import React, {
FunctionComponent,
useCallback,
useEffect,
useMemo,
} from "react"
import React, { FunctionComponent, useMemo } from "react"
import { defineMessages } from "react-intl"
import { Modal } from "../../interactive/modal"
import { IconType, useViewFormContext } from "generic-view/utils"
Expand All @@ -25,7 +20,6 @@ import { ButtonAction } from "generic-view/models"
import { Paragraph5 } from "../../texts/paragraphs"

export const SELECTED_CONTACTS_FIELD = "selected-contacts"
export const ALL_CONTACTS_FIELD = "all-contacts"

const messages = defineMessages({
title: {
Expand Down Expand Up @@ -53,12 +47,10 @@ export const ImportContactsList: FunctionComponent<Props> = ({
nextAction,
}) => {
const getFormContext = useViewFormContext()
const { watch, setValue } = getFormContext()
const { watch } = getFormContext()
const contacts = useSelector(importContactsSelector)
const searchPhrase = watch("search")
const selectedContacts = watch(SELECTED_CONTACTS_FIELD) || []
const allContactsSelected =
selectedContacts.length === contacts?.length && contacts?.length > 0

const filteredContacts = useMemo(() => {
return contacts.filter(({ firstName, middleName, lastName }) => {
Expand All @@ -71,23 +63,6 @@ export const ImportContactsList: FunctionComponent<Props> = ({
})
}, [contacts, searchPhrase])

const toggleAll = useCallback(() => {
if (allContactsSelected) {
setValue(SELECTED_CONTACTS_FIELD, [])
setValue(ALL_CONTACTS_FIELD, "")
} else {
setValue(
SELECTED_CONTACTS_FIELD,
contacts.map(({ id }) => id)
)
setValue(ALL_CONTACTS_FIELD, "true")
}
}, [allContactsSelected, contacts, setValue])

useEffect(() => {
setValue(ALL_CONTACTS_FIELD, allContactsSelected ? "true" : "")
}, [allContactsSelected, setValue])

return (
<>
<Modal.TitleIcon config={{ type: IconType.ContactsBook }} />
Expand All @@ -101,11 +76,9 @@ export const ImportContactsList: FunctionComponent<Props> = ({
<AllContactsSelector>
<AllCheckbox
config={{
name: ALL_CONTACTS_FIELD,
value: "true",
name: SELECTED_CONTACTS_FIELD,
multipleValues: (contacts || []).map(({ id }) => id),
label: intl.formatMessage(messages.selectAllButton),
onToggle: toggleAll,
checked: allContactsSelected,
}}
/>
<SelectedInfo>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,16 @@ export const ImportContacts: APIFC<undefined, ImportContactsConfig> = ({
...props
}) => {
return (
<Form {...props}>
<Form
{...props}
config={{
formOptions: {
defaultValues: {
[SELECTED_CONTACTS_FIELD]: [],
},
},
}}
>
<ImportContactsForm {...config} />
</Form>
)
Expand Down

0 comments on commit 5462388

Please sign in to comment.