Skip to content

Commit f293be0

Browse files
added api based autocomplete
1 parent 574613e commit f293be0

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

src/components/CippComponents/CippApiDialog.jsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,22 +111,23 @@ export const CippApiDialog = (props) => {
111111
</DialogContent>
112112
<DialogContent>
113113
{fields &&
114-
fields.map((field, index) => {
115-
switch (field.type) {
114+
fields.map((fieldProps, index) => {
115+
switch (fieldProps.type) {
116116
case "autoComplete":
117117
return (
118118
<Controller
119119
key={index}
120-
name={field.name}
120+
name={fieldProps.name}
121121
control={formHook.control}
122122
render={({ field }) => (
123123
<CippAutoComplete
124124
required
125+
api={fieldProps.api}
125126
creatable={false}
126127
multiple={false}
127-
defaultValue={"Select a bla"}
128-
options={["one", "two", "three"]}
129-
onChange={(e, nv) => field.onChange(nv)}
128+
defaultValue={fieldProps.label}
129+
options={fieldProps.options}
130+
onChange={(e, nv) => field.onChange(nv.value)}
130131
/>
131132
)}
132133
/>

src/components/CippComponents/CippAutocomplete.jsx

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { ArrowDropDown } from "@mui/icons-material";
22
import { Autocomplete, CircularProgress, createFilterOptions, TextField } from "@mui/material";
3+
import { ApiGetCall } from "../../api/ApiCall";
4+
import { useEffect, useState } from "react";
5+
import { useSettings } from "../../hooks/use-settings";
6+
import { getCippError } from "../../utils/get-cipp-error";
37

48
export const CippAutoComplete = (props) => {
59
const {
610
size,
11+
api,
712
label,
813
multiple = true,
914
creatable = true,
@@ -12,18 +17,51 @@ export const CippAutoComplete = (props) => {
1217
placeholder,
1318
disableClearable,
1419
name,
15-
options,
20+
options = [],
1621
onChange,
1722
required = false,
1823
sx,
1924
...other
2025
} = props;
2126
const filter = createFilterOptions();
27+
const [usedOptions, setUsedOptions] = useState(options);
28+
const [getRequestInfo, setGetRequestInfo] = useState({ url: "", waiting: false, queryKey: "" });
29+
const actionGetRequest = ApiGetCall({
30+
...getRequestInfo,
31+
onResult: (result) => {
32+
setPartialResults((prevResults) => [...prevResults, result]);
33+
},
34+
});
35+
const currentTenant = useSettings().currentTenant;
36+
useEffect(() => {
37+
if (api) {
38+
setGetRequestInfo({
39+
url: api.url,
40+
data: { tenantFilter: currentTenant, ...api.data },
41+
waiting: true,
42+
queryKey: api.queryKey,
43+
});
44+
}
45+
if (actionGetRequest.isSuccess) {
46+
const convertedOptions = actionGetRequest.data?.map((option) => {
47+
return { label: option[api.labelKey], value: option[api.valueKey] };
48+
});
49+
setUsedOptions(convertedOptions);
50+
}
51+
if (actionGetRequest.isError) {
52+
setUsedOptions({ label: "Error", value: getCippError(actionGetRequest.error) });
53+
}
54+
}, [api, actionGetRequest.data]);
55+
2256
return (
2357
<Autocomplete
2458
key={defaultValue}
2559
popupIcon={
26-
props.isFetching ? <CircularProgress color="inherit" size={20} /> : <ArrowDropDown />
60+
props.isFetching || actionGetRequest.isFetching ? (
61+
<CircularProgress color="inherit" size={20} />
62+
) : (
63+
<ArrowDropDown />
64+
)
2765
}
2866
value={value}
2967
filterSelectedOptions
@@ -41,7 +79,7 @@ export const CippAutoComplete = (props) => {
4179
defaultValue={defaultValue}
4280
name={name}
4381
onChange={onChange}
44-
options={options}
82+
options={api ? usedOptions : options}
4583
getOptionLabel={(option) => option.label || option}
4684
sx={sx}
4785
renderInput={(params) => (

src/pages/identity/users/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,26 @@ const Page = () => {
8686
confirmText: "Are you sure you want to set the out of office?",
8787
multiPost: false,
8888
},
89+
{
90+
label: "Add to Group",
91+
type: "POST",
92+
url: "/api/EditGroup",
93+
data: { addMember: { value: "userPrincipalName" }, TenantId: "Tenant" },
94+
fields: [
95+
{
96+
type: "autoComplete",
97+
name: "input",
98+
label: "Select a user",
99+
api: {
100+
url: "/api/Listusers",
101+
labelKey: "displayName",
102+
valueKey: "userPrincipalName",
103+
},
104+
},
105+
],
106+
confirmText: "Are you sure you want to set the out of office?",
107+
multiPost: false,
108+
},
89109
{
90110
label: "Disable Out of Office",
91111
type: "POST",

0 commit comments

Comments
 (0)