Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { CSSProperties, HTMLAttributes, SyntheticEvent, useState } from 'react';
import { Field } from 'formik';
import { graphql } from 'react-relay';
import Alert from '@mui/material/Alert';
Expand All @@ -8,10 +8,14 @@ import { fetchQuery } from '../../../../relay/environment';
import AutocompleteField from '../../../../components/AutocompleteField';
import { useFormatter } from '../../../../components/i18n';
import ItemIcon from '../../../../components/ItemIcon';
import { Theme } from '../../../../components/Theme';
import { FieldOption } from '../../../../utils/field';
import { FilterGroup } from '../../../../utils/filters/filtersHelpers-types';
import { ObjectOrganizationFieldQuery$data } from '@components/common/form/__generated__/ObjectOrganizationFieldQuery.graphql';

// Deprecated - https://mui.com/system/styles/basics/
// Do not use it for new code.
const useStyles = makeStyles(() => ({
const useStyles = makeStyles<Theme>(() => ({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makeStyles is deprecated, you should rather integrate the styles directly in the html :)

icon: {
paddingTop: 4,
display: 'inline-block',
Expand Down Expand Up @@ -43,21 +47,53 @@ export const searchObjectOrganizationFieldQuery = graphql`
}
`;

const ObjectOrganizationField = (props) => {
const {
name,
label,
style,
onChange,
helpertext,
disabled,
defaultOrganizations,
outlined = true,
multiple = true,
alert = true,
filters = null,
} = props;
interface ObjectOrganizationFieldBaseProps {
name: string;
label: string;
style: CSSProperties;
helpertext?: string;
disabled?: boolean;
defaultOrganizations?: { name: string; id: string }[];
outlined?: boolean;
alert?: boolean;
filters?: FilterGroup | null;
}

interface ObjectOrganizationFieldMultipleProps
extends ObjectOrganizationFieldBaseProps {
multiple?: true;
onChange?: (
name: string,
value: FieldOption[],
resetForm?: () => void,
) => void;
}

interface ObjectOrganizationFieldSingleProps
extends ObjectOrganizationFieldBaseProps {
multiple: false;
onChange?: (
name: string,
value: FieldOption,
resetForm?: () => void,
) => void;
}

type ObjectOrganizationFieldProps = ObjectOrganizationFieldMultipleProps | ObjectOrganizationFieldSingleProps;

const ObjectOrganizationField = ({
name,
label,
style,
onChange,
helpertext,
disabled,
defaultOrganizations,
outlined = true,
multiple = true,
alert = true,
filters = null,
}: ObjectOrganizationFieldProps) => {
const defaultStateOrganizations = (defaultOrganizations ?? []).map((n) => ({
label: n.name,
value: n.id,
Expand All @@ -66,19 +102,21 @@ const ObjectOrganizationField = (props) => {
const classes = useStyles();
const { t_i18n } = useFormatter();

const searchOrganizations = (event) => {
fetchQuery(searchObjectOrganizationFieldQuery, {
search: (event && event.target && event.target.value) ?? '',
filters,
})
.toPromise()
.then((data) => {
const searchResults = data.organizations.edges.map((n) => ({
label: n.node.name,
value: n.node.id,
}));
setOrganizations(searchResults);
});
const searchOrganizations = (event?: SyntheticEvent<Element, Event>) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const searchOrganizations = (event?: SyntheticEvent<Element, Event>) => {
const searchOrganizations = (event?: BaseSyntheticEvent) => {

if (event?.target instanceof HTMLInputElement) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are changing the behavior with this if, you can remove it if you event is typed correctly (BaseSyntheticEvent)

fetchQuery(searchObjectOrganizationFieldQuery, {
search: event.target.value ?? '',
filters,
})
.toPromise()
.then((data) => {
const searchResults = (data as ObjectOrganizationFieldQuery$data).organizations?.edges.map((n) => ({
label: n.node.name,
value: n.node.id,
}));
setOrganizations(searchResults ?? []);
});
}
};

if (outlined === false) {
Expand All @@ -99,7 +137,10 @@ const ObjectOrganizationField = (props) => {
options={organizations}
onInputChange={searchOrganizations}
onChange={typeof onChange === 'function' ? onChange : null}
renderOption={(renderProps, option) => (
renderOption={(
renderProps: HTMLAttributes<HTMLLIElement>,
option: { label: string },
) => (
<li {...renderProps}>
<div className={classes.icon}>
<ItemIcon type="Organization" />
Expand Down Expand Up @@ -127,7 +168,10 @@ const ObjectOrganizationField = (props) => {
options={organizations}
onInputChange={searchOrganizations}
onChange={typeof onChange === 'function' ? onChange : null}
renderOption={(renderProps, option) => (
renderOption={(
renderProps: HTMLAttributes<HTMLLIElement>,
option: { label: string },
) => (
<li {...renderProps}>
<div className={classes.icon}>
<ItemIcon type="Organization" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import MarkdownField from '../../../../../components/fields/MarkdownField';
import ObjectOrganizationField from '../../../common/form/ObjectOrganizationField';
import { useFormatter } from '../../../../../components/i18n';
import DateTimePickerField from '../../../../../components/DateTimePickerField';
import { fieldSpacingContainerStyle } from '../../../../../utils/field';
import { FieldOption, fieldSpacingContainerStyle } from '../../../../../utils/field';
import useAuth from '../../../../../utils/hooks/useAuth';
import { isOnlyOrganizationAdmin } from '../../../../../utils/hooks/useGranted';
import useApiMutation from '../../../../../utils/hooks/useApiMutation';
Expand Down Expand Up @@ -149,14 +149,14 @@ const UserEditionOverviewComponent: FunctionComponent<

const handleChangeObjectOrganization = (
name: string,
values: { label: string; value: string }[],
value: FieldOption[],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can keep the denomination 'values' since it's an array

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
value: FieldOption[],
values: FieldOption[],

) => {
const currentValues = (user?.objectAssignedOrganization?.edges ?? []).map((n) => ({
label: n.node.name,
value: n.node.id,
}));
const added = R.difference(values, currentValues);
const removed = R.difference(currentValues, values);
const added = R.difference(value, currentValues);
const removed = R.difference(currentValues, value);
if (added.length > 0) {
commitOrganizationAdd({
variables: {
Expand Down
Loading