From dbff194f487086f86419568d1bc7e4a7cf5ea0af Mon Sep 17 00:00:00 2001 From: piedram Date: Tue, 22 Aug 2023 15:44:23 -0400 Subject: [PATCH] Refactor Scenario5 --- .../queue/AssignToAttorneyLegacyModalView.jsx | 4 +- .../AssignToAttorneyLegacyWidget.jsx | 386 ------------------ .../components/AssignToAttorneyWidget.jsx | 83 +++- 3 files changed, 66 insertions(+), 407 deletions(-) delete mode 100644 client/app/queue/components/AssignToAttorneyLegacyWidget.jsx diff --git a/client/app/queue/AssignToAttorneyLegacyModalView.jsx b/client/app/queue/AssignToAttorneyLegacyModalView.jsx index 5fa212bed60..d855e6a62b4 100644 --- a/client/app/queue/AssignToAttorneyLegacyModalView.jsx +++ b/client/app/queue/AssignToAttorneyLegacyModalView.jsx @@ -5,7 +5,7 @@ import PropTypes from 'prop-types'; import { taskActionData } from './utils'; import { sprintf } from 'sprintf-js'; -import { AssignToAttorneyLegacyWidgetModal } from './components/AssignToAttorneyLegacyWidget'; +import { AssignToAttorneyWidgetModal } from './components/AssignToAttorneyWidget'; import { taskById } from './selectors'; import COPY from '../../COPY'; @@ -87,7 +87,7 @@ class AssignToAttorneyLegacyModalView extends React.PureComponent { return null; } - return ( instructionData)); - const isInstructionArray = (instructions === null ? null : instructions); - const instructionType = Array.isArray(props.selectedTasks[0].instructions) ? isInstructionArray : null; - - this.state = { - - instructions: ((this.props.isModal ? instructionType : null) || null) - }; - } - - componentDidMount = () => this.props.resetSuccessMessages?.(); - - validAssignee = () => { - const { selectedAssignee } = this.props; - - if (!selectedAssignee || (selectedAssignee === OTHER && !this.props.selectedAssigneeSecondary)) { - if (!this.props.isModal) { - this.props.showErrorMessage( - { title: COPY.ASSIGN_WIDGET_NO_ASSIGNEE_TITLE, - detail: COPY.ASSIGN_WIDGET_NO_ASSIGNEE_DETAIL }); - } - - return false; - } - - return true; - - } - - validTasks = () => { - if (this.props.selectedTasks.length === 0) { - if (!this.props.isModal) { - this.props.showErrorMessage( - { title: COPY.ASSIGN_WIDGET_NO_TASK_TITLE, - detail: COPY.ASSIGN_WIDGET_NO_TASK_DETAIL }); - } - - return false; - } - - return true; - } - - validInstructions = () => { - if (this.props.isModal && this.state.instructions.length === 0) { - return false; - } - - return true; - } - - validateForm = () => this.validAssignee() && this.validTasks() && this.validInstructions(); - - submit = () => { - const { selectedAssignee, selectedAssigneeSecondary, selectedTasks } = this.props; - - this.props.resetSuccessMessages?.(); - this.props.resetErrorMessages?.(); - - if (this.props.isModal) { - // QueueFlowModal will call validateForm - } else if (!this.validateForm()) { - return; - } - - if (selectedAssignee === OTHER) { - return this.assignTasks(selectedTasks, this.getAssignee(selectedAssigneeSecondary)); - } - - return this.assignTasks(selectedTasks, this.getAssignee(selectedAssignee)); - } - - getAssignee = (userId) => { - const { attorneysOfJudge, attorneys, currentUser, selectedTasks } = this.props; - - // Assignee could be the current user - const judgeOpt = { id: currentUser.id, full_name: currentUser.fullName }; - const assigneeOpts = [...attorneysOfJudge, judgeOpt, ...(attorneys?.data || [])]; - - let assignee = assigneeOpts.find((user) => user?.id?.toString() === userId.toString()); - - if (!assignee) { - // Sometimes attorneys are pulled from task action data. If we can't find the selected attorney in state, check - // the tasks. - const option = taskActionData({ - ...this.props, - task: selectedTasks[0], - })?.options.find((opt) => opt.value === userId); - - assignee = { id: option.value, full_name: option.label }; - } - - return assignee; - }; - - assignTasks = (selectedTasks, assignee) => { - const { - previousAssigneeId, - userId - } = this.props; - - const { instructions } = this.state; - - this.props.setSavePending(); - - return this.props.onTaskAssignment( - { tasks: selectedTasks, - assigneeId: assignee.id, - previousAssigneeId, - instructions, - assignee: assignee.full_name }). - then(() => { - const isReassign = selectedTasks[0].type === 'AttorneyTask'; - - this.props.resetAssignees(); - - return this.props.showSuccessMessage({ - title: sprintf(COPY.ASSIGN_WIDGET_SUCCESS, { - verb: isReassign ? 'Reassigned' : 'Assigned', - numCases: selectedTasks.length, - casePlural: pluralize('tasks', selectedTasks.length), - // eslint-disable-next-line camelcase - assignee: assignee.full_name - }) - }); - }, (error) => { - this.props.saveFailure(); - - let errorDetail; - - try { - errorDetail = error.response.body.errors[0].detail; - } catch (ex) { - errorDetail = this.props.isModal && userId ? - - {COPY.ASSIGN_WIDGET_ASSIGNMENT_ERROR_DETAIL_MODAL_LINK} - {COPY.ASSIGN_WIDGET_ASSIGNMENT_ERROR_DETAIL_MODAL} - : COPY.ASSIGN_WIDGET_ASSIGNMENT_ERROR_DETAIL; - } - - return this.props.showErrorMessage({ - title: COPY.ASSIGN_WIDGET_ASSIGNMENT_ERROR_TITLE, - detail: errorDetail }); - }). - finally(() => { - if (!this.props.isModal) { - this.props.resetSaveState(); - } - }); - } - - render = () => { - const { - attorneys, - attorneysOfJudge, - currentUser, - selectedAssignee, - selectedAssigneeSecondary, - selectedTasks, - savePending, - highlightFormItems, - isModal, - onCancel - } = this.props; - const { instructions } = this.state; - const optionFromAttorney = (attorney) => ({ label: attorney.full_name, - value: attorney.id.toString() }); - const otherOpt = { label: COPY.ASSIGN_WIDGET_OTHER, value: OTHER }; - const judgeOpt = currentUser ? { label: currentUser.fullName, value: currentUser.id } : null; - const options = [...attorneysOfJudge.map(optionFromAttorney), ...(judgeOpt ? [judgeOpt] : []), otherOpt]; - const selectedOption = options.find((option) => option.value === selectedAssignee); - let optionsOther = []; - let placeholderOther = COPY.ASSIGN_WIDGET_LOADING; - let selectedOptionOther = null; - - if (attorneys.error) { - placeholderOther = COPY.ASSIGN_WIDGET_ERROR_LOADING_ATTORNEYS; - } - - if (attorneys.data) { - optionsOther = attorneys.data.map(optionFromAttorney); - } else if (isModal) { - optionsOther = taskActionData({ - ...this.props, - task: selectedTasks[0] - })?.options; - } - - if (optionsOther?.length) { - placeholderOther = COPY.ASSIGN_WIDGET_DROPDOWN_PLACEHOLDER; - selectedOptionOther = optionsOther.find((option) => option.value === selectedAssigneeSecondary); - } - - const Widget = - option && this.props.setSelectedAssignee({ assigneeId: option.value })} - value={selectedOption} /> - {selectedAssignee === OTHER && - -
- - option && this.props.setSelectedAssigneeSecondary({ assigneeId: option.value })} - value={selectedOptionOther} /> - } - {isModal && -
- this.setState({ instructions: value })} - value = {this.state.instructions ? null : this.state.instructions} /> -
} - {!isModal &&