Skip to content

Commit

Permalink
Merge branch 'develop' into bugfix/DEVSU-2153-print-report-missing-se…
Browse files Browse the repository at this point in the history
…ctions
  • Loading branch information
bnguyen-bcgsc committed May 22, 2024
2 parents 3493d27 + 2067b22 commit 131d244
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
5 changes: 3 additions & 2 deletions app/components/AsyncButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import './index.scss';

type AsyncButtonProps = {
className?: string;
children;
children?;
isLoading: boolean;
onClick?: () => void;
} & ButtonProps<'label', { component: 'label' }>;
} & ButtonProps<'label', { component?: 'label' }>;

const AsyncButton = ({
className,
Expand Down Expand Up @@ -46,6 +46,7 @@ const AsyncButton = ({
classes={{ label: `${loadingStarted ? 'async-button__label' : ''}` }}
className="async-button"
onClick={handleClick}
disabled={isLoading}
{...buttonProps}
>
{children}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import React, { useState, useEffect, useCallback } from 'react';
import {
Button,
CircularProgress,
Dialog,
DialogActions,
Expand All @@ -19,6 +18,7 @@ import { useForm, Controller } from 'react-hook-form';
import api, { ApiCallSet } from '@/services/api';
import { UserType, GroupType } from '@/common';
import snackbar from '@/services/SnackbarUtils';
import AsyncButton from '@/components/AsyncButton';
import {
ProjectType,
} from '../../../../types';
Expand Down Expand Up @@ -72,6 +72,7 @@ const AddEditUserDialog = ({
const [projectOptions, setProjectOptions] = useState<ProjectType[]>([]);
const [groupOptions, setGroupOptions] = useState<GroupType[]>([]);
const [dialogTitle, setDialogTitle] = useState<string>('');
const [isApiCalling, setIsApiCalling] = useState(false);

// Grab project and groups
useEffect(() => {
Expand Down Expand Up @@ -107,6 +108,7 @@ const AddEditUserDialog = ({
}, [editData, groupOptions, projectOptions, setValue]);

const handleClose = useCallback(async (formData: UserForm) => {
setIsApiCalling(true);
const {
firstName,
lastName,
Expand All @@ -127,14 +129,21 @@ const AddEditUserDialog = ({
};

try {
const addEditResp = editData
? await api.put(`/user/${editData.ident}`, userReq).request()
: await api.post('/user', userReq).request();
let addEditResp;
try {
addEditResp = editData
? await api.put(`/user/${editData.ident}`, userReq).request()
: await api.post('/user', userReq).request();
} catch (e) {
throw { ...e, errorType: 'user detail' };
}

// Project Section
if (dirtyFields.projects && editData) {
const existingProjects = editData.projects.map(({ ident }) => ident);

if (dirtyFields.projects) {
let existingProjects = [];
if (editData) {
existingProjects = editData.projects.map(({ ident }) => ident);
}
const toAddProjs = projects.filter((projectId) => !existingProjects.includes(projectId));
const toRemoveProjs = existingProjects.filter((projectId) => !projects.includes(projectId));

Expand All @@ -150,8 +159,11 @@ const AddEditUserDialog = ({
}

// Groups Section
if (dirtyFields.groups && editData) {
const existingGroups = editData.groups.map(({ ident }) => ident);
if (dirtyFields.groups) {
let existingGroups = [];
if (editData) {
existingGroups = editData.groups.map(({ ident }) => ident);
}

const toAddGroups = groups.filter((groupId) => !existingGroups.includes(groupId));
const toRemoveGroups = existingGroups.filter((groupId) => !groups.includes(groupId));
Expand All @@ -169,6 +181,7 @@ const AddEditUserDialog = ({
addEditResp.projects = projectOptions.filter(({ ident }) => projects.includes(ident));
addEditResp.groups = groupOptions.filter(({ ident }) => groups.includes(ident));

setIsApiCalling(false);
snackbar.success(`User successfully ${editData ? 'updated' : 'created'}`);
onClose(addEditResp);
} catch (e) {
Expand All @@ -179,10 +192,8 @@ const AddEditUserDialog = ({
if (e.errorType) {
snackbar.error(`Error setting ${e.errorType}s related to user${content}`);
} else {
console.dir(e);
snackbar.error(`Error ${editData ? 'editing' : 'creating'} user${content}`);
}
console.error(e);
}
}

Expand Down Expand Up @@ -343,12 +354,12 @@ const AddEditUserDialog = ({
)}
</DialogContent>
<DialogActions className="edit-dialog__actions">
<Button color="primary" onClick={() => onClose(null)}>
<AsyncButton isLoading={isApiCalling} color="primary" onClick={() => onClose(null)}>
Cancel
</Button>
<Button color="primary" onClick={handleSubmit(handleClose)}>
</AsyncButton>
<AsyncButton isLoading={isApiCalling} color="primary" onClick={handleSubmit(handleClose)}>
Save
</Button>
</AsyncButton>
</DialogActions>
</Dialog>
);
Expand Down

0 comments on commit 131d244

Please sign in to comment.