Skip to content

Commit

Permalink
Merge pull request #111 from tgxn/fix_reducers
Browse files Browse the repository at this point in the history
reducer fixes
  • Loading branch information
tgxn authored Nov 7, 2023
2 parents fdc350b + b68f20c commit 80c0401
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 68 deletions.
3 changes: 1 addition & 2 deletions src/components/Header/ConfigModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import Divider from "@mui/joy/Divider";
import {
selectMandatoryModComment,
setConfigItem,
setConfigItemJson,
selectBlurNsfw,
selectShowAvatars,
selectNsfwWords,
Expand Down Expand Up @@ -133,7 +132,7 @@ export default function ConfigModal({ open, onClose }) {
label="NSFW Words List"
subtext="list of words to also mark as NSFW"
value={nsfwWords}
onChange={(e) => dispatch(setConfigItemJson("nsfwWords", e))}
onChange={(e) => dispatch(setConfigItem("nsfwWords", e))}
/>
)}
</Box>
Expand Down
1 change: 0 additions & 1 deletion src/components/Shared/ActorMeta.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { PersonMetaChips } from "./UserChips.jsx";

import {
setConfigItem,
setConfigItemJson,
selectBlurNsfw,
selectShowAvatars,
selectNsfwWords,
Expand Down
1 change: 0 additions & 1 deletion src/components/Tooltip.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { PersonMetaChips } from "./Shared/UserChips.jsx";

import {
setConfigItem,
setConfigItemJson,
selectBlurNsfw,
selectShowAvatars,
selectNsfwWords,
Expand Down
7 changes: 5 additions & 2 deletions src/hooks/useLemmyHttp.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { getSiteData } from "../hooks/getSiteData";

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

import { selectCurrentUser, updateCurrentUserData, setAccountIsLoading } from "../redux/reducer/accountReducer";
import {
selectCurrentUser,
updateCurrentUserData,
setAccountIsLoading,
} from "../redux/reducer/accountReducer";

export function useLemmyHttp(callLemmyMethod, formData = {}) {
const currentUser = useSelector(selectCurrentUser);
Expand All @@ -21,7 +25,6 @@ export function useLemmyHttp(callLemmyMethod, formData = {}) {
return formDataArray;
}, [formData]);


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

const { isSuccess, isLoading, isError, error, data, isFetching, refetch } = useQuery({
Expand Down
32 changes: 9 additions & 23 deletions src/pages/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ export default function LoginForm() {
auth: auth.jwt,
});

// save if they chose to
if (saveSession) {
dispatch(addUser({instance: instanceBase, jwt: auth.jwt, site: getSite}));
} else {
dispatch(setCurrentUser({base: instanceBase, jwt: auth.jwt, site: getSite}));
dispatch(addUser({ base: instanceBase, jwt: auth.jwt, site: getSite }));
}

// always set the current user on login
dispatch(setCurrentUser({ base: instanceBase, jwt: auth.jwt, site: getSite }));
} else {
setLoginError(auth);
}
Expand Down Expand Up @@ -311,8 +313,6 @@ export default function LoginForm() {
variant="outlined"
color="neutral"
onClick={async () => {
// setIsLoading(true);

dispatch(setAccountIsLoading(true));

try {
Expand All @@ -322,36 +322,22 @@ export default function LoginForm() {
auth: user.jwt,
});

if (!getSite.my_user) {
// set instance base to the current instance
if (!getSite || !getSite.my_user) {
// set instance base to the current instance for easy login
setInstanceBase(user.base);
setUsername(user.site.my_user.local_user_view?.person.name);

throw new Error("jwt does not provide auth, re-authenticate");
}

// if (saveSession) {
// dispatch(addUser(user.base, auth.jwt, getSite));
// } else {
// dispatch(setCurrentUser(user.base, auth.jwt, getSite));
dispatch(setCurrentUser({base: instanceBase, jwt: user.jwt, site: getSite}));
// }
// session is already saved
dispatch(setCurrentUser({ base: user.base, jwt: user.jwt, site: getSite }));
} catch (e) {
setLoginError(typeof e == "string" ? e : e.message);
} finally {
// setIsLoading(false);
dispatch(setAccountIsLoading(false));
}

// if (!expired) {

// dispatch(setCurrentUser(user.base, user.jwt, user.site));
// } else {
// // set the form values
// setInstanceBase(jwt.iss);
// setUsername(user.site.my_user?.local_user_view?.person.name);
// setPassword("");
// }
}}
>
<ListItemContent
Expand Down
34 changes: 18 additions & 16 deletions src/redux/reducer/accountReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ const initialState = {
users: storedUsers ? JSON.parse(storedUsers) : [], // { base, jwt, site }
currentUser: storedCurrentUser ? JSON.parse(storedCurrentUser) : null, // { base, jwt, site }
};

const accountReducer = createSlice({
name: "accountReducer",
initialState,
reducers: {
setAccountIsLoading: (state, action) => {
state.accountIsLoading = action.payload.isLoading;
state.accountIsLoading = action.payload;
},
setUsers: (state, action) => {
if (action.payload) {
localStorage.setItem("users", JSON.stringify(action.payload.users));
localStorage.setItem("users", JSON.stringify(action.payload));
}

return {
...state,
users: action.payload.users,
users: action.payload,
};
},
addUser: (state, action) => {
Expand All @@ -37,7 +37,6 @@ const accountReducer = createSlice({
action.payload.site.my_user.local_user_view.person.name
),
);

// add new, save to local storage
const newUsers = [...cleanUsers, action.payload];
if (action.payload == null) {
Expand All @@ -53,7 +52,7 @@ const accountReducer = createSlice({
};
},
setCurrentUser: (state, action) => {
console.log(action)
console.log("setCurrentUser", action);
if (action.payload === null) {
localStorage.removeItem("currentUser");
} else {
Expand All @@ -67,34 +66,37 @@ const accountReducer = createSlice({
updateCurrentUserData: (state, action) => {
const newCurrentUser = {
...state.currentUser,
site: action.payload.site,
site: action.payload,
};
localStorage.setItem("currentUser", JSON.stringify(newCurrentUser));
return {
...state,
currentUser: newCurrentUser,
};
},
logoutCurrent: (state, action) => {
logoutCurrent: (state) => {
localStorage.removeItem("currentUser");
return {
...state,
currentUser: null,
};
},
}
})
},
});

export default accountReducer.reducer;

export const { setAccountIsLoading, addUser, updateCurrentUserData, logoutCurrent, setCurrentUser, setUsers } = accountReducer.actions;
export const {
setAccountIsLoading,
addUser,
updateCurrentUserData,
logoutCurrent,
setCurrentUser,
setUsers,
} = accountReducer.actions;

export const selectIsLoading = (state) => state.accountReducer.isLoading;
export const selectAccountIsLoading = (state) => state.accountReducer.accountIsLoading;

export const selectCurrentUser = (state) => {
console.log(state)
return state.accountReducer.currentUser

};
export const selectCurrentUser = (state) => state.accountReducer.currentUser;
export const selectUsers = (state) => state.accountReducer.users;
51 changes: 28 additions & 23 deletions src/redux/reducer/configReducer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createSlice } from "@reduxjs/toolkit";

function loadWithDefault(key, defaultValue = null, parseJson = true) {
// load json stored values from localStorage with a default
function loadWithDefault(key, defaultValue = null) {
const storedValue = localStorage.getItem(key);

if (storedValue) {
Expand Down Expand Up @@ -43,36 +44,40 @@ const initialState = {
nsfwWords: loadWithDefault("config.nsfwWords", []),
};


const configReducer = createSlice({
name: "configReducer",
initialState,
reducers: {
setConfigItem: (state, action) => {
const newConfig = {
...state,
[action.payload.configKey]: action.payload.configValue,
};
localStorage.setItem(`config.${action.payload.configKey}`, action.payload.configValue);
return newConfig;
},
setConfigItemJson: (state, action) => {
const newConfigJson = {
...state,
[action.payload.configKey]: action.payload.configValue,
};
localStorage.setItem(`config.${action.payload.configKey}`, JSON.stringify(action.payload.configValue));
return newConfigJson;
setConfigItem: {
reducer(state, action) {
console.log("setConfigItem", action.payload);

const newConfig = {
...state,
[action.payload.configKey]: action.payload.configValue,
};
localStorage.setItem(
`config.${action.payload.configKey}`,
JSON.stringify(action.payload.configValue),
);
return newConfig;
},
// extract the arguments from the action
prepare(...args) {
return {
payload: {
configKey: args[0],
configValue: args[1],
},
};
},
},

}
})


},
});

export default configReducer.reducer;

export const { setConfigItem, setConfigItemJson } = configReducer.actions;
export const { setConfigItem } = configReducer.actions;

export const selectIsInElectron = (state) => state.configReducer.isInElectron;
export const selectFilterCommunity = (state) => state.configReducer.filterCommunity;
Expand Down

0 comments on commit 80c0401

Please sign in to comment.