Passed
Push — feature/experience-form-modals ( 7402c2 )
by Tristan
05:47
created

SkillSubform.tsx ➔ SkillSubform   B

Complexity

Conditions 1

Size

Total Lines 141
Code Lines 114

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 114
dl 0
loc 141
rs 7
c 0
b 0
f 0
cc 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import React from "react";
2
import { FormattedMessage, useIntl, defineMessages } from "react-intl";
3
import { Field, FormikProps, FormikErrors, FormikTouched } from "formik";
4
import { jobShow } from "../../../helpers/routes";
5
import { getLocale } from "../../../helpers/localize";
6
import CheckboxGroup from "../../Form/CheckboxGroup";
7
import CheckboxInput from "../../Form/CheckboxInput";
8
9
const messages = defineMessages({
10
  linkToJobTitle: {
11
    id: "experienceModal.linkToJob.title",
12
    defaultMessage:
13
      "Open the job poster in a new tab or window to review the definition of skills.",
14
    description: "Title attribute for the link back to the job.",
15
  },
16
  skillCheckboxGroupLabel: {
17
    id: "experienceModal.skillCheckboxGroupLabel",
18
    defaultMessage: "Select all that apply:",
19
    description: "Label for the Skills checkbox groups.",
20
  },
21
});
22
23
export interface SkillFormValues {
24
  requiredSkills: string[];
25
  optionalSkills: string[];
26
}
27
28
interface SkillSubformProps<T extends SkillFormValues> {
29
  jobId: number;
30
  jobRequiredSkills: string[];
31
  jobOptionalSkills: string[];
32
  formikProps: FormikProps<T>;
33
}
34
35
export function SkillSubform<T extends SkillFormValues>({
36
  jobId,
37
  jobRequiredSkills,
38
  jobOptionalSkills,
39
  formikProps,
40
}: SkillSubformProps<T>): React.ReactElement {
41
  const intl = useIntl();
42
  const locale = getLocale(intl.locale);
43
  const {
44
    errors,
45
    touched,
46
  }: {
47
    errors: FormikErrors<SkillFormValues>;
48
    touched: FormikTouched<SkillFormValues>;
49
  } = formikProps;
50
51
  const linkToJob = (text): React.ReactElement => (
52
    <a
53
      href={jobShow(locale, jobId)}
54
      title={intl.formatMessage(messages.linkToJobTitle)}
55
      target="_blank"
56
      rel="noreferrer"
57
    >
58
      {text}
59
    </a>
60
  );
61
62
  return (
63
    <>
64
      <div data-c-container="medium">
65
        <p
66
          data-c-margin="top(1) bottom(1)"
67
          data-c-font-size="h4"
68
          data-c-font-weight="bold"
69
          data-c-color="c3"
70
        >
71
          <FormattedMessage
72
            id="experienceModal.connectSubtitle"
73
            defaultMessage="Connect This Experience to the Job"
74
            description="Subtitle of Connect-to-skills section."
75
          />
76
        </p>
77
        <p data-c-margin="bottom(1)">
78
          <FormattedMessage
79
            id="experienceModal.connectDescription"
80
            defaultMessage="Below you can select which of the job skills you used during this experience. Later on, you’ll be asked to provide a few sentences to help managers understand how you used this skill. You can <a>review the definitions</a> of the skills on the job poster."
81
            description="Explanation for Connect-to-skills section."
82
            values={{
83
              a: (...chunks): React.ReactElement => linkToJob(chunks),
84
            }}
85
          />
86
        </p>
87
        <p data-c-margin="bottom(1)">
88
          <FormattedMessage
89
            id="experienceModal.connectDescription.noSkillsOkay"
90
            defaultMessage="If none of the skills apply to this experience, feel free to save it without any skills selected."
91
            description="Explanation that you can save an experience without connecting it to skills yet."
92
          />
93
        </p>
94
      </div>
95
      <div data-c-container="medium">
96
        <div data-c-grid="gutter(all, 1) middle">
97
          <div data-c-grid-item="base(1of1)">
98
            <p data-c-font-weight="bold">
99
              <FormattedMessage
100
                id="experienceModal.requiredSkillsSubtitle"
101
                defaultMessage="Please indicate which of the following <a>required skills</a> (if any) you used in this experience."
102
                description="Title for the Required Skills checkbox."
103
                values={{
104
                  a: (...chunks): React.ReactElement => linkToJob(chunks),
105
                }}
106
              />
107
            </p>
108
          </div>
109
          <CheckboxGroup
110
            id="requiredSkills"
111
            label={intl.formatMessage(messages.skillCheckboxGroupLabel)}
112
            grid="base(1of1)"
113
            value={formikProps.values.requiredSkills}
114
            error={errors.requiredSkills}
115
            touched={touched.requiredSkills}
116
            onChange={formikProps.setFieldValue}
117
            onBlur={formikProps.setFieldTouched}
118
          >
119
            {jobRequiredSkills.map(
120
              (name): React.ReactElement => {
121
                return (
122
                  <Field
123
                    key={name}
124
                    id={name}
125
                    name={name}
126
                    label={name}
127
                    component={CheckboxInput}
128
                    grid="base(1of1)"
129
                  />
130
                );
131
              },
132
            )}
133
          </CheckboxGroup>
134
135
          <div data-c-grid-item="base(1of1)">
136
            <p data-c-font-weight="bold">
137
              <FormattedMessage
138
                id="experienceModal.optionalSkillsSubtitle"
139
                defaultMessage="Please indicate which of the following <a>optional skills</a> (if any) you used in this experience."
140
                description="Title for the Optional Skills checkbox."
141
                values={{
142
                  a: (...chunks): React.ReactElement => linkToJob(chunks),
143
                }}
144
              />
145
            </p>
146
          </div>
147
          <CheckboxGroup
148
            id="optionalSkills"
149
            label={intl.formatMessage(messages.skillCheckboxGroupLabel)}
150
            grid="base(1of1)"
151
            value={formikProps.values.optionalSkills}
152
            error={errors.optionalSkills}
153
            touched={touched.optionalSkills}
154
            onChange={formikProps.setFieldValue}
155
            onBlur={formikProps.setFieldTouched}
156
          >
157
            {jobOptionalSkills.map(
158
              (name): React.ReactElement => {
159
                return (
160
                  <Field
161
                    key={name}
162
                    id={name}
163
                    name={name}
164
                    label={name}
165
                    component={CheckboxInput}
166
                    grid="base(1of1)"
167
                  />
168
                );
169
              },
170
            )}
171
          </CheckboxGroup>
172
        </div>
173
      </div>
174
    </>
175
  );
176
}
177
178
export default SkillSubform;
179