Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CP-3192] Improved form implementation for contacts import list #2226

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading