Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] work experience form reset fields bug #12435

Merged
merged 10 commits into from
Jan 10, 2025
Merged
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
17 changes: 16 additions & 1 deletion api/app/Generators/ApplicationDocGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,22 @@ public function generate(): self
}

if (isset($snapshot['experiences'])) {
$experiences = Experience::hydrateSnapshot($snapshot['experiences']);
$snapshotExperiences = $snapshot['experiences'];

// the snapshot stores the department and classification models connected by relation
// to render with GeneratesUserDoc, map the model to a string with the appropriate property name
foreach ($snapshotExperiences as &$experience) {
if ($experience['__typename'] === 'WorkExperience') {
if (isset($experience['department'])) {
$experience['departmentId'] = $experience['department']['id'];
}
if (isset($experience['classification'])) {
$experience['classificationId'] = $experience['classification']['id'];
}
}
}

$experiences = Experience::hydrateSnapshot($snapshotExperiences);
$this->experiences($section, collect($experiences), false, 2);
}

Expand Down
21 changes: 20 additions & 1 deletion api/app/Models/WorkExperience.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Models;

use App\Enums\CafForce;
use App\Enums\EmploymentCategory;
use App\Models\Scopes\MatchExperienceType;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -83,7 +85,24 @@ class WorkExperience extends Experience

public function getTitle(?string $lang = 'en'): string
{
return sprintf('%s %s %s', $this->role, Lang::get('common.at', [], $lang), $this->organization);
if ($this->employment_category === EmploymentCategory::EXTERNAL_ORGANIZATION->name) {
return sprintf('%s %s %s', $this->role, Lang::get('common.with', [], $lang), $this->organization);
}

if ($this->employment_category === EmploymentCategory::GOVERNMENT_OF_CANADA->name) {
/** @var Department | null $department */
$department = $this->department_id ? Department::find($this->department_id) : null;

return sprintf('%s %s %s', $this->role, Lang::get('common.with', [], $lang), $department ? $department->name[$lang] : Lang::get('common.not_found', [], $lang));
}

if ($this->employment_category === EmploymentCategory::CANADIAN_ARMED_FORCES->name) {
$caf_force = $this->caf_force ? CafForce::localizedString($this->caf_force)[$lang] : Lang::get('common.not_found', [], $lang);

return sprintf('%s %s %s', $this->role, Lang::get('common.with', [], $lang), $caf_force);
}

return sprintf('%s %s %s', $this->role, Lang::get('common.with', [], $lang), $this->organization);
}

