Skip to content

Commit 9924bb4

Browse files
committed
ILEX-135 fix some logic
1 parent 69ce8bc commit 9924bb4

File tree

3 files changed

+332
-180
lines changed

3 files changed

+332
-180
lines changed

src/components/TermEditor/newTerm/AddNewTermDialog.jsx

Lines changed: 125 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -30,73 +30,91 @@ const HeaderRightSideContent = ({
3030
onContinue,
3131
onClose,
3232
isCreateButtonDisabled,
33-
isEditing
33+
isEditing,
34+
hasExactMatch,
35+
hasAnySynonymMatch
3436
}) => {
3537
const [ontologyChecked, setOntologyChecked] = useState(false);
3638

3739
const handleOntologyChange = (event) => {
3840
setOntologyChecked(event.target.checked);
3941
};
4042

43+
const getButtonText = () => {
44+
if (isEditing) return 'Edit term';
45+
if (hasExactMatch || hasAnySynonymMatch) return 'Add to existing';
46+
return 'Create new';
47+
};
48+
49+
if (activeStep === 2) {
50+
return (
51+
<Button variant="contained" onClick={onClose}>
52+
Finish
53+
</Button>
54+
);
55+
}
56+
4157
return (
4258
<Box display='flex' alignItems='center'>
43-
{activeStep !== 2 ? (
44-
<>
45-
<FormControlLabel
46-
control={
47-
<Checkbox
48-
size="small"
49-
icon={<UncheckedIcon />}
50-
checkedIcon={<CheckedIcon />}
51-
checked={ontologyChecked}
52-
onChange={handleOntologyChange}
53-
/>
54-
}
55-
sx={{ color: gray600 }}
56-
label="Add to ontology"
57-
/>
58-
<OntologySearch disabled={!ontologyChecked} />
59-
<Divider orientation="vertical" flexItem sx={{ m: '0 1rem' }} />
60-
<MobileStepper
61-
variant="dots"
62-
steps={3}
63-
position="static"
64-
activeStep={activeStep}
65-
sx={{ maxWidth: 64, flexGrow: 1 }}
59+
<FormControlLabel
60+
control={
61+
<Checkbox
62+
size="small"
63+
icon={<UncheckedIcon />}
64+
checkedIcon={<CheckedIcon />}
65+
checked={ontologyChecked}
66+
onChange={handleOntologyChange}
6667
/>
67-
<Divider orientation="vertical" flexItem sx={{ m: '0 1rem' }} />
68-
<Stack direction="row" spacing={1.5}>
69-
<CustomButton onClick={onClose}>Cancel</CustomButton>
70-
<Button
71-
onClick={onContinue}
72-
disabled={isCreateButtonDisabled}
73-
variant="contained"
74-
sx={{
75-
padding: '0.625rem 0.875rem',
76-
'&.Mui-disabled': {
77-
border: `1px solid ${gray200}`,
78-
color: gray400,
79-
backgroundColor: gray100
80-
}
81-
}}
82-
>
83-
{isEditing ? 'Edit term' : 'Create new'}
84-
</Button>
85-
</Stack>
86-
</>
87-
) : (
88-
<Button variant="contained" onClick={onClose}>Finish</Button>
89-
)}
68+
}
69+
sx={{ color: gray600 }}
70+
label="Add to ontology"
71+
/>
72+
<OntologySearch disabled={!ontologyChecked} />
73+
74+
<Divider orientation="vertical" flexItem sx={{ m: '0 1rem' }} />
75+
76+
<MobileStepper
77+
variant="dots"
78+
steps={3}
79+
position="static"
80+
activeStep={activeStep}
81+
sx={{ maxWidth: 64, flexGrow: 1 }}
82+
/>
83+
84+
<Divider orientation="vertical" flexItem sx={{ m: '0 1rem' }} />
85+
86+
<Stack direction="row" spacing={1.5}>
87+
<CustomButton onClick={onClose}>
88+
Cancel
89+
</CustomButton>
90+
<Button
91+
onClick={onContinue}
92+
disabled={isCreateButtonDisabled}
93+
variant="contained"
94+
sx={{
95+
padding: '0.625rem 0.875rem',
96+
'&.Mui-disabled': {
97+
border: `1px solid ${gray200}`,
98+
color: gray400,
99+
backgroundColor: gray100
100+
}
101+
}}
102+
>
103+
{getButtonText()}
104+
</Button>
105+
</Stack>
90106
</Box>
91-
)
107+
);
92108
};
93109

94110
HeaderRightSideContent.propTypes = {
95111
activeStep: PropTypes.number.isRequired,
96112
onContinue: PropTypes.func.isRequired,
97113
onClose: PropTypes.func.isRequired,
98114
isCreateButtonDisabled: PropTypes.bool.isRequired,
99-
isEditing: PropTypes.bool.isRequired
115+
isEditing: PropTypes.bool.isRequired,
116+
hasExactMatch: PropTypes.bool.isRequired,
117+
hasAnySynonymMatch: PropTypes.bool.isRequired
100118
};
101119

102120
const AddNewTermDialog = ({ open, handleClose }) => {
@@ -109,18 +127,16 @@ const AddNewTermDialog = ({ open, handleClose }) => {
109127
const [existingIds, setExistingIds] = useState([]);
110128
const [loading, setLoading] = useState(false);
111129
const [hasExactMatch, setHasExactMatch] = useState(false);
130+
const [hasAnySynonymMatch, setHasAnySynonymMatch] = useState(false);
112131
const [isEditing, setIsEditing] = useState(false);
132+
113133
const { user } = useContext(GlobalDataContext);
114134

115135
const isCreateButtonDisabled = useMemo(() => {
116-
if (hasExactMatch) return true;
117-
118136
if (termValue === "") return true;
119-
120137
if (isEditing && termValue === selectedTermValue) return true;
121-
122138
return false;
123-
}, [hasExactMatch, termValue, isEditing, selectedTermValue]);
139+
}, [termValue, isEditing, selectedTermValue]);
124140

