diff --git a/frontend/src/app/core/test-utils/MockWorkerService.ts b/frontend/src/app/core/test-utils/MockWorkerService.ts index e4bd442568..0a009c87e2 100644 --- a/frontend/src/app/core/test-utils/MockWorkerService.ts +++ b/frontend/src/app/core/test-utils/MockWorkerService.ts @@ -5,7 +5,7 @@ import { QualificationsByGroup } from '@core/model/qualification.model'; import { MultipleTrainingResponse, TrainingRecordRequest } from '@core/model/training.model'; import { URLStructure } from '@core/model/url.model'; import { Worker, WorkerEditResponse, WorkersResponse } from '@core/model/worker.model'; -import { WorkerService } from '@core/services/worker.service'; +import { NewWorkerMandatoryInfo, WorkerService } from '@core/services/worker.service'; import { build, fake, oneOf, perBuild, sequence } from '@jackfranklin/test-data-bot'; import { Observable, of } from 'rxjs'; @@ -339,7 +339,7 @@ export class MockWorkerService extends WorkerService { @Injectable() export class MockWorkerServiceWithUpdateWorker extends MockWorkerService { - public static factory(worker: Worker) { + public static factory(worker: Worker = null) { return (httpClient: HttpClient) => { const service = new MockWorkerServiceWithUpdateWorker(httpClient); if (worker) { @@ -359,6 +359,24 @@ export class MockWorkerServiceWithUpdateWorker extends MockWorkerService { } } +@Injectable() +export class MockWorkerServiceWithNoWorker extends MockWorkerService { + public static factory(workerMandatoryInfo: NewWorkerMandatoryInfo = null) { + return (httpClient: HttpClient) => { + const service = new MockWorkerServiceWithNoWorker(httpClient); + if (workerMandatoryInfo) { + service.setNewWorkerMandatoryInfo(workerMandatoryInfo.nameOrId, workerMandatoryInfo.contract); + } + return service; + }; + } + public get worker() { + return null; + } + + public worker$ = of(null); +} + @Injectable() export class MockWorkerServiceWithoutReturnUrl extends MockWorkerServiceWithUpdateWorker { public static factory(worker: Worker) { diff --git a/frontend/src/app/features/workers/main-job-role/main-job-role.component.spec.ts b/frontend/src/app/features/workers/main-job-role/main-job-role.component.spec.ts index bcd0bb5c34..084af03457 100644 --- a/frontend/src/app/features/workers/main-job-role/main-job-role.component.spec.ts +++ b/frontend/src/app/features/workers/main-job-role/main-job-role.component.spec.ts @@ -1,26 +1,27 @@ -import { render } from '@testing-library/angular'; -import { MainJobRoleComponent } from './main-job-role.component'; -import { RouterTestingModule } from '@angular/router/testing'; +import { HttpClient } from '@angular/common/http'; import { HttpClientTestingModule } from '@angular/common/http/testing'; -import { ReactiveFormsModule, UntypedFormBuilder } from '@angular/forms'; -import { ProgressBarComponent } from '@shared/components/progress-bar/progress-bar.component'; -import { WorkerService } from '@core/services/worker.service'; -import { MockWorkerServiceWithUpdateWorker, workerBuilder } from '@core/test-utils/MockWorkerService'; -import { WindowRef } from '@core/services/window.ref'; -import { ActivatedRoute, Router, RouterModule } from '@angular/router'; -import { SharedModule } from '@shared/shared.module'; import { NO_ERRORS_SCHEMA } from '@angular/core'; import { getTestBed } from '@angular/core/testing'; +import { ReactiveFormsModule, UntypedFormBuilder } from '@angular/forms'; +import { ActivatedRoute, Router, RouterModule } from '@angular/router'; +import { RouterTestingModule } from '@angular/router/testing'; +import { Contracts } from '@core/model/contracts.enum'; +import { Roles } from '@core/model/roles.enum'; +import { Worker } from '@core/model/worker.model'; +import { AlertService } from '@core/services/alert.service'; import { PermissionsService } from '@core/services/permissions/permissions.service'; -import { MockPermissionsService } from '@core/test-utils/MockPermissionsService'; -import { HttpClient } from '@angular/common/http'; import { UserService } from '@core/services/user.service'; +import { WindowRef } from '@core/services/window.ref'; +import { WorkerService } from '@core/services/worker.service'; +import { MockPermissionsService } from '@core/test-utils/MockPermissionsService'; import { MockUserService } from '@core/test-utils/MockUserService'; -import { Roles } from '@core/model/roles.enum'; -import { Contracts } from '@core/model/contracts.enum'; -import { Worker } from '@core/model/worker.model'; +import { MockWorkerServiceWithUpdateWorker, workerBuilder } from '@core/test-utils/MockWorkerService'; +import { ProgressBarComponent } from '@shared/components/progress-bar/progress-bar.component'; +import { SharedModule } from '@shared/shared.module'; +import { render } from '@testing-library/angular'; import userEvent from '@testing-library/user-event'; -import { AlertService } from '@core/services/alert.service'; + +import { MainJobRoleComponent } from './main-job-role.component'; describe('MainJobRoleComponent', () => { async function setup(insideFlow = true, returnToMandatoryDetails = false, addNewWorker = false) { @@ -433,5 +434,28 @@ describe('MainJobRoleComponent', () => { 'mental-health-professional', ]); }); + + it('should clear the mandatory worker info in the worker service on submit', async () => { + const { fixture, getByText, workerService } = await setup(); + + const clearWorkerInfoSpy = spyOn(workerService, 'clearNewWorkerMandatoryInfo'); + + userEvent.click(getByText('Professional and related roles')); + userEvent.click(getByText('Social worker')); + userEvent.click(getByText('Save and return')); + fixture.detectChanges(); + + expect(clearWorkerInfoSpy).toHaveBeenCalled(); + }); + + it('should clear the mandatory worker info in the worker service on click of Cancel', async () => { + const { getByText, workerService } = await setup(); + + const clearWorkerInfoSpy = spyOn(workerService, 'clearNewWorkerMandatoryInfo'); + + userEvent.click(getByText('Cancel')); + + expect(clearWorkerInfoSpy).toHaveBeenCalled(); + }); }); }); diff --git a/frontend/src/app/features/workers/main-job-role/main-job-role.component.ts b/frontend/src/app/features/workers/main-job-role/main-job-role.component.ts index 7c443f756f..5d414ee57f 100644 --- a/frontend/src/app/features/workers/main-job-role/main-job-role.component.ts +++ b/frontend/src/app/features/workers/main-job-role/main-job-role.component.ts @@ -1,13 +1,14 @@ import { Component, OnDestroy, OnInit } from '@angular/core'; -import { QuestionComponent } from '../question/question.component'; -import { Job } from '@core/model/job.model'; import { UntypedFormBuilder, Validators } from '@angular/forms'; import { ActivatedRoute, Router } from '@angular/router'; +import { Job } from '@core/model/job.model'; +import { AlertService } from '@core/services/alert.service'; import { BackLinkService } from '@core/services/backLink.service'; import { ErrorSummaryService } from '@core/services/error-summary.service'; -import { NewWorkerMandatoryInfo, WorkerService } from '@core/services/worker.service'; import { EstablishmentService } from '@core/services/establishment.service'; -import { AlertService } from '@core/services/alert.service'; +import { NewWorkerMandatoryInfo, WorkerService } from '@core/services/worker.service'; + +import { QuestionComponent } from '../question/question.component'; @Component({ selector: 'app-main-job-role.component', @@ -156,7 +157,6 @@ export class MainJobRoleComponent extends QuestionComponent implements OnInit, O if (this.editFlow) { this.next = this.determineConditionalRouting(); } else { - this.workerService.clearNewWorkerMandatoryInfo(); this.next = this.getRoutePath('mandatory-details'); } !this.editFlow && this.workerService.setAddStaffRecordInProgress(true); @@ -183,4 +183,9 @@ export class MainJobRoleComponent extends QuestionComponent implements OnInit, O }, ]; } + + public onSubmit(): void { + this.workerService.clearNewWorkerMandatoryInfo(); + super.onSubmit(); + } } diff --git a/frontend/src/app/features/workers/staff-details/staff-details.component.html b/frontend/src/app/features/workers/staff-details/staff-details.component.html index 6da4726305..a66305ae60 100644 --- a/frontend/src/app/features/workers/staff-details/staff-details.component.html +++ b/frontend/src/app/features/workers/staff-details/staff-details.component.html @@ -26,56 +26,6 @@

{{ worker ? 'Update' : 'Add a' }} staff reco type="text" /> -
- - - Error: - {{ getFormErrorMessage('mainJob', 'required') }} - - - -
- - - Error: - {{ getFormErrorMessage('otherJobRole', 'maxlength') }} - - -
-
@@ -120,8 +70,8 @@

{ - async function setup(insideFlow = true, returnToMandatoryDetails = false) { + async function setup( + insideFlow = true, + returnToMandatoryDetails = false, + creatingNewWorker = false, + workerInfo = null, + ) { let path; if (returnToMandatoryDetails) { path = 'mandatory-details'; @@ -56,13 +59,11 @@ describe('StaffDetailsComponent', () => { useFactory: MockUserService.factory(0, Roles.Admin), deps: [HttpClient], }, - { - provide: JobService, - useClass: MockJobService, - }, { provide: WorkerService, - useClass: MockWorkerServiceWithUpdateWorker, + useFactory: creatingNewWorker + ? MockWorkerServiceWithNoWorker.factory(workerInfo) + : MockWorkerServiceWithUpdateWorker.factory(), }, { provide: ActivatedRoute, @@ -78,6 +79,7 @@ describe('StaffDetailsComponent', () => { }, snapshot: { params: {}, + data: {}, }, }, }, @@ -97,6 +99,8 @@ describe('StaffDetailsComponent', () => { const submitSpy = spyOn(component, 'setSubmitAction').and.callThrough(); const alertSpy = spyOn(alertService, 'addAlert').and.callThrough(); const setAddStaffRecordInProgressSpy = spyOn(workerService, 'setAddStaffRecordInProgress'); + const setNewWorkerMandatoryInfoSpy = spyOn(workerService, 'setNewWorkerMandatoryInfo').and.callThrough(); + const clearNewWorkerMandatoryInfoSpy = spyOn(workerService, 'clearNewWorkerMandatoryInfo').and.callThrough(); return { component, @@ -114,6 +118,8 @@ describe('StaffDetailsComponent', () => { submitSpy, alertSpy, setAddStaffRecordInProgressSpy, + setNewWorkerMandatoryInfoSpy, + clearNewWorkerMandatoryInfoSpy, }; } @@ -141,29 +147,6 @@ describe('StaffDetailsComponent', () => { expect(getByText('Update staff record')).toBeTruthy(); }); - it('should render the page without the conditional input', async () => { - const { component, fixture } = await setup(); - - const form = component.form; - form.controls.nameOrId.setValue(''); - form.controls.mainJob.setValue(''); - form.controls.contract.setValue(''); - fixture.detectChanges(); - - expect(fixture.nativeElement.querySelector('.govuk-select__conditional--hidden')).toBeTruthy(); - }); - - it('should show the conditional input when selecting a job which has an other value of true', async () => { - const { component, getByLabelText, fixture } = await setup(); - - const otherJobs = component.jobsAvailable.filter((job) => job.other); - - userEvent.selectOptions(getByLabelText('Main job role'), [otherJobs[0].id.toString()]); - fixture.detectChanges(); - - expect(fixture.nativeElement.querySelector('.govuk-select__conditional--hidden')).toBeFalsy(); - }); - describe('progress bar', () => { it('should render the progress bar when accessed from the flow', async () => { const { getByTestId } = await setup(); @@ -179,28 +162,60 @@ describe('StaffDetailsComponent', () => { }); describe('submit buttons', () => { - it(`should show 'Save staff record' cta button and 'Cancel' link when adding a staff record`, async () => { + it(`should show 'Continue' cta button and 'Cancel' link when adding a staff record`, async () => { const { getByText } = await setup(); - expect(getByText('Save this staff record')).toBeTruthy(); + expect(getByText('Continue')).toBeTruthy(); expect(getByText('Cancel')).toBeTruthy(); }); - it(`should show 'Save' and 'Cancel' buttons when not in mandatory details flow or in the staff record flow`, async () => { + it(`should navigate to main-job-role page and set mandatory info in worker service after clicking 'Continue' cta button when adding a staff record`, async () => { + const { + fixture, + getByText, + getByLabelText, + routerSpy, + updateWorkerSpy, + setNewWorkerMandatoryInfoSpy, + submitSpy, + } = await setup(true, false, true); + + const enteredName = 'Someone'; + userEvent.type(getByLabelText('Name or ID number'), enteredName); + userEvent.click(getByLabelText('Temporary')); + userEvent.click(getByText('Continue')); + fixture.detectChanges(); + + expect(updateWorkerSpy).not.toHaveBeenCalled(); + expect(submitSpy).toHaveBeenCalledWith({ action: 'continue', save: true }); + expect(setNewWorkerMandatoryInfoSpy).toHaveBeenCalledWith(enteredName, 'Temporary' as Contracts); + expect(routerSpy.calls.mostRecent().args[0]).toEqual(['main-job-role']); + }); + + it(`should clear mandatory info in worker service after clicking 'Cancel' button when adding a staff record`, async () => { + const { fixture, getByText, clearNewWorkerMandatoryInfoSpy } = await setup(true, false, true); + + userEvent.click(getByText('Cancel')); + fixture.detectChanges(); + + expect(clearNewWorkerMandatoryInfoSpy).toHaveBeenCalled(); + }); + + it(`should show 'Save and return' and 'Cancel' buttons when not in mandatory details flow or in the staff record flow`, async () => { const { getByText } = await setup(false, false); - expect(getByText('Save')).toBeTruthy(); + expect(getByText('Save and return')).toBeTruthy(); expect(getByText('Cancel')).toBeTruthy(); }); it(`should show 'Save and return' cta button and 'Cancel' link when editing a staff record outside`, async () => { const { getByText } = await setup(false); - expect(getByText('Save')).toBeTruthy(); + expect(getByText('Save and return')).toBeTruthy(); expect(getByText('Cancel')).toBeTruthy(); }); - it(`should call submit data and navigate to the wdf staff record summary page when 'Save' is clicked in WDF version of the page`, async () => { + it(`should call submit data and navigate to the wdf staff record summary page when 'Save and return' is clicked in WDF version of the page`, async () => { const { component, router, fixture, getByText, getByLabelText, submitSpy, routerSpy, updateWorkerSpy } = await setup(false, false); spyOnProperty(router, 'url').and.returnValue('/wdf/staff-record'); @@ -208,49 +223,41 @@ describe('StaffDetailsComponent', () => { component.ngOnInit(); fixture.detectChanges(); - userEvent.selectOptions(getByLabelText('Main job role'), ['2']); userEvent.click(getByLabelText('Temporary')); - userEvent.click(getByText('Save')); + userEvent.click(getByText('Save and return')); fixture.detectChanges(); const updatedFormData = component.form.value; expect(updatedFormData).toEqual({ nameOrId: component.worker.nameOrId, - mainJob: '2', - otherJobRole: null, contract: 'Temporary', }); - expect(submitSpy).toHaveBeenCalledWith({ action: 'continue', save: true }); + expect(submitSpy).toHaveBeenCalledWith({ action: 'return', save: true }); expect(updateWorkerSpy).toHaveBeenCalledWith(component.workplace.uid, component.worker.uid, { nameOrId: component.worker.nameOrId, contract: 'Temporary', - mainJob: { jobId: 2 }, }); expect(routerSpy).toHaveBeenCalledWith(['/wdf', 'staff-record', fixture.componentInstance.worker.uid]); }); - it(`should call submit data and navigate to the the staff record summary page when 'Save' is clicked outside of mandatory details flow`, async () => { + it(`should call submit data and navigate to the the staff record summary page when 'Save and return' is clicked outside of mandatory details flow`, async () => { const { component, fixture, getByText, getByLabelText, submitSpy, routerSpy, updateWorkerSpy } = await setup( false, ); - userEvent.selectOptions(getByLabelText('Main job role'), ['2']); userEvent.click(getByLabelText('Temporary')); - userEvent.click(getByText('Save')); + userEvent.click(getByText('Save and return')); fixture.detectChanges(); const updatedFormData = component.form.value; expect(updatedFormData).toEqual({ nameOrId: component.worker.nameOrId, - mainJob: '2', - otherJobRole: null, contract: 'Temporary', }); - expect(submitSpy).toHaveBeenCalledWith({ action: 'continue', save: true }); + expect(submitSpy).toHaveBeenCalledWith({ action: 'return', save: true }); expect(updateWorkerSpy).toHaveBeenCalledWith(component.workplace.uid, component.worker.uid, { nameOrId: component.worker.nameOrId, contract: 'Temporary', - mainJob: { jobId: 2 }, }); expect(routerSpy).toHaveBeenCalledWith([ '/workplace', @@ -267,7 +274,6 @@ describe('StaffDetailsComponent', () => { true, ); - userEvent.selectOptions(getByLabelText('Main job role'), ['2']); userEvent.click(getByLabelText('Temporary')); userEvent.click(getByText('Save and return')); fixture.detectChanges(); @@ -275,15 +281,12 @@ describe('StaffDetailsComponent', () => { const updatedFormData = component.form.value; expect(updatedFormData).toEqual({ nameOrId: component.worker.nameOrId, - mainJob: '2', - otherJobRole: null, contract: 'Temporary', }); expect(submitSpy).toHaveBeenCalledWith({ action: 'return', save: true }); expect(updateWorkerSpy).toHaveBeenCalledWith(component.workplace.uid, component.worker.uid, { nameOrId: component.worker.nameOrId, contract: 'Temporary', - mainJob: { jobId: 2 }, }); expect(routerSpy).toHaveBeenCalledWith([ '/workplace', @@ -294,41 +297,6 @@ describe('StaffDetailsComponent', () => { ]); }); - it(`should call submit data and navigate to the the correct url when 'Save and return' is clicked with the dropdown input`, async () => { - const { component, fixture, getByText, getByLabelText, submitSpy, routerSpy, updateWorkerSpy } = await setup( - false, - ); - - const otherJobs = component.jobsAvailable.filter((job) => job.other); - userEvent.selectOptions(getByLabelText('Main job role'), [otherJobs[0].id.toString()]); - fixture.detectChanges(); - userEvent.type(getByLabelText('What is the job role?'), 'Admin'); - userEvent.click(getByLabelText('Temporary')); - userEvent.click(getByText('Save')); - fixture.detectChanges(); - - const updatedFormData = component.form.value; - expect(updatedFormData).toEqual({ - nameOrId: component.worker.nameOrId, - mainJob: '21', - otherJobRole: 'Admin', - contract: 'Temporary', - }); - expect(submitSpy).toHaveBeenCalledWith({ action: 'continue', save: true }); - expect(updateWorkerSpy).toHaveBeenCalledWith(component.workplace.uid, component.worker.uid, { - nameOrId: component.worker.nameOrId, - contract: 'Temporary', - mainJob: { jobId: 21, other: 'Admin' }, - }); - expect(routerSpy).toHaveBeenCalledWith([ - '/workplace', - 'mocked-uid', - 'staff-record', - fixture.componentInstance.worker.uid, - 'staff-record-summary', - ]); - }); - it('should not show a banner when updating a staff record', async () => { const { component, fixture, getByText, getByLabelText, workerService, alertSpy } = await setup(false); @@ -336,9 +304,8 @@ describe('StaffDetailsComponent', () => { component.worker.contract = 'Permanent' as Contracts; }); - userEvent.selectOptions(getByLabelText('Main job role'), ['2']); userEvent.click(getByLabelText('Permanent')); - userEvent.click(getByText('Save')); + userEvent.click(getByText('Save and return')); fixture.detectChanges(); expect(alertSpy).not.toHaveBeenCalled(); @@ -358,15 +325,14 @@ describe('StaffDetailsComponent', () => { component.worker.contract = 'Permanent' as Contracts; }); - userEvent.selectOptions(getByLabelText('Main job role'), ['2']); userEvent.click(getByLabelText('Permanent')); - userEvent.click(getByText('Save')); + userEvent.click(getByText('Save and return')); fixture.detectChanges(); expect(alertSpy).not.toHaveBeenCalled(); }); - it('should not call setAddStaffRecordInProgress when clicking save', async () => { + it('should not call setAddStaffRecordInProgress when clicking save and return', async () => { const { component, fixture, getByText, getByLabelText, workerService, setAddStaffRecordInProgressSpy } = await setup(false); @@ -374,9 +340,8 @@ describe('StaffDetailsComponent', () => { component.worker.contract = 'Permanent' as Contracts; }); - userEvent.selectOptions(getByLabelText('Main job role'), ['2']); userEvent.click(getByLabelText('Permanent')); - userEvent.click(getByText('Save')); + userEvent.click(getByText('Save and return')); fixture.detectChanges(); expect(setAddStaffRecordInProgressSpy).not.toHaveBeenCalled(); }); @@ -434,73 +399,52 @@ describe('StaffDetailsComponent', () => { component.worker = null; const form = component.form; form.controls.nameOrId.setValue(''); - form.controls.mainJob.setValue(''); form.controls.contract.setValue(''); - userEvent.selectOptions(getByLabelText('Main job role'), ['2']); userEvent.click(getByLabelText('Temporary')); - userEvent.click(getByText('Save this staff record')); + userEvent.click(getByText('Continue')); fixture.detectChanges(); expect(getAllByText('Enter their name or ID number').length).toEqual(2); }); - it('should return an error message if the main job value is not filled in', async () => { + it('should return an error message if the contract is not filled in', async () => { const { component, fixture, getByLabelText, getByText, getAllByText } = await setup(); component.worker = null; const form = component.form; form.controls.nameOrId.setValue(''); - form.controls.mainJob.setValue(''); form.controls.contract.setValue(''); userEvent.type(getByLabelText('Name or ID number'), 'Someone'); - userEvent.click(getByLabelText('Temporary')); - userEvent.click(getByText('Save this staff record')); + userEvent.click(getByText('Continue')); fixture.detectChanges(); - expect(getAllByText('Select their main job role').length).toEqual(2); + expect(getAllByText('Select the type of contract they have').length).toEqual(2); }); + }); - it('should return an error message when conditional other job role is showing and the input has more than 120 characters ', async () => { - const { component, fixture, getByLabelText, getByText, getAllByText } = await setup(); + describe('Prefilling the form', () => { + it(`should prefill the form with mandatory info in worker service if it exists when adding a staff record`, async () => { + const mandatoryInfoInService = { nameOrId: 'Someone', contract: 'Temporary' as Contracts }; - component.worker = null; - const form = component.form; - form.controls.nameOrId.setValue(''); - form.controls.mainJob.setValue(''); - form.controls.contract.setValue(''); + const { getByLabelText } = await setup(true, false, true, mandatoryInfoInService); - const otherJobs = component.jobsAvailable.filter((job) => job.other); - userEvent.type(getByLabelText('Name or ID number'), 'Someone'); - userEvent.selectOptions(getByLabelText('Main job role'), [otherJobs[0].id.toString()]); - fixture.detectChanges(); - userEvent.type( - getByLabelText('What is the job role?'), - 'ReallyLongStringReallyLongStringReallyLongStringReallyLongStringReallyLongStringReallyLongStringReallyLongStringReallyLongString', - ); - userEvent.click(getByLabelText('Temporary')); - userEvent.click(getByText('Save this staff record')); - fixture.detectChanges(); + const nameOrId = getByLabelText('Name or ID number') as HTMLFormElement; + const temporaryContractType = getByLabelText('Temporary') as HTMLFormElement; - expect(getAllByText('Job role must be 120 characters or fewer').length).toEqual(2); + expect(nameOrId.value).toEqual(mandatoryInfoInService.nameOrId); + expect(temporaryContractType.checked).toBeTrue(); }); - it('should return an error message if the contract is not filled in', async () => { - const { component, fixture, getByLabelText, getByText, getAllByText } = await setup(); - - component.worker = null; - const form = component.form; - form.controls.nameOrId.setValue(''); - form.controls.mainJob.setValue(''); - form.controls.contract.setValue(''); + it(`should prefill the form with worker data if existing staff record`, async () => { + const { component, getByLabelText } = await setup(); - userEvent.type(getByLabelText('Name or ID number'), 'Someone'); - userEvent.selectOptions(getByLabelText('Main job role'), ['2']); - userEvent.click(getByText('Save this staff record')); - fixture.detectChanges(); + const nameOrId = getByLabelText('Name or ID number') as HTMLFormElement; + const workerContractType = getByLabelText(component.worker.contract) as HTMLFormElement; - expect(getAllByText('Select the type of contract they have').length).toEqual(2); + expect(nameOrId.value).toEqual(component.worker.nameOrId); + expect(workerContractType.checked).toBeTrue(); }); }); }); diff --git a/frontend/src/app/features/workers/staff-details/staff-details.component.ts b/frontend/src/app/features/workers/staff-details/staff-details.component.ts index c9aa66767e..7dd37c3d13 100644 --- a/frontend/src/app/features/workers/staff-details/staff-details.component.ts +++ b/frontend/src/app/features/workers/staff-details/staff-details.component.ts @@ -7,7 +7,6 @@ import { AlertService } from '@core/services/alert.service'; import { BackLinkService } from '@core/services/backLink.service'; import { ErrorSummaryService } from '@core/services/error-summary.service'; import { EstablishmentService } from '@core/services/establishment.service'; -import { JobService } from '@core/services/job.service'; import { WorkerService } from '@core/services/worker.service'; import { QuestionComponent } from '../question/question.component'; @@ -25,13 +24,10 @@ export class StaffDetailsComponent extends QuestionComponent implements OnInit, { value: Contracts.Other, tag: 'Other' }, ]; public jobsAvailable: Job[] = []; - public submitTitle = 'Save this staff record'; public showInputTextforOtherRole: boolean; public canExit = true; public editFlow: boolean; - private otherJobRoleCharacterLimit = 120; public inMandatoryDetailsFlow: boolean; - public summaryContinue: boolean; private isAddingNewWorker: boolean; constructor( @@ -43,14 +39,11 @@ export class StaffDetailsComponent extends QuestionComponent implements OnInit, public workerService: WorkerService, protected establishmentService: EstablishmentService, protected alertService: AlertService, - private jobService: JobService, ) { super(formBuilder, router, route, backLinkService, errorSummaryService, workerService, establishmentService); this.form = this.formBuilder.group( { nameOrId: [null, Validators.required], - mainJob: [null, Validators.required], - otherJobRole: [null, [Validators.maxLength(this.otherJobRoleCharacterLimit)]], contract: [null, Validators.required], }, { updateOn: 'submit' }, @@ -60,8 +53,8 @@ export class StaffDetailsComponent extends QuestionComponent implements OnInit, init(): void { this.inMandatoryDetailsFlow = this.route.parent.snapshot.url[0].path === 'mandatory-details'; this.isAddingNewWorker = !this.worker; - this.summaryContinue = !this.insideFlow && !this.inMandatoryDetailsFlow; - this.getJobs(); + + this.prefillFormIfExistingWorkerOrInfoSetInWorkerService(); this.getReturnPath(); this.editFlow = this.inMandatoryDetailsFlow || this.wdfEditPageFlag || !this.insideFlow; } @@ -77,24 +70,6 @@ export class StaffDetailsComponent extends QuestionComponent implements OnInit, }, ], }, - { - item: 'mainJob', - type: [ - { - name: 'required', - message: `Select their main job role`, - }, - ], - }, - { - item: 'otherJobRole', - type: [ - { - name: 'maxlength', - message: `Job role must be ${this.otherJobRoleCharacterLimit} characters or fewer `, - }, - ], - }, { item: 'contract', type: [ @@ -107,57 +82,25 @@ export class StaffDetailsComponent extends QuestionComponent implements OnInit, ]; } - private getJobs(): void { - this.subscriptions.add( - this.jobService.getJobs().subscribe((jobs) => { - this.jobsAvailable = jobs; - if (this.worker) { - this.renderInEditMode(); - } - }), - ); - } - generateUpdateProps() { - const { nameOrId, contract, mainJob, otherJobRole } = this.form.controls; + const { nameOrId, contract } = this.form.controls; - const props = { + return { nameOrId: nameOrId.value, contract: contract.value, - mainJob: { - jobId: parseInt(mainJob.value, 10), - ...(otherJobRole.value && { other: otherJobRole.value }), - }, }; - - if (this.worker && parseInt(mainJob.value, 10) !== 23) { - this.worker.registeredNurse = null; - if (this.worker.nurseSpecialism) { - this.worker.nurseSpecialism.specialism = null; - } - } - return props; } - selectedJobRole(id: number) { - this.showInputTextforOtherRole = false; - const otherJob = this.jobsAvailable.find((job) => job.id === +id); - if (otherJob && otherJob.other) { - this.showInputTextforOtherRole = true; + prefillFormIfExistingWorkerOrInfoSetInWorkerService(): void { + const worker = this.worker || this.workerService.newWorkerMandatoryInfo; + if (worker) { + this.form.patchValue({ + nameOrId: worker.nameOrId, + contract: worker.contract, + }); } } - renderInEditMode(): void { - this.form.patchValue({ - nameOrId: this.worker.nameOrId, - mainJob: this.worker.mainJob.jobId, - otherJobRole: this.worker.mainJob.other, - contract: this.worker.contract, - }); - - this.selectedJobRole(this.worker.mainJob.jobId); - } - private getReturnPath() { if (this.inMandatoryDetailsFlow) { this.returnUrl = this.getRoutePath('mandatory-details'); @@ -167,6 +110,7 @@ export class StaffDetailsComponent extends QuestionComponent implements OnInit, public onSubmit(): void { if (!this.submitAction.save) { + this.workerService.clearNewWorkerMandatoryInfo(); this.navigate(); return; }