Skip to content

Commit

Permalink
Merge pull request #76 from LemmyWorld/main
Browse files Browse the repository at this point in the history
Improvement: Added Select Variables
  • Loading branch information
tgxn authored Oct 31, 2023
2 parents f09e917 + c65b420 commit 80a1d88
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 36 deletions.
6 changes: 4 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import Reports from "./pages/Reports";
import Login from "./pages/Login";

import AppStore from "./store";
import { selectIsInElectron } from "./reducers/configReducer";
import { selectCurrentUser } from "./reducers/accountReducer";

function PageRouter() {
const theme = useTheme();

const currentUser = useSelector((state) => state.accountReducer.currentUser);
const isInElectron = useSelector((state) => state.configReducer.isInElectron);
const currentUser = useSelector(selectCurrentUser);
const isInElectron = useSelector(selectIsInElectron);

// when we get a jwt, set the cookie in electron
React.useEffect(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/components/Actions/CommentButtons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import {
ConfirmDialog,
} from "./BaseElements.jsx";
import { getSiteData } from "../../hooks/getSiteData";
import { selectShowResolved } from "../../reducers/configReducer.js";

export const ResolveCommentReportButton = ({ report, ...props }) => {
const queryClient = useQueryClient();

const showResolved = useSelector((state) => state.configReducer.showResolved);
const showResolved = useSelector(selectShowResolved);
const { baseUrl, siteData, localPerson, userRole } = getSiteData();

const { data, callAction, isSuccess, isLoading, error } = useLemmyHttpAction("resolveCommentReport");
Expand Down
3 changes: 2 additions & 1 deletion src/components/Actions/PMButtons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import {
ConfirmDialog,
} from "./BaseElements.jsx";
import { getSiteData } from "../../hooks/getSiteData";
import { selectShowResolved } from "../../reducers/configReducer.js";

// allow resolving / unresolving a post report
// resolvePrivateMessageReport
export const ResolvePMReportButton = ({ report, ...props }) => {
const queryClient = useQueryClient();

const showResolved = useSelector((state) => state.configReducer.showResolved);
const showResolved = useSelector(selectShowResolved);
const { baseUrl, siteData, localPerson, userRole } = getSiteData();

const { data, callAction, isSuccess, isLoading, error } = useLemmyHttpAction("resolvePrivateMessageReport");
Expand Down
3 changes: 2 additions & 1 deletion src/components/Actions/PostButtons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import {
ConfirmDialog,
} from "./BaseElements.jsx";
import { getSiteData } from "../../hooks/getSiteData";
import { selectShowResolved } from "../../reducers/configReducer.js";

// allow resolving / unresolving a post report
export const ResolvePostReportButton = ({ report, ...props }) => {
const queryClient = useQueryClient();

const showResolved = useSelector((state) => state.configReducer.showResolved);
const showResolved = useSelector(selectShowResolved);
const { baseUrl, siteData, localPerson, userRole } = getSiteData();

const { data, callAction, isSuccess, isLoading, error } = useLemmyHttpAction("resolvePostReport");
Expand Down
5 changes: 3 additions & 2 deletions src/components/Actions/RegistrationButtons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useLemmyHttpAction } from "../../hooks/useLemmyHttp.js";
import { getSiteData } from "../../hooks/getSiteData";

import { BaseActionButton, InputElement, ConfirmDialog } from "./BaseElements.jsx";
import { selectHideReadApprovals } from "../../reducers/configReducer.js";

export const ApproveButton = ({ registration, ...props }) => {
const queryClient = useQueryClient();
Expand All @@ -24,7 +25,7 @@ export const ApproveButton = ({ registration, ...props }) => {
"approveRegistrationApplication",
);

const hideReadApprovals = useSelector((state) => state.configReducer.hideReadApprovals);
const hideReadApprovals = useSelector(selectHideReadApprovals);

const [isConfirming, setIsConfirming] = React.useState(false);

Expand Down Expand Up @@ -172,7 +173,7 @@ export const DenyButton = ({ registration, ...props }) => {
const [confirmOpen, setConfirmOpen] = React.useState(false);
const [denyReason, setDenyReason] = React.useState("");

const hideReadApprovals = useSelector((state) => state.configReducer.hideReadApprovals);
const hideReadApprovals = useSelector(selectHideReadApprovals);

React.useEffect(() => {
if (isSuccess) {
Expand Down
14 changes: 7 additions & 7 deletions src/components/Filters.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import Checkbox from "@mui/joy/Checkbox";

import Chip from "@mui/joy/Chip";

import { setConfigItem } from "../reducers/configReducer";
import { selectFilterCommunity, selectFilterType, selectHideReadApprovals, selectModLogType, selectShowRemoved, selectShowResolved, setConfigItem } from "../reducers/configReducer";

import { getSiteData } from "../hooks/getSiteData";
import { getModLogTypeNames } from "../utils";

export function FilterCommunity() {
const dispatch = useDispatch();
const filterCommunity = useSelector((state) => state.configReducer.filterCommunity);
const filterCommunity = useSelector(selectFilterCommunity);

const { modCommms } = getSiteData();

Expand Down Expand Up @@ -84,7 +84,7 @@ export function FilterCommunity() {

export function FilterTypeSelect() {
const dispatch = useDispatch();
const filterType = useSelector((state) => state.configReducer.filterType);
const filterType = useSelector(selectFilterType);

return (
<Select
Expand Down Expand Up @@ -119,7 +119,7 @@ export function FilterTypeSelect() {

export function FilterModLogType() {
const dispatch = useDispatch();
const modLogType = useSelector((state) => state.configReducer.modLogType);
const modLogType = useSelector(selectModLogType);

const modlogTypes = getModLogTypeNames();

Expand Down Expand Up @@ -148,7 +148,7 @@ export function FilterModLogType() {

export function FilterResolved() {
const dispatch = useDispatch();
const showResolved = useSelector((state) => state.configReducer.showResolved);
const showResolved = useSelector(selectShowResolved);

return (
<Checkbox
Expand All @@ -164,7 +164,7 @@ export function FilterResolved() {

export function HideRead() {
const dispatch = useDispatch();
const hideReadApprovals = useSelector((state) => state.configReducer.hideReadApprovals);
const hideReadApprovals = useSelector(selectHideReadApprovals);

return (
<Checkbox
Expand All @@ -180,7 +180,7 @@ export function HideRead() {

export function FilterRemoved() {
const dispatch = useDispatch();
const showRemoved = useSelector((state) => state.configReducer.showRemoved);
const showRemoved = useSelector(selectShowRemoved);

return (
<Checkbox
Expand Down
4 changes: 2 additions & 2 deletions src/components/SiteHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import GitHubIcon from "@mui/icons-material/GitHub";
import FlagIcon from "@mui/icons-material/Flag";
import HowToRegIcon from "@mui/icons-material/HowToReg";

import { logoutCurrent } from "../reducers/accountReducer";
import { logoutCurrent, selectUsers } from "../reducers/accountReducer";

import { LemmyHttp } from "lemmy-js-client";

Expand Down Expand Up @@ -226,7 +226,7 @@ function UserMenu() {

const queryClient = useQueryClient();

const users = useSelector((state) => state.accountReducer.users);
const users = useSelector(selectUsers);

const { mutate: refreshMutate } = refreshAllData();

Expand Down
3 changes: 2 additions & 1 deletion src/hooks/getSiteData.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useMemo } from "react";

import { useSelector } from "react-redux";
import { selectCurrentUser } from "../reducers/accountReducer";

// accessor for the site data returned at the time of login
export function getSiteData() {
const currentUser = useSelector((state) => state.accountReducer.currentUser);
const currentUser = useSelector(selectCurrentUser);

const modCommms = useMemo(() => {
if (!currentUser) return [];
Expand Down
8 changes: 4 additions & 4 deletions src/hooks/useLemmyHttp.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { getSiteData } from "../hooks/getSiteData";

import { LemmyHttp } from "lemmy-js-client";

import { updateCurrentUserData } from "../reducers/accountReducer";
import { selectCurrentUser, updateCurrentUserData } from "../reducers/accountReducer";

export function useLemmyHttp(callLemmyMethod, formData) {
const currentUser = useSelector((state) => state.accountReducer.currentUser);
const currentUser = useSelector(selectCurrentUser);

const { baseUrl, siteData, localPerson, userRole } = getSiteData();

Expand Down Expand Up @@ -45,7 +45,7 @@ export function useLemmyHttp(callLemmyMethod, formData) {
}

export function useLemmyHttpAction(callLemmyMethod) {
const currentUser = useSelector((state) => state.accountReducer.currentUser);
const currentUser = useSelector(selectCurrentUser);

const mutation = useMutation({
mutationFn: async (formData) => {
Expand Down Expand Up @@ -77,7 +77,7 @@ export function refreshAllData() {
const dispatch = useDispatch();
const queryClient = useQueryClient();

const currentUser = useSelector((state) => state.accountReducer.currentUser);
const currentUser = useSelector(selectCurrentUser);

const mutation = useMutation({
mutationFn: async () => {
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useLemmyInfinite.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getSiteData } from "../hooks/getSiteData";
import { useSelector } from "react-redux";

import { LemmyHttp } from "lemmy-js-client";
import { selectCurrentUser } from "../reducers/accountReducer";

export default function useLemmyInfinite({
callLemmyMethod,
Expand All @@ -16,8 +17,8 @@ export default function useLemmyInfinite({
enabled = true,
perPage = 25,
}) {
const currentUser = useSelector((state) => state.accountReducer.currentUser);
// const showResolved = useSelector((state) => state.configReducer.showResolved);
const currentUser = useSelector(selectCurrentUser);
// const showResolved = useSelector(selectShowResolved);

const formDataArray = useMemo(() => {
const formDataArray = [];
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useLemmyModLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { LemmyHttp } from "lemmy-js-client";

import useLemmyInfinite from "./useLemmyInfinite";
import { getModLogTypeNames } from "../utils";
import { selectModLogCommunityId, selectModLogType } from "../reducers/configReducer";

// gets paginated / infinite list of reports from lemmy
export default function useLemmyModLog() {
const modLogType = useSelector((state) => state.configReducer.modLogType);
const modLogCommunityId = useSelector((state) => state.configReducer.modLogCommunityId);
const modLogType = useSelector(selectModLogType);
const modLogCommunityId = useSelector(selectModLogCommunityId);

const { baseUrl, siteData, localPerson, userRole } = getSiteData();

Expand Down
11 changes: 6 additions & 5 deletions src/hooks/useLemmyReports.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import { useSelector } from "react-redux";
import { LemmyHttp } from "lemmy-js-client";

import useLemmyInfinite from "./useLemmyInfinite";
import { selectFilterCommunity, selectFilterType, selectOrderBy, selectShowRemoved, selectShowResolved } from "../reducers/configReducer";

// gets paginated / infinite list of reports from lemmy
export default function useLemmyReports() {
const orderBy = useSelector((state) => state.configReducer.orderBy);
const filterType = useSelector((state) => state.configReducer.filterType);
const filterCommunity = useSelector((state) => state.configReducer.filterCommunity);
const showResolved = useSelector((state) => state.configReducer.showResolved);
// const showRemoved = useSelector((state) => state.configReducer.showRemoved);
const orderBy = useSelector(selectOrderBy);
const filterType = useSelector(selectFilterType);
const filterCommunity = useSelector(selectFilterCommunity);
const showResolved = useSelector(selectShowResolved);
// const showRemoved = useSelector(selectShowRemoved);

const { baseUrl, siteData, localPerson, userRole } = getSiteData();

Expand Down
3 changes: 2 additions & 1 deletion src/pages/Actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import { getSiteData } from "../hooks/getSiteData";
import { parseActorId } from "../utils";

import ModLogAccordians from "../components/Activity/ModLogAccordians";
import { selectModLogType } from "../reducers/configReducer";

export default function Actions() {
const { baseUrl, siteData, localPerson, userRole } = getSiteData();

const locaUserParsedActor = parseActorId(localPerson.actor_id);

const modLogType = useSelector((state) => state.configReducer.modLogType);
const modLogType = useSelector(selectModLogType);

const [limitLocalInstance, setLimitLocalInstance] = React.useState(true);
const [limitCommunityId, setLimitCommunityId] = React.useState(null);
Expand Down
3 changes: 2 additions & 1 deletion src/pages/Approvals.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import useLemmyInfinite from "../hooks/useLemmyInfinite";
import ApprovalsList from "../components/ApprovalsList.jsx";

import { getSiteData } from "../hooks/getSiteData";
import { selectHideReadApprovals } from "../reducers/configReducer";

export default function Approvals() {
const hideReadApprovals = useSelector((state) => state.configReducer.hideReadApprovals);
const hideReadApprovals = useSelector(selectHideReadApprovals);

const { baseUrl, siteData, localPerson, userRole } = getSiteData();

Expand Down
9 changes: 5 additions & 4 deletions src/pages/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@ import { LemmyHttp } from "lemmy-js-client";

import { useLemmyUserDataAction } from "../hooks/useLemmyHttp";

import { addUser, setAccountIsLoading, setUsers, setCurrentUser } from "../reducers/accountReducer";
import { addUser, setAccountIsLoading, setUsers, setCurrentUser, selectAccountIsLoading, selectUsers } from "../reducers/accountReducer";

import { BasicInfoTooltip } from "../components/Tooltip.jsx";
import { selectIsInElectron } from "../reducers/configReducer";

export default function LoginForm() {
const domainLock = window?.RuntimeConfig?.DomainLock != "" ? window?.RuntimeConfig?.DomainLock : false;
console.log("RuntimeConfig DomainLock", domainLock);

const dispatch = useDispatch();

const accountIsLoading = useSelector((state) => state.accountReducer.accountIsLoading);
const users = useSelector((state) => state.accountReducer.users);
const isInElectron = useSelector((state) => state.configReducer.isInElectron);
const accountIsLoading = useSelector(selectAccountIsLoading);
const users = useSelector(selectUsers);
const isInElectron = useSelector(selectIsInElectron);

// form state
const [instanceBase, setInstanceBase] = React.useState(domainLock ? domainLock : "");
Expand Down
4 changes: 4 additions & 0 deletions src/reducers/accountReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,7 @@ const accountReducer = (state = initialState, action = {}) => {
};

export default accountReducer;

export const selectCurrentUser = (state) => state.accountReducer.currentUser;
export const selectUsers = (state) => state.accountReducer.users;
export const selectAccountIsLoading = (state) => state.accountReducer.accountIsLoading;
11 changes: 11 additions & 0 deletions src/reducers/configReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,14 @@ const configReducer = (state = initialState, action = {}) => {
};

export default configReducer;


export const selectIsInElectron = (state) => state.configReducer.isInElectron;
export const selectFilterCommunity = (state) => state.configReducer.filterCommunity;
export const selectFilterType = (state) => state.configReducer.filterType;
export const selectModLogType = (state) => state.configReducer.modLogType;
export const selectShowResolved = (state) => state.configReducer.showResolved;
export const selectHideReadApprovals = (state) => state.configReducer.hideReadApprovals;
export const selectShowRemoved = (state) => state.configReducer.showRemoved;
export const selectModLogCommunityId = (state) => state.configReducer.modLogCommunityId;
export const selectOrderBy = (state) => state.configReducer.orderBy;

0 comments on commit 80a1d88

Please sign in to comment.