Skip to content

Commit

Permalink
fixing usememo to usecallback
Browse files Browse the repository at this point in the history
  • Loading branch information
stephmilovic committed Oct 6, 2020
1 parent 8ffeec8 commit 4262a0f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,14 @@ export const EditConnector = React.memo(
type: 'SET_CURRENT_CONNECTOR',
payload: getConnectorById(newConnectorId, connectors),
});
if (userActions.length > 0) {
dispatch({
type: 'SET_FIELDS',
payload: getConnectorFieldsFromUserActions(newConnectorId, userActions),
});
}
} else if (fields === null && userActions.length > 0) {
dispatch({
type: 'SET_FIELDS',
payload: getConnectorFieldsFromUserActions(newConnectorId, userActions),
payload: getConnectorFieldsFromUserActions(newConnectorId, userActions ?? []),
});
} else if (fields === null) {
dispatch({
type: 'SET_FIELDS',
payload: getConnectorFieldsFromUserActions(newConnectorId, userActions ?? []),
});
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,26 @@ const JiraSettingFieldsComponent: React.FunctionComponent<SettingFieldsProps<Jir
const { issueType = null, priority = null, parent = null } = fields ?? {};
const { http, notifications } = useKibana().services;

const handleIssueType = useCallback(
(issueTypeSelectOptions: Array<{ value: string; text: string }>) => {
if (issueType == null && issueTypeSelectOptions.length > 0) {
// if there is no issue type set in the edit view, set it to default
if (isEdit) {
onChange({
issueType: issueTypeSelectOptions[0].value,
parent,
priority,
});
}
}
},
[isEdit, issueType, onChange, parent, priority]
);
const { isLoading: isLoadingIssueTypes, issueTypes } = useGetIssueTypes({
connector,
http,
toastNotifications: notifications.toasts,
handleIssueType,
});

const issueTypesSelectOptions = useMemo(
Expand All @@ -43,28 +59,15 @@ const JiraSettingFieldsComponent: React.FunctionComponent<SettingFieldsProps<Jir

const currentIssueType = useMemo(() => {
if (!issueType && issueTypesSelectOptions.length > 0) {
// if there is no issue type set in the edit view, set it to default
if (isEdit) {
onChange({
issueType: issueTypesSelectOptions[0].value,
parent,
priority,
});
}
return issueTypesSelectOptions[0].value;
} else if (
issueTypesSelectOptions.length > 0 &&
!issueTypesSelectOptions.some(({ value }) => value === issueType)
) {
onChange({
issueType: issueTypesSelectOptions[0].value,
parent,
priority,
});
return issueTypesSelectOptions[0].value;
}
return issueType;
}, [isEdit, issueType, issueTypesSelectOptions, onChange, parent, priority]);
}, [issueType, issueTypesSelectOptions]);

const { isLoading: isLoadingFields, fields: fieldsByIssueType } = useGetFieldsByIssueType({
connector,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface Props {
'get$' | 'add' | 'remove' | 'addSuccess' | 'addWarning' | 'addDanger' | 'addError'
>;
connector?: ActionConnector;
handleIssueType: (options: Array<{ value: string; text: string }>) => void;
}

export interface UseGetIssueTypes {
Expand All @@ -29,6 +30,7 @@ export const useGetIssueTypes = ({
http,
connector,
toastNotifications,
handleIssueType,
}: Props): UseGetIssueTypes => {
const [isLoading, setIsLoading] = useState(true);
const [issueTypes, setIssueTypes] = useState<IssueTypes>([]);
Expand All @@ -54,7 +56,12 @@ export const useGetIssueTypes = ({

if (!didCancel) {
setIsLoading(false);
const asOptions = (res.data ?? []).map((type) => ({
text: type.name ?? '',
value: type.id ?? '',
}));
setIssueTypes(res.data ?? []);
handleIssueType(asOptions);
if (res.status && res.status === 'error') {
toastNotifications.addDanger({
title: i18n.ISSUE_TYPES_API_ERROR,
Expand All @@ -81,7 +88,7 @@ export const useGetIssueTypes = ({
setIsLoading(false);
abortCtrl.current.abort();
};
}, [http, connector, toastNotifications]);
}, [http, connector, toastNotifications, handleIssueType]);

return {
issueTypes,
Expand Down

0 comments on commit 4262a0f

Please sign in to comment.