Skip to content

Commit a2fcce9

Browse files
authored
Merge pull request #413 from bcgsc/bugfix/DEVSU-2165-new-project-user-undefined
[DEVSU-2165] new project user undefined
2 parents 6196a7c + 9fea680 commit a2fcce9

File tree

3 files changed

+70
-16
lines changed

3 files changed

+70
-16
lines changed

app/components/ReportAutocomplete/index.tsx

+34-8
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import {
66
Button,
77
} from '@mui/material';
88
import { ReportType } from '@/context/ReportContext';
9-
import api from '../../services/api';
109

1110
import './index.scss';
11+
import { useDebounce } from 'use-debounce';
12+
import { useSnackbar } from 'notistack';
13+
import api from '../../services/api';
1214

1315
type ReportAutocompleteProps = {
1416
defaultValue?: ReportType;
@@ -24,20 +26,44 @@ const ReportAutocomplete = ({
2426
const [options, setOptions] = useState([]);
2527
const [loading, setLoading] = useState(false);
2628
const [value, setValue] = useState<ReportType>(null);
29+
const [text, setText] = useState<string>(defaultValue?.patientId || '');
30+
const [debouncedText] = useDebounce(text, 500);
31+
const snackbar = useSnackbar();
2732

2833
useEffect(() => {
2934
if (defaultValue) {
3035
setValue(defaultValue);
3136
}
3237
}, [defaultValue]);
3338

34-
const handleInputChange = async ({ target: { value: queryValue } }) => {
35-
setLoading(true);
36-
const autocompleted = await api.get(`/reports?searchText=${queryValue}`, {}).request();
39+
useEffect(() => {
40+
setText((prevVal) => {
41+
if (!value?.patientId) { return ''; }
42+
if (prevVal !== value?.patientId) { return value?.patientId; }
43+
return prevVal;
44+
});
45+
}, [value?.patientId]);
46+
47+
useEffect(() => {
48+
const getData = async () => {
49+
try {
50+
setLoading(true);
51+
const reportsResp = await api.get(`/reports?searchText=${debouncedText}`).request();
52+
setOptions(reportsResp.reports);
53+
} catch (e) {
54+
snackbar.enqueueSnackbar('Error getting report autocomplete data');
55+
} finally {
56+
setLoading(false);
57+
}
58+
};
59+
if (debouncedText) {
60+
getData();
61+
}
62+
}, [snackbar, debouncedText]);
3763

38-
setOptions(autocompleted.reports);
39-
setLoading(false);
40-
};
64+
const handleTextChange = useCallback(({ target: { value: nextText } }) => {
65+
setText((prevText) => (prevText === nextText ? prevText : nextText));
66+
}, []);
4167

4268
const handleSubmit = useCallback(async () => {
4369
onSubmit(value);
@@ -58,7 +84,7 @@ const ReportAutocomplete = ({
5884
label={label || 'Report'}
5985
variant="outlined"
6086
margin="normal"
61-
onChange={handleInputChange}
87+
onChange={handleTextChange}
6288
InputProps={{
6389
...params.InputProps,
6490
endAdornment: (

app/components/UserAutocomplete/index.tsx

+34-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
CircularProgress,
66
Button,
77
} from '@mui/material';
8-
8+
import { useDebounce } from 'use-debounce';
9+
import { useSnackbar } from 'notistack';
910
import api from '@/services/api';
1011
import { UserType } from '@/common';
1112

@@ -27,19 +28,45 @@ const UserAutocomplete = ({
2728
const [options, setOptions] = useState([]);
2829
const [loading, setLoading] = useState(false);
2930
const [value, setValue] = useState<Partial<UserType>>(null);
31+
const [text, setText] = useState<string>((value ? `${value.firstName} ${value.lastName}` : '') || '');
32+
const [debouncedText] = useDebounce(text, 500);
33+
const snackbar = useSnackbar();
3034

3135
useEffect(() => {
3236
if (defaultValue) {
3337
setValue(defaultValue);
3438
}
3539
}, [defaultValue]);
3640

37-
const handleInputChange = async ({ target: { value: queryValue } }) => {
38-
setLoading(true);
39-
const autocompleted = await api.get(`/user/search?query=${queryValue}`, {}).request();
41+
useEffect(() => {
42+
setText((prevVal) => {
43+
if (!value?.firstName) { return ''; }
44+
if (prevVal !== `${value.firstName} ${value.lastName}`) {
45+
return `${value.firstName} ${value.lastName}`;
46+
}
47+
return prevVal;
48+
});
49+
}, [value?.firstName, value?.lastName]);
50+
51+
useEffect(() => {
52+
const getData = async () => {
53+
try {
54+
setLoading(true);
55+
const autocompleted = await api.get(`/user/search?query=${debouncedText}`, {}).request();
56+
setOptions(autocompleted);
57+
} catch (e) {
58+
snackbar.enqueueSnackbar('Error getting user autocomplete data');
59+
} finally {
60+
setLoading(false);
61+
}
62+
};
63+
if (debouncedText) {
64+
getData();
65+
}
66+
}, [snackbar, debouncedText]);
4067

41-
setOptions(autocompleted);
42-
setLoading(false);
68+
const handleTextChange = async ({ target: { value: nextText } }) => {
69+
setText((prevText) => (prevText === nextText ? prevText : nextText));
4370
};
4471

4572
const handleSubmit = useCallback(() => {
@@ -68,7 +95,7 @@ const UserAutocomplete = ({
6895
label={label || 'User'}
6996
variant="outlined"
7097
margin="normal"
71-
onChange={handleInputChange}
98+
onChange={handleTextChange}
7299
InputProps={{
73100
...params.InputProps,
74101
endAdornment: (

app/views/ProjectsView/components/AddEditProjectDialog/index.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ const AddEditProjectDialog = ({
121121
const projectUpdateResp = await projectUpdateCall.request();
122122

123123
// Handle relationships to projects
124-
const { added: usersToAdd, removed: usersToRemove } = getIdentDiff(editData.users ?? [], users);
124+
const { added: usersToAdd, removed: usersToRemove } = getIdentDiff(editData?.users ?? [], users);
125125
const { added: reportsToAdd, removed: reportsToRemove } = getIdentDiff(existingReports ?? [], reports);
126126

127127
const apiCallQueue = [];
@@ -136,6 +136,7 @@ const AddEditProjectDialog = ({
136136
const updatedProject = await api.get(`/project/${projectUpdateResp.ident}`).request();
137137
onClose(updatedProject);
138138
} catch (e) {
139+
console.error('handleClose ~ e:', e);
139140
snackbar.error(`Error ${editData ? 'editing' : 'creating'} report, ${e?.message}`);
140141
}
141142
} else {

0 commit comments

Comments
 (0)