Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add loading for autocomplete user and role #244

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import useAppStore from '../../../../store/useStore';
import { FetchedData, IRoles } from '../../../../utils/interfaces';
import { debounce } from '../../../../helpers/helper';
import TcAutocomplete from '../../../shared/TcAutocomplete';
import { Chip } from '@mui/material';
import { Chip, CircularProgress } from '@mui/material';

interface ITcRolesAutoCompleteProps {
isEdit?: boolean;
Expand All @@ -29,14 +29,15 @@ function TcRolesAutoComplete({
const [selectedRoles, setSelectedRoles] = useState<IRoles[]>([]);

const [fetchedRoles, setFetchedRoles] = useState<FetchedData>({
limit: 100,
limit: 8,
page: 1,
results: [],
totalPages: 0,
totalResults: 0,
});
const [filteredRolesByName, setFilteredRolesByName] = useState<string>('');
const [isInitialized, setIsInitialized] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(false);

const fetchDiscordRoles = async (
platformId: string,
Expand All @@ -45,6 +46,8 @@ function TcRolesAutoComplete({
name?: string
) => {
try {
setIsLoading(true);

const fetchedRoles = await retrievePlatformProperties({
platformId,
name: name,
Expand Down Expand Up @@ -73,7 +76,10 @@ function TcRolesAutoComplete({
};
});
}
} catch (error) {}
} catch (error) {
} finally {
setIsLoading(false);
}
};

useEffect(() => {
Expand All @@ -92,7 +98,7 @@ function TcRolesAutoComplete({
if (inputValue === '') {
setFilteredRolesByName('');
setFetchedRoles({
limit: 100,
limit: 8,
page: 1,
results: [],
totalPages: 0,
Expand Down Expand Up @@ -149,6 +155,12 @@ function TcRolesAutoComplete({
getOptionLabel={(option) => option.name}
label={'Select Role(s)'}
multiple={true}
loading={isLoading}
loadingText={
<div className="text-center">
<CircularProgress size={24} />
</div>
}
disabled={isDisabled}
value={selectedRoles}
onChange={handleChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import useAppStore from '../../../../store/useStore';
import { FetchedData, IUser } from '../../../../utils/interfaces';
import { debounce } from '../../../../helpers/helper';
import TcAutocomplete from '../../../shared/TcAutocomplete';
import { Chip } from '@mui/material';
import { Chip, CircularProgress } from '@mui/material';

interface ITcUsersAutoCompleteProps {
isEdit?: boolean;
Expand All @@ -29,14 +29,15 @@ function TcUsersAutoComplete({
const [selectedUsers, setSelectedUsers] = useState<IUser[]>([]);

const [fetchedUsers, setFetchedUsers] = useState<FetchedData>({
limit: 100,
limit: 8,
page: 1,
results: [],
totalPages: 0,
totalResults: 0,
});
const [filteredUsersByName, setFilteredUsersByName] = useState<string>('');
const [isInitialized, setIsInitialized] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(false);

const fetchDiscordUsers = async (
platformId: string,
Expand All @@ -45,6 +46,8 @@ function TcUsersAutoComplete({
ngu?: string
) => {
try {
setIsLoading(true);

const fetchedUsers = await retrievePlatformProperties({
platformId,
ngu: ngu,
Expand Down Expand Up @@ -73,7 +76,10 @@ function TcUsersAutoComplete({
};
});
}
} catch (error) {}
} catch (error) {
} finally {
setIsLoading(false);
}
};

useEffect(() => {
Expand All @@ -83,6 +89,11 @@ function TcUsersAutoComplete({

const debouncedFetchDiscordUsers = debounce(fetchDiscordUsers, 700);

const handleClearAll = () => {
if (!platformId) return;
fetchDiscordUsers(platformId, fetchedUsers.page, fetchedUsers.limit);
};

const handleSearchChange = (event: React.SyntheticEvent<Element, Event>) => {
const target = event.target as HTMLInputElement;
const inputValue = target.value;
Expand All @@ -92,16 +103,16 @@ function TcUsersAutoComplete({
if (inputValue === '') {
setFilteredUsersByName('');
setFetchedUsers({
limit: 100,
limit: 8,
page: 1,
results: [],
totalPages: 0,
totalResults: 0,
});

debouncedFetchDiscordUsers(platformId, 1, 100);
debouncedFetchDiscordUsers(platformId, 1, 8);
} else {
debouncedFetchDiscordUsers(platformId, 1, 100, inputValue);
debouncedFetchDiscordUsers(platformId, 1, 8, inputValue);
}
};

Expand Down Expand Up @@ -149,10 +160,22 @@ function TcUsersAutoComplete({
getOptionLabel={(option) => option.ngu}
label={'Select User(s)'}
multiple={true}
loading={isLoading}
loadingText={
<div className="text-center">
<CircularProgress size={24} />
</div>
}
disabled={isDisabled}
value={selectedUsers}
onChange={handleChange}
onInputChange={handleSearchChange}
onInputChange={(event, value, reason) => {
if (reason === 'clear') {
handleClearAll();
} else {
handleSearchChange(event);
}
}}
isOptionEqualToValue={(option, value) =>
option.discordId === value.discordId
}
Expand Down
Loading