125141
const statusProps = getAddTermStatusProps(addTermResponse, termValue);
126142

@@ -135,9 +151,7 @@ const AddNewTermDialog = ({ open, handleClose }) => {
135151
setTermValue(newValue);
136152

137153
if (isEventObject && isEditing) {
138-
if (newValue !== selectedTermValue) {
139-
setIsEditing(true);
140-
}
154+
setIsEditing(newValue !== selectedTermValue);
141155
} else if (isEventObject) {
142156
setIsEditing(false);
143157
}
@@ -155,23 +169,23 @@ const AddNewTermDialog = ({ open, handleClose }) => {
155169
setExistingIds(newValue);
156170
};
157171

158-
const handleExactMatchChange = (value) => {
159-
setHasExactMatch(value)
160-
}
172+
const handleExactMatchChange = useCallback((value) => {
173+
setHasExactMatch(value);
174+
}, []);
161175

162176
const handleTermSelection = (result) => {
163177
if (result?.label) {
164178
setSelectedTermValue(result.label);
165179
handleTermValueChange(result.label);
166180
}
167-
}
181+
};
168182

169183
const editTerm = useCallback(() => {
170184
console.log("Edit term");
171185
}, []);
172186

173187
const createNewTerm = useCallback(async () => {
174-
if (!termValue || !selectedType || hasExactMatch) return;
188+
if (!termValue || !selectedType) return;
175189

176190
setLoading(true);
177191

@@ -183,22 +197,27 @@ const AddNewTermDialog = ({ open, handleClose }) => {
183197
};
184198

185199
try {
186-
const response = await createNewEntity({
187-
group: groupName,
188-
data: body,
189-
session: token
190-
});
191-
192-
if (response.term && response.term.id) {
200+
if (hasExactMatch || hasAnySynonymMatch) {
193201
setActiveStep(1);
194-
setAddTermResponse(response.term.id);
202+
setAddTermResponse(termValue);
203+
} else {
204+
const response = await createNewEntity({
205+
group: groupName,
206+
data: body,
207+
session: token
208+
});
209+
210+
if (response.term && response.term.id) {
211+
setActiveStep(1);
212+
setAddTermResponse(response.term.id);
213+
}
195214
}
196215
} catch (error) {
197-
console.error("Creation failed:", error);
216+
console.error("Action failed:", error);
198217
} finally {
199218
setLoading(false);
200219
}
201-
}, [termValue, selectedType, user, hasExactMatch]);
220+
}, [termValue, selectedType, user, hasExactMatch, hasAnySynonymMatch]);
202221

203222
const handleAction = useCallback(() => {
204223
if (isEditing) {
@@ -209,9 +228,16 @@ const AddNewTermDialog = ({ open, handleClose }) => {
209228
}, [isEditing, editTerm, createNewTerm]);
210229

211230
if (loading) {
212-
return <Box sx={{ display: 'flex', alignItems: 'center', justifyContent: 'center', width: '100%' }}>
213-
<CircularProgress />
214-
</Box>
231+
return (
232+
<Box sx={{
233+
display: 'flex',
234+
alignItems: 'center',
235+
justifyContent: 'center',
236+
width: '100%'
237+
}}>
238+
<CircularProgress />
239+
</Box>
240+
);
215241
}
216242

217243
return (
@@ -226,25 +252,34 @@ const AddNewTermDialog = ({ open, handleClose }) => {
226252
onContinue={handleAction}
227253
isCreateButtonDisabled={isCreateButtonDisabled}
228254
isEditing={isEditing}
255+
hasExactMatch={hasExactMatch}
256+
hasAnySynonymMatch={hasAnySynonymMatch}
229257
/>
230258
}
231259
sx={{ '& .MuiDialogContent-root': { padding: 0, overflowY: "hidden" } }}
232260
>
233-
{activeStep === 0 && <FirstStepContent
234-
term={termValue}
235-
type={selectedType}
236-
hasExactMatch={hasExactMatch}
237-
existingIds={existingIds}
238-
synonyms={exactSynonyms}
239-
isEditing={isEditing}
240-
handleTermChange={handleTermValueChange}
241-
handleTypeChange={handleTypeChange}
242-
handleExactMatchChange={handleExactMatchChange}
243-
handleSynonymChange={handleSynonymChange}
244-
handleExistingIdChange={handleExistingIdChange}
245-
onTermSelect={handleTermSelection}
246-
/>}
247-
{activeStep === 1 && <SecondStepContent searchTerm={addTermResponse} />}
261+
{activeStep === 0 && (
262+
<FirstStepContent
263+
term={termValue}
264+
type={selectedType}
265+
hasExactMatch={hasExactMatch}
266+
existingIds={existingIds}
267+
synonyms={exactSynonyms}
268+
isEditing={isEditing}
269+
handleTermChange={handleTermValueChange}
270+
handleTypeChange={handleTypeChange}
271+
handleExactMatchChange={handleExactMatchChange}
272+
handleSynonymChange={handleSynonymChange}
273+
onAnySynonymMatchChange={setHasAnySynonymMatch}
274+
handleExistingIdChange={handleExistingIdChange}
275+
onTermSelect={handleTermSelection}
276+
/>
277+
)}
278+
279+
{activeStep === 1 && (
280+
<SecondStepContent searchTerm={addTermResponse} />
281+
)}
282+
248283
{activeStep === 2 && addTermResponse != null && (
249284
<StatusStep statusProps={statusProps} />
250285
)}

0 commit comments

Comments
 (0)