ENCOA-289

This commit is contained in:
Carlos-Mesquita
2024-12-24 11:52:34 +00:00
parent 8d7b47312e
commit bac2a08748
3 changed files with 74 additions and 7 deletions

View File

@@ -87,7 +87,15 @@ export default function BatchCreateUser({ user, entities = [], permissions, onFi
const [isExpiryDateEnabled, setIsExpiryDateEnabled] = useState(true);
const [type, setType] = useState<Type>("student");
const [showHelp, setShowHelp] = useState(false);
const [entity, setEntity] = useState((entities || [])[0]?.id || undefined)
const [entity, setEntity] = useState<{id: string | null, label: string | null}| undefined>(() => {
if (!entities?.length) {
return undefined;
}
return {
id: entities[0].id,
label: entities[0].label
};
});
const { openFilePicker, filesContent, clear } = useFilePicker({
accept: ".xlsx",
@@ -291,11 +299,28 @@ export default function BatchCreateUser({ user, entities = [], permissions, onFi
if (!!crossRefEmails) {
const existingEmails = new Set(crossRefEmails.map((x: any) => x.email));
const dupes = infos.filter(info => existingEmails.has(info.email));
const newUsersList = infos.filter(info => !existingEmails.has(info.email));
const newUsersList = infos
.filter(info => !existingEmails.has(info.email))
.map(info => ({
...info,
entityLabels: [entity!.label!]
}));
setNewUsers(newUsersList);
setDuplicatedUsers(dupes);
const {data: emailEntityMap} = await axios.post("/api/users/controller?op=getEntities", {
emails: dupes.map((x) => x.email)
});
const withLabels = dupes.map((u) => ({
...u,
entityLabels: emailEntityMap.find((e: any) => e.email === u.email)?.entityLabels || []
}))
setDuplicatedUsers(withLabels);
} else {
setNewUsers(infos);
const withLabel = infos.map(info => ({
...info,
entityLabels: [entity!.label!]
}));
setNewUsers(withLabel);
}
} catch (error) {
toast.error("Something went wrong, please try again later!");
@@ -305,7 +330,7 @@ export default function BatchCreateUser({ user, entities = [], permissions, onFi
if (infos.length > 0) {
crossReferenceEmails();
}
}, [infos]);
}, [infos, entity]);
const makeUsers = async () => {
const newUsersSentence = newUsers.length > 0 ? `create ${newUsers.length} user(s)` : undefined;
@@ -459,7 +484,16 @@ export default function BatchCreateUser({ user, entities = [], permissions, onFi
<Select
defaultValue={{ value: (entities || [])[0]?.id, label: (entities || [])[0]?.label }}
options={entities.map((e) => ({ value: e.id, label: e.label }))}
onChange={(e) => setEntity(e?.value || undefined)}
onChange={(e) => {
if (!e) {
setEntity(undefined);
return;
}
setEntity({
id: e?.value,
label: e?.label
});
}}
isClearable={checkAccess(user, ["admin", "developer"])}
/>
</div>