Skip to content

Commit 2bb1fd1

Browse files
committed
Improved code
Signed-off-by: Yi Cai <[email protected]>
1 parent ccca59b commit 2bb1fd1

File tree

2 files changed

+64
-36
lines changed

2 files changed

+64
-36
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react';
2+
3+
import { FormHelperText, TextField } from '@material-ui/core';
4+
5+
interface KeyValueTextFieldProps {
6+
label: string;
7+
name: string;
8+
value: string;
9+
onChange: (
10+
event: React.FocusEvent<HTMLTextAreaElement | HTMLInputElement>,
11+
) => void;
12+
}
13+
14+
const KeyValueTextField: React.FC<KeyValueTextFieldProps> = ({
15+
label,
16+
name,
17+
value,
18+
onChange,
19+
}) => {
20+
return (
21+
<div>
22+
<TextField
23+
multiline
24+
label={label}
25+
placeholder="key1: value2; key2: value2"
26+
variant="outlined"
27+
margin="normal"
28+
fullWidth
29+
name={name}
30+
value={value}
31+
onChange={onChange}
32+
/>
33+
<FormHelperText style={{ marginLeft: '1rem' }}>
34+
Use semicolon to separate {label.toLowerCase()}
35+
</FormHelperText>
36+
</div>
37+
);
38+
};
39+
40+
export default KeyValueTextField;

plugins/bulk-import/src/components/PreviewFile/PreviewPullRequest.tsx

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { useFormikContext } from 'formik';
2424

2525
import { AddRepositoriesFormValues, PullRequestPreviewData } from '../../types';
2626
import { getYamlKeyValuePairs } from '../../utils/repository-utils';
27+
import KeyValueTextField from './KeyValueTextField';
2728

2829
const useDrawerContentStyles = makeStyles(theme => ({
2930
previewCard: {
@@ -209,6 +210,20 @@ export const PreviewPullRequest = ({
209210
}
210211
};
211212

213+
const keyValueTextFields = [
214+
{
215+
label: 'Annotations',
216+
name: 'prAnnotations',
217+
value: pullRequest?.[repoName]?.prAnnotations,
218+
},
219+
{
220+
label: 'Labels',
221+
name: 'prLabels',
222+
value: pullRequest?.[repoName]?.prLabels,
223+
},
224+
{ label: 'Spec', name: 'prSpec', value: pullRequest?.[repoName]?.prSpec },
225+
];
226+
212227
return (
213228
<>
214229
<Box marginTop={2}>
@@ -353,42 +368,15 @@ export const PreviewPullRequest = ({
353368
WARNING: This may fail if no CODEOWNERS file is found at the target
354369
location.
355370
</FormHelperText>
356-
<TextField
357-
multiline
358-
label="Annotations"
359-
placeholder="key1: value2; key2: value2"
360-
variant="outlined"
361-
margin="normal"
362-
fullWidth
363-
name={`repositories.${pullRequest[repoName].componentName}.prAnnotations`}
364-
value={pullRequest?.[repoName]?.prAnnotations}
365-
onChange={handleChange}
366-
/>
367-
<FormHelperText>Use semicolon to separate annotations</FormHelperText>
368-
<TextField
369-
multiline
370-
label="Labels"
371-
placeholder="key1: value2; key2: value2"
372-
variant="outlined"
373-
margin="normal"
374-
fullWidth
375-
name={`repositories.${pullRequest[repoName].componentName}.prLabels`}
376-
value={pullRequest?.[repoName]?.prLabels}
377-
onChange={handleChange}
378-
/>
379-
<FormHelperText>Use semicolon to separate labels</FormHelperText>
380-
<TextField
381-
multiline
382-
label="Spec"
383-
placeholder="key1: value2; key2: value2"
384-
variant="outlined"
385-
margin="normal"
386-
fullWidth
387-
name={`repositories.${pullRequest[repoName].componentName}.prSpec`}
388-
value={pullRequest?.[repoName]?.prSpec}
389-
onChange={handleChange}
390-
/>
391-
<FormHelperText>Use semicolon to separate specs</FormHelperText>
371+
{keyValueTextFields.map(field => (
372+
<KeyValueTextField
373+
key={field.name}
374+
label={field.label}
375+
name={`repositories.${pullRequest[repoName].componentName}.${field.name}`}
376+
value={field.value || ''}
377+
onChange={handleChange}
378+
/>
379+
))}
392380
<Box marginTop={2}>
393381
<Typography variant="h6">
394382
Preview {`${approvalTool.toLowerCase()}`}

0 commit comments

Comments
 (0)