// extends dateRange, check if the end date is in the future and append a message if needed
Expand Down
6 changes: 3 additions & 3 deletions api/app/Traits/Generator/GeneratesUserDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public function experience(Section $section, AwardExperience|CommunityExperience
sprintf(
'%s %s %s',
$experience->role,
Lang::get('common.at', [], $this->lang),
Lang::get('common.with', [], $this->lang),
$this->localizeEnum($experience->caf_force,
CafForce::class),
),
Expand All @@ -362,12 +362,12 @@ public function experience(Section $section, AwardExperience|CommunityExperience
);
} elseif ($experience->employment_category === EmploymentCategory::GOVERNMENT_OF_CANADA->name) {
/** @var Department | null $department */
$department = Department::find($experience->department_id);
$department = $experience->department_id ? Department::find($experience->department_id) : null;
$section->addTitle(
sprintf(
'%s %s %s',
$experience->role,
Lang::get('common.at', [], $this->lang),
Lang::get('common.with', [], $this->lang),
$department ? $department->name[$this->lang] : Lang::get('common.not_found', [], $this->lang),
),
$headingRank
Expand Down
1 change: 1 addition & 0 deletions api/lang/en/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'status' => 'Status',
'present' => 'Present',
'at' => 'at',
'with' => 'with',
'not_provided' => 'Not provided',
'not_sure' => 'Not sure',
'expected_end_date' => '(Expected end date)',
Expand Down
1 change: 1 addition & 0 deletions api/lang/fr/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'status' => 'Statut',
'present' => 'Aujourd’hui',
'at' => 'à',
'with' => 'à',
'not_provided' => 'Renseignements manquants',
'not_sure' => 'Pas certain',
'expected_end_date' => '(Date de fin prévue)',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ const GovFields = ({ labels }: SubExperienceFormProps) => {
label={labels.classificationGroup}
name="classificationGroup"
nullSelection={intl.formatMessage(
uiMessages.nullSelectionOptionLevel,
uiMessages.nullSelectionOptionGroup,
)}
rules={{
required: intl.formatMessage(errorMessages.required),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useIntl, defineMessage, MessageDescriptor } from "react-intl";
import { useQuery } from "urql";
import { useFormContext, useWatch } from "react-hook-form";
import { useEffect } from "react";
import { useEffect, useRef } from "react";

import {
FieldLabels,
Expand Down Expand Up @@ -92,6 +92,9 @@ const WorkFields = ({ labels }: SubExperienceFormProps) => {

const { resetField, formState } = useFormContext<WorkFormValues>();

const prevEmploymentCategory = useRef<EmploymentCategory | null | undefined>(
formState.defaultValues?.employmentCategory,
);
const watchEmploymentCategory = useWatch<{
employmentCategory: EmploymentCategory;
}>({
Expand All @@ -118,7 +121,7 @@ const WorkFields = ({ labels }: SubExperienceFormProps) => {
resetField(name, { keepDirty: false, defaultValue: null });
};

if (formState.dirtyFields.employmentCategory) {
if (prevEmploymentCategory.current !== watchEmploymentCategory) {
resetDirtyField("team"); // both external and goc

// external fields
Expand Down Expand Up @@ -146,7 +149,9 @@ const WorkFields = ({ labels }: SubExperienceFormProps) => {
resetDirtyField("currentRole");
resetDirtyField("endDate");
}
}, [formState.dirtyFields, watchEmploymentCategory, resetField]);

prevEmploymentCategory.current = watchEmploymentCategory;
}, [watchEmploymentCategory, resetField]);

return (
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,9 @@ const FormFields = ({
id="currentClassificationGroup"
label={labels.currentClassificationGroup}
name="currentClassificationGroup"
nullSelection={intl.formatMessage({
defaultMessage: "Select a group",
id: "9Upe1V",
description: "Null selection for form.",
})}
nullSelection={intl.formatMessage(
uiMessages.nullSelectionOptionGroup,
)}
rules={{
required: intl.formatMessage(errorMessages.required),
}}
Expand Down
4 changes: 0 additions & 4 deletions apps/web/src/lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -2263,10 +2263,6 @@
"defaultMessage": "Organisation",
"description": "Label displayed on Work Experience form for organization input"
},
"9Upe1V": {
"defaultMessage": "Sélectionnez un groupe",
"description": "Null selection for form."
},
"9VPBwR": {
"defaultMessage": "Homme danseur en régalia traditionnelle.",
"description": "Indigenous Apprenticeship lower back image text alternative"
Expand Down
33 changes: 30 additions & 3 deletions apps/web/src/utils/experienceUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -639,14 +639,41 @@ const getWorkExperienceDefaultValues = (
cafForce,
cafRank,
} = experience;

const isIndeterminate =
govEmploymentType?.value === WorkExperienceGovEmployeeType.Indeterminate;
const indeterminateActing =
isIndeterminate && govPositionType?.value === GovPositionType.Acting;
const indeterminateAssignment =
isIndeterminate && govPositionType?.value === GovPositionType.Assignment;
const indeterminateSecondment =
isIndeterminate && govPositionType?.value === GovPositionType.Secondment;

const expectedEndDate =
govEmploymentType?.value === WorkExperienceGovEmployeeType.Student ||
govEmploymentType?.value === WorkExperienceGovEmployeeType.Casual ||
govEmploymentType?.value === WorkExperienceGovEmployeeType.Term ||
indeterminateActing ||
indeterminateAssignment ||
indeterminateSecondment;

let currentRole = false;

if (endDate) {
currentRole = false;
if (expectedEndDate) {
currentRole = endDate >= strToFormDate(new Date().toISOString());
}
} else {
currentRole = true;
}

return {
role,
organization,
team: division,
startDate,
currentRole: endDate
? endDate >= strToFormDate(new Date().toISOString()) // today's date
: true,
currentRole,
endDate,
employmentCategory: employmentCategory?.value,
extSizeOfOrganization: extSizeOfOrganization?.value,
Expand Down
4 changes: 4 additions & 0 deletions packages/i18n/src/lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,10 @@
"defaultMessage": "Technologie de l’information",
"description": "Full name of abbreviation for IT"
},
"nICORU": {
"defaultMessage": "Sélectionnez un groupe",
"description": "Null selection og group for select input."
},
"nMX0n8": {
"defaultMessage": "Vous n’avez ajouté aucune expérience de ce type.",
"description": "Message to user when no experiences of that type exist."
Expand Down
5 changes: 5 additions & 0 deletions packages/i18n/src/messages/uiMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ const uiMessages = defineMessages({
id: "xEkbvy",
description: "Null selection of level for select input",
},
nullSelectionOptionGroup: {
defaultMessage: "Select a group",
id: "nICORU",
description: "Null selection og group for select input.",
},
});

export default uiMessages;
Loading