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

move user table params to the recoil state #519

Merged
merged 3 commits into from
Jul 26, 2022
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
21 changes: 20 additions & 1 deletion packages/frontend/src/components/form/SearchInput.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { faMagnifyingGlass } from '@fortawesome/free-solid-svg-icons';
import {
faMagnifyingGlass,
faCircleXmark,
} from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import React from 'react';

interface SearchInputProps {
handleChange: (element) => void;
value?: string;
placeholder?: string;
handleClear?: (element) => void;
}

export const SearchInput = ({
handleChange,
value,
placeholder = 'Search',
handleClear,
}: SearchInputProps): JSX.Element => {
return (
<div className="relative flex items-center border border-warm-gray-400 h-10">
Expand All @@ -33,6 +39,19 @@ export const SearchInput = ({
handleChange(e.target.value)
}
/>
{value !== '' && handleClear && (
<div className="absolute inset-y-0 right-6 flex items-center pl-3">
<button onClick={handleClear}>
<span className="text-warm-gray-800 dark:text-white">
<FontAwesomeIcon
icon={faCircleXmark}
size="lg"
className="absolute transform -translate-y-1/2 top-1/2 left-3"
/>
</span>
</button>
</div>
)}
</div>
);
};
37 changes: 37 additions & 0 deletions packages/frontend/src/model/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ import {
import { ActiveTokenSet } from './auth';
import { AllPeriods } from './periods';

interface roleOptionsProps {
value: string;
label: string;
}

export const roleOptions = [
{ label: 'All users', value: UserRole.USER },
{ label: 'Admins', value: UserRole.ADMIN },
{ label: 'Forwarders', value: UserRole.FORWARDER },
{ label: 'Quantifiers', value: UserRole.QUANTIFIER },
];

const AllUsersQuery = selector({
key: 'AllUsersQuery',
get: ({ get }) => {
Expand All @@ -39,6 +51,31 @@ export const AllUsers = atom<UserDto[] | undefined>({
default: undefined,
});

export const UsersTableData = atom<UserDto[] | undefined>({
key: 'UsersTableData',
default: undefined,
});

export const UsersTableSelectedRole = atom<roleOptionsProps>({
key: 'UsersTableSelectedRole',
default: roleOptions[0],
});

export const UsersTableFilter = atom<string>({
key: 'UsersTableFilter',
default: '',
});

export const UsersTablePage = atom<number>({
key: 'UsersTablePage',
default: 1,
});

export const UsersTableLastPage = atom<number>({
key: 'UsersTableLastPage',
default: 0,
});

export const AllAdminUsers = selector({
key: 'AllAdminUsers',
get: ({ get }) => {
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/src/pages/EventLogs/EventLogsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const EventLogsPage = (): JSX.Element => {
setPage(1);
}}
value={searchValue}
handleClear={(): void => setSearchValue('')}
/>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ export const QuantifyTable = ({
periodId,
receiverId,
}: Props): JSX.Element | null => {
const [searchValue, setSearchValue] = React.useState<string | undefined>(
undefined
);
const [searchValue, setSearchValue] = React.useState<string>('');

const data = useRecoilValue(
PeriodQuantifierReceiverPraise({ periodId, receiverId })
Expand Down Expand Up @@ -207,6 +205,8 @@ export const QuantifyTable = ({
<SearchInput
handleChange={(e): void => handleSearchInput(e)}
placeholder="Filter"
value={searchValue}
handleClear={(): void => handleSearchInput('')}
/>
</div>
</div>
Expand Down
51 changes: 27 additions & 24 deletions packages/frontend/src/pages/Users/components/UsersTable.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
import React from 'react';
import { useRecoilValue } from 'recoil';
import { useRecoilValue, useRecoilState } from 'recoil';
import { UserDto, UserRole } from 'api/dist/user/types';
import {
AllAdminUsers,
AllForwarderUsers,
AllQuantifierUsers,
AllUsers,
UsersTableData,
UsersTableSelectedRole,
UsersTableFilter,
UsersTablePage,
UsersTableLastPage,
roleOptions,
} from '@/model/users';
import { SearchInput } from '@/components/form/SearchInput';
import { SelectInput } from '@/components/form/SelectInput';
import { UsersTableRow } from './UsersTableRow';
import { UsersTablePagination } from './UsersTablePagination';

interface roleOptionsProps {
value: string;
label: string;
}

const roleOptions = [
{ label: 'All users', value: UserRole.USER },
{ label: 'Admins', value: UserRole.ADMIN },
{ label: 'Forwarders', value: UserRole.FORWARDER },
{ label: 'Quantifiers', value: UserRole.QUANTIFIER },
];

const USERS_PER_PAGE = 10;

export const UsersTable = (): JSX.Element => {
const allAdminUsers = useRecoilValue(AllAdminUsers);
const allForwarderUsers = useRecoilValue(AllForwarderUsers);
const allQuantifierUsers = useRecoilValue(AllQuantifierUsers);
const allUsers = useRecoilValue(AllUsers);
const [tableData, setTableData] = React.useState<UserDto[]>();
const [selectedRole, setSelectedRole] = React.useState<roleOptionsProps>(
roleOptions[0]

const [tableData, setTableData] = useRecoilState(UsersTableData);
const [selectedRole, setSelectedRole] = useRecoilState(
UsersTableSelectedRole
);
const [filter, setFilter] = React.useState<string>('');
const [page, setPage] = React.useState<number>(1);
const [lastPage, setLastPage] = React.useState<number>(0);
const [filter, setFilter] = useRecoilState(UsersTableFilter);
const [page, setPage] = useRecoilState(UsersTablePage);
const [lastPage, setLastPage] = useRecoilState(UsersTableLastPage);

const applyFilter = React.useCallback(
(data: UserDto[] | undefined): UserDto[] => {
Expand All @@ -62,7 +57,7 @@ export const UsersTable = (): JSX.Element => {
if (allUsers) {
setTableData(allUsers);
}
}, [allUsers]);
}, [allUsers, setTableData]);

React.useEffect(() => {
switch (selectedRole.value) {
Expand All @@ -79,18 +74,22 @@ export const UsersTable = (): JSX.Element => {
setTableData(allQuantifierUsers);
break;
}
setFilter('');
}, [
selectedRole,
allUsers,
allAdminUsers,
allForwarderUsers,
allQuantifierUsers,
setTableData,
setFilter,
]);

React.useEffect(() => {
if (tableData) {
setPage(1);
if (page > lastPage) {
setPage(1);
}

const filteredData = applyFilter(tableData);

if (filteredData.length % USERS_PER_PAGE === 0) {
Expand All @@ -99,7 +98,7 @@ export const UsersTable = (): JSX.Element => {
setLastPage(Math.trunc(filteredData.length / USERS_PER_PAGE) + 1);
}
}
}, [tableData, filter, applyFilter]);
}, [tableData, filter, applyFilter, setLastPage, page, lastPage, setPage]);

return (
<>
Expand All @@ -109,7 +108,11 @@ export const UsersTable = (): JSX.Element => {
selected={selectedRole}
options={roleOptions}
/>
<SearchInput handleChange={setFilter} value={filter} />
<SearchInput
handleChange={setFilter}
value={filter}
handleClear={(): void => setFilter('')}
/>
</div>

<div className="flex justify-between px-5 mb-2">
Expand Down