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

Modernize redux: userSlice #242

Merged
merged 15 commits into from
Feb 28, 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
35 changes: 0 additions & 35 deletions app/src/actions/userActions.ts

This file was deleted.

7 changes: 2 additions & 5 deletions app/src/components/shared/MainNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
} from "../../thunks/tableThunks";
import { fetchEvents } from "../../slices/eventSlice";
import { fetchRecordings } from "../../thunks/recordingThunks";
import { fetchUsers } from "../../thunks/userThunks";
import { fetchThemes } from "../../slices/themeSlice";
import { fetchFilters, fetchStats } from "../../thunks/tableFilterThunks";
import { setOffset } from "../../actions/tableActions";
Expand All @@ -28,6 +27,7 @@ import { GlobalHotKeys } from "react-hotkeys";
import { availableHotkeys } from "../../configs/hotkeysConfig";
import { fetchAcls } from "../../slices/aclSlice";
import { useAppDispatch } from "../../store";
import { fetchUsers } from "../../slices/userSlice";
import { fetchServers } from "../../slices/serverSlice";
import { fetchSeries } from "../../slices/seriesSlice";
import { fetchJobs } from "../../slices/jobSlice";
Expand Down Expand Up @@ -56,8 +56,6 @@ const MainNav = ({
loadingServersIntoTable,
// @ts-expect-error TS(7031): Binding element 'loadingServicesIntoTable' implici... Remove this comment to see the full error message
loadingServicesIntoTable,
// @ts-expect-error TS(7031): Binding element 'loadingUsers' implicitly has an '... Remove this comment to see the full error message
loadingUsers,
// @ts-expect-error TS(7031): Binding element 'loadingUsersIntoTable' implicitly... Remove this comment to see the full error message
loadingUsersIntoTable,
// @ts-expect-error TS(7031): Binding element 'loadingGroupsIntoTable' implicitl... Remove this comment to see the full error message
Expand Down Expand Up @@ -165,7 +163,7 @@ const MainNav = ({
resetOffset();

// Fetching users from server
loadingUsers();
dispatch(fetchUsers());

// Load users into table
loadingUsersIntoTable();
Expand Down Expand Up @@ -338,7 +336,6 @@ const mapDispatchToProps = (dispatch) => ({
loadingJobsIntoTable: () => dispatch(loadJobsIntoTable()),
loadingServersIntoTable: () => dispatch(loadServersIntoTable()),
loadingServicesIntoTable: () => dispatch(loadServicesIntoTable()),
loadingUsers: () => dispatch(fetchUsers()),
loadingUsersIntoTable: () => dispatch(loadUsersIntoTable()),
loadingGroupsIntoTable: () => dispatch(loadGroupsIntoTable()),
loadingAclsIntoTable: () => dispatch(loadAclsIntoTable()),
Expand Down
8 changes: 4 additions & 4 deletions app/src/components/users/Acls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Notifications from "../shared/Notifications";
import NewResourceModal from "../shared/NewResourceModal";
import { aclsTemplateMap } from "../../configs/tableConfigs/aclsTableMap";
import { fetchFilters } from "../../thunks/tableFilterThunks";
import { fetchUsers } from "../../thunks/userThunks";
import {
loadAclsIntoTable,
loadGroupsIntoTable,
Expand All @@ -26,6 +25,7 @@ import { getUserInformation } from "../../selectors/userInfoSelectors";
import { getCurrentFilterResource } from "../../selectors/tableFilterSelectors";
import { useAppDispatch, useAppSelector } from "../../store";
import { fetchAcls } from "../../slices/aclSlice";
import { fetchUsers } from "../../slices/userSlice";
import { fetchGroups } from "../../slices/groupSlice";

/**
Expand All @@ -36,10 +36,10 @@ const Acls: React.FC = () => {
const [displayNavigation, setNavigation] = useState(false);
const [displayNewAclModal, setNewAclModal] = useState(false);

const dispatch = useAppDispatch();
const acls = useAppSelector(state => getTotalAcls(state));
const dispatch = useAppDispatch();
const acls = useAppSelector(state => getTotalAcls(state));
const user = useAppSelector(state => getUserInformation(state));
const currentFilterType = useAppSelector(state => getCurrentFilterResource(state));
const currentFilterType = useAppSelector(state => getCurrentFilterResource(state));

const loadAcls = async () => {
// Fetching acls from server
Expand Down
7 changes: 2 additions & 5 deletions app/src/components/users/Groups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import NewResourceModal from "../shared/NewResourceModal";
import { getTotalGroups } from "../../selectors/groupSelectors";
import { groupsTemplateMap } from "../../configs/tableConfigs/groupsTableMap";
import { fetchFilters } from "../../thunks/tableFilterThunks";
import { fetchUsers } from "../../thunks/userThunks";
import {
loadAclsIntoTable,
loadGroupsIntoTable,
Expand All @@ -27,6 +26,7 @@ import { hasAccess } from "../../utils/utils";
import { getCurrentFilterResource } from "../../selectors/tableFilterSelectors";
import { fetchAcls } from "../../slices/aclSlice";
import { useAppDispatch, useAppSelector } from "../../store";
import { fetchUsers } from "../../slices/userSlice";
import { fetchGroups } from "../../slices/groupSlice";

/**
Expand All @@ -37,8 +37,6 @@ const Groups = ({
loadingGroupsIntoTable,
// @ts-expect-error TS(7031): Binding element 'loadingFilters' implicitly has an... Remove this comment to see the full error message
loadingFilters,
// @ts-expect-error TS(7031): Binding element 'loadingUsers' implicitly has an '... Remove this comment to see the full error message
loadingUsers,
// @ts-expect-error TS(7031): Binding element 'loadingUsersIntoTable' implicitly... Remove this comment to see the full error message
loadingUsersIntoTable,
// @ts-expect-error TS(7031): Binding element 'loadingAclsIntoTable' implicitly ... Remove this comment to see the full error message
Expand Down Expand Up @@ -77,7 +75,7 @@ const Groups = ({
resetOffset();

// Fetching users from server
loadingUsers();
dispatch(fetchUsers());

// Load users into table
loadingUsersIntoTable();
Expand Down Expand Up @@ -216,7 +214,6 @@ const mapDispatchToProps = (dispatch) => ({
// @ts-expect-error TS(7006): Parameter 'resource' implicitly has an 'any' type.
loadingFilters: (resource) => dispatch(fetchFilters(resource)),
loadingGroupsIntoTable: () => dispatch(loadGroupsIntoTable()),
loadingUsers: () => dispatch(fetchUsers()),
loadingUsersIntoTable: () => dispatch(loadUsersIntoTable()),
loadingAclsIntoTable: () => dispatch(loadAclsIntoTable()),
resetTextFilter: () => dispatch(editTextFilter("")),
Expand Down
82 changes: 24 additions & 58 deletions app/src/components/users/Users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import React, { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { Link } from "react-router-dom";
import cn from "classnames";
import { connect } from "react-redux";
import MainNav from "../shared/MainNav";
import TableFilters from "../shared/TableFilters";
import Table from "../shared/Table";
import Notifications from "../shared/Notifications";
import NewResourceModal from "../shared/NewResourceModal";
import { usersTemplateMap } from "../../configs/tableConfigs/usersTableMap";
import { getTotalUsers } from "../../selectors/userSelectors";
import { fetchUsers } from "../../thunks/userThunks";
import {
loadAclsIntoTable,
loadGroupsIntoTable,
Expand All @@ -26,75 +24,64 @@ import { getUserInformation } from "../../selectors/userInfoSelectors";
import { hasAccess } from "../../utils/utils";
import { getCurrentFilterResource } from "../../selectors/tableFilterSelectors";
import { fetchAcls } from "../../slices/aclSlice";
import { useAppDispatch } from "../../store";
import { useAppDispatch, useAppSelector } from "../../store";
import { fetchUsers } from "../../slices/userSlice";
import { fetchGroups } from "../../slices/groupSlice";

/**
* This component renders the table view of users
*/
const Users = ({
// @ts-expect-error TS(7031): Binding element 'loadingUsers' implicitly has an '... Remove this comment to see the full error message
loadingUsers,
// @ts-expect-error TS(7031): Binding element 'loadingUsersIntoTable' implicitly... Remove this comment to see the full error message
loadingUsersIntoTable,
// @ts-expect-error TS(7031): Binding element 'users' implicitly has an 'any' ty... Remove this comment to see the full error message
users,
// @ts-expect-error TS(7031): Binding element 'loadingFilters' implicitly has an... Remove this comment to see the full error message
loadingFilters,
// @ts-expect-error TS(7031): Binding element 'loadingGroupsIntoTable' implicitl... Remove this comment to see the full error message
loadingGroupsIntoTable,
// @ts-expect-error TS(7031): Binding element 'loadingAclsIntoTable' implicitly ... Remove this comment to see the full error message
loadingAclsIntoTable,
// @ts-expect-error TS(7031): Binding element 'resetTextFilter' implicitly has a... Remove this comment to see the full error message
resetTextFilter,
// @ts-expect-error TS(7031): Binding element 'resetOffset' implicitly has an 'a... Remove this comment to see the full error message
resetOffset,
// @ts-expect-error TS(7031): Binding element 'user' implicitly has an 'any' typ... Remove this comment to see the full error message
user,
// @ts-expect-error TS(7031): Binding element 'currentFilterType' implicitly has... Remove this comment to see the full error message
currentFilterType,
}) => {
const Users: React.FC = () => {
const { t } = useTranslation();
const dispatch = useAppDispatch();
const dispatch = useAppDispatch();
const [displayNavigation, setNavigation] = useState(false);
const [displayNewUserModal, setNewUserModal] = useState(false);

const users = useAppSelector(state => getTotalUsers(state));
const user = useAppSelector(state => getUserInformation(state));
const currentFilterType = useAppSelector(state => getCurrentFilterResource(state));

// TODO: Get rid of the wrappers when modernizing redux is done
const fetchUsersWrapper = () => {
dispatch(fetchUsers())
}

const loadUsers = async () => {
// Fetching users from server
await loadingUsers();
await dispatch(fetchUsers());

// Load users into table
loadingUsersIntoTable();
dispatch(loadUsersIntoTable());
};

const loadGroups = () => {
// Reset the current page to first page
resetOffset();
dispatch(setOffset(0));

// Fetching groups from server
dispatch(fetchGroups());

// Load groups into table
loadingGroupsIntoTable();
dispatch(loadGroupsIntoTable());
};

const loadAcls = () => {
// Reset the current page to first page
resetOffset();
dispatch(setOffset(0));

// Fetching acls from server
dispatch(fetchAcls());

// Load acls into table
loadingAclsIntoTable();
dispatch(loadAclsIntoTable());
};

useEffect(() => {
if ("users" !== currentFilterType) {
loadingFilters("users");
dispatch(fetchFilters("users"));
}

resetTextFilter();
dispatch(editTextFilter(""));

// Load users on mount
loadUsers().then((r) => console.info(r));
Expand Down Expand Up @@ -183,8 +170,8 @@ const Users = ({
<div className="controls-container">
{/* Include filters component */}
<TableFilters
loadResource={loadingUsers}
loadResourceIntoTable={loadingUsersIntoTable}
loadResource={fetchUsersWrapper}
loadResourceIntoTable={loadUsersIntoTable}
resource={"users"}
/>
<h1>{t("USERS.USERS.TABLE.CAPTION")}</h1>
Expand All @@ -198,25 +185,4 @@ const Users = ({
);
};

// Getting state data out of redux store
// @ts-expect-error TS(7006): Parameter 'state' implicitly has an 'any' type.
const mapStateToProps = (state) => ({
users: getTotalUsers(state),
user: getUserInformation(state),
currentFilterType: getCurrentFilterResource(state),
});

// Mapping actions to dispatch
// @ts-expect-error TS(7006): Parameter 'dispatch' implicitly has an 'any' type.
const mapDispatchToProps = (dispatch) => ({
// @ts-expect-error TS(7006): Parameter 'resource' implicitly has an 'any' type.
loadingFilters: (resource) => dispatch(fetchFilters(resource)),
loadingUsers: () => dispatch(fetchUsers()),
loadingUsersIntoTable: () => dispatch(loadUsersIntoTable()),
loadingGroupsIntoTable: () => dispatch(loadGroupsIntoTable()),
loadingAclsIntoTable: () => dispatch(loadAclsIntoTable()),
resetTextFilter: () => dispatch(editTextFilter("")),
resetOffset: () => dispatch(setOffset(0)),
});

export default connect(mapStateToProps, mapDispatchToProps)(Users);
export default Users;
7 changes: 2 additions & 5 deletions app/src/components/users/partials/UsersActionsCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import ConfirmModal from "../../shared/ConfirmModal";
import { deleteUser } from "../../../thunks/userThunks";
import UserDetailsModal from "./modal/UserDetailsModal";
import { getUserInformation } from "../../../selectors/userInfoSelectors";
import { hasAccess } from "../../../utils/utils";
import { deleteUser } from "../../../slices/userSlice";
import { useAppDispatch } from "../../../store";
import { fetchUserDetails } from "../../../slices/userDetailsSlice";

Expand All @@ -14,7 +14,6 @@ import { fetchUserDetails } from "../../../slices/userDetailsSlice";
*/
const UsersActionCell = ({
row,
deleteUser,
user
}: any) => {
const { t } = useTranslation();
Expand All @@ -29,7 +28,7 @@ const UsersActionCell = ({

// @ts-expect-error TS(7006): Parameter 'id' implicitly has an 'any' type.
const deletingUser = (id) => {
deleteUser(id);
dispatch(deleteUser(id));
};

const showUserDetails = async () => {
Expand Down Expand Up @@ -92,8 +91,6 @@ const mapStateToProps = (state) => ({
// Mapping actions to dispatch
// @ts-expect-error TS(7006): Parameter 'dispatch' implicitly has an 'any' type.
const mapDispatchToProps = (dispatch) => ({
// @ts-expect-error TS(7006): Parameter 'id' implicitly has an 'any' type.
deleteUser: (id) => dispatch(deleteUser(id)),
});

export default connect(mapStateToProps, mapDispatchToProps)(UsersActionCell);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from "react";
import SelectContainer from "../../../shared/wizard/SelectContainer";
import WizardNavigationButtons from "../../../shared/wizard/WizardNavigationButtons";
import { fetchUsersAndUsernames } from "../../../../thunks/userThunks";
import { fetchUsersAndUsernames } from "../../../../slices/userSlice";

/**
* This component renders the user selection page of the new group wizard and group details wizard
Expand Down
9 changes: 4 additions & 5 deletions app/src/components/users/partials/wizard/NewUserWizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import NewUserGeneralTab from "./NewUserGeneralTab";
import UserRolesTab from "./UserRolesTab";
import { initialFormValuesNewUser } from "../../../../configs/modalConfig";
import { getUsernames } from "../../../../selectors/userSelectors";
import { postNewUser } from "../../../../thunks/userThunks";
import { NewUserSchema } from "../../../../utils/validate";
import { postNewUser } from "../../../../slices/userSlice";
import { useAppDispatch } from "../../../../store";

/**
* This component renders the new user wizard
*/
const NewUserWizard = ({
close,
usernames,
postNewUser
}: any) => {
const { t } = useTranslation();
const dispatch = useAppDispatch();

const navStyle = {
left: "0px",
Expand All @@ -35,7 +36,7 @@ const NewUserWizard = ({

// @ts-expect-error TS(7006): Parameter 'values' implicitly has an 'any' type.
const handleSubmit = (values) => {
const response = postNewUser(values);
const response = dispatch(postNewUser(values));
console.info(response);
close();
};
Expand Down Expand Up @@ -111,8 +112,6 @@ const mapStateToProps = (state) => ({
// Mapping actions to dispatch
// @ts-expect-error TS(7006): Parameter 'dispatch' implicitly has an 'any' type.
const mapDispatchToProps = (dispatch) => ({
// @ts-expect-error TS(7006): Parameter 'values' implicitly has an 'any' type.
postNewUser: (values) => dispatch(postNewUser(values)),
});

export default connect(mapStateToProps, mapDispatchToProps)(NewUserWizard);
Loading
Loading