Skip to content

Commit

Permalink
Move finding current contact in state to a selector (with extra defen…
Browse files Browse the repository at this point in the history
…sive null check)
  • Loading branch information
stephenhand committed Dec 11, 2023
1 parent f901544 commit 52c18ef
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
5 changes: 2 additions & 3 deletions plugin-hrm-form/src/components/CustomCRMContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { createContactAsyncAction } from '../states/contacts/saveContact';
import { getAseloFeatureFlags, getHrmConfig } from '../hrmConfig';
import { newContact } from '../states/contacts/contactState';
import { selectAnyContactIsSaving } from '../states/contacts/selectContactSaveStatus';
import selectCurrentOfflineContact from '../states/contacts/selectCurrentOfflineContact';

type OwnProps = {
task?: ITask;
Expand Down Expand Up @@ -127,9 +128,7 @@ const mapStateToProps = (state: RootState) => {
} = state;
const { selectedTaskSid } = flex.view;
const { isAddingOfflineContact } = routing;
const currentOfflineContact = Object.values(activeContacts.existingContacts).find(
contact => contact.savedContact.taskId === getOfflineContactTaskSid(),
);
const currentOfflineContact = selectCurrentOfflineContact(state);
const hasUnsavedChanges =
Object.values(activeContacts.existingContacts).some(
({ savedContact, draftContact }) => !_.isEqual(savedContact, getUnsavedContact(savedContact, draftContact)),
Expand Down
27 changes: 27 additions & 0 deletions plugin-hrm-form/src/states/contacts/selectCurrentOfflineContact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (C) 2021-2023 Technology Matters
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

import { RootState } from '..';
import { namespace } from '../storeNamespaces';
import getOfflineContactTaskSid from './offlineContactTaskSid';
import { ContactState } from './existingContacts';

const selectCurrentOfflineContact = ({ [namespace]: { activeContacts } }: RootState): ContactState | undefined =>
Object.values(activeContacts.existingContacts).find(
contact => contact.savedContact?.taskId === getOfflineContactTaskSid(),
);

export default selectCurrentOfflineContact;

0 comments on commit 52c18ef

Please sign in to comment.