Skip to content

Commit

Permalink
Correctly login with different carreras
Browse files Browse the repository at this point in the history
  • Loading branch information
FdelMazo committed Mar 3, 2021
1 parent 5e3ebb8 commit 893cd3a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 51 deletions.
6 changes: 3 additions & 3 deletions src/components/Body.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable react-hooks/exhaustive-deps */
import { ChatIcon, CheckIcon, EmailIcon, Icon } from "@chakra-ui/icons";
import {
Box,
Expand All @@ -11,7 +12,7 @@ import {
Text,
Textarea,
Tooltip,
useToast
useToast,
} from "@chakra-ui/react";
import React, { useEffect } from "react";
import Graph from "react-graph-vis";
Expand All @@ -24,9 +25,9 @@ import useWindowSize from "./useWindowSize";
const Body = (props) => {
const {
graph,
redraw,
aprobar,
setNetwork,
redraw,
setNodes,
setEdges,
desaprobar,
Expand Down Expand Up @@ -87,7 +88,6 @@ const Body = (props) => {
duration: null,
isClosable: true,
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
Expand Down
7 changes: 4 additions & 3 deletions src/components/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import PadronInput from "./PadronInput";

const Header = (props) => {
const { displayedNode } = props;
const { changeCarrera } = React.useContext(GraphContext);
const { logged, user, register } = React.useContext(UserContext);
const { changeCarrera, setFirstTime } = React.useContext(GraphContext);
const { user } = React.useContext(UserContext);

return (
<Flex
Expand All @@ -26,12 +26,13 @@ const Header = (props) => {
<ScaleFade in={!displayedNode} unmountOnExit>
<Select
color="white"
key={user.carrera?.id}
css={{
"& *": { color: "initial" },
}}
onChange={(e) => {
setFirstTime(false);
changeCarrera(e.target.value);
if (logged) register();
}}
value={user.carrera?.id}
>
Expand Down
2 changes: 1 addition & 1 deletion src/components/PadronInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
PopoverBody,
PopoverContent,
PopoverTrigger,
Switch,
Switch
} from "@chakra-ui/react";
import React from "react";
import { GraphContext, UserContext } from "../Contexts";
Expand Down
17 changes: 10 additions & 7 deletions src/components/UserMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ import {
MenuItem,
MenuItemOption,
MenuList,
MenuOptionGroup
MenuOptionGroup,
} from "@chakra-ui/react";
import React from "react";
import { GraphContext, UserContext } from "../Contexts";

const UserMenu = (props) => {
const { register, logout, user } = React.useContext(UserContext);
const { changeOrientacion, changeFinDeCarrera, getNode } = React.useContext(
GraphContext
);
const { logout, user } = React.useContext(UserContext);
const {
setFirstTime,
changeOrientacion,
changeFinDeCarrera,
getNode,
} = React.useContext(GraphContext);

const OrientacionesMenuGroup = () => (
<MenuOptionGroup
onChange={(value) => {
setFirstTime(false);
changeOrientacion(value);
register();
}}
value={user.orientacion?.nombre || "none"}
title="Orientación"
Expand Down Expand Up @@ -58,8 +61,8 @@ const UserMenu = (props) => {
{user.carrera.finDeCarrera && (
<MenuOptionGroup
onChange={(value) => {
setFirstTime(false);
changeFinDeCarrera(value);
register();
}}
value={user.finDeCarrera?.id || "none"}
title="Fin de Carrera"
Expand Down
51 changes: 28 additions & 23 deletions src/useGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@ const useGraph = (loginHook) => {
const [shouldLoadGraph, setShouldLoadGraph] = React.useState(false);
const [autosave, setAutosave] = React.useState(false);
const [loadingGraph, setLoadingGraph] = React.useState(false);
const { user, setUser, logged, getGraph, postGraph } = loginHook;
const [firstTime, setFirstTime] = React.useState(true);
const { user, setUser, register, logged, getGraph, postGraph } = loginHook;

React.useEffect(() => {
if (!logged) changeCarrera(CARRERAS[0].id);
else {
changeCarrera(user.carrera.id);
}
}, [logged]);

React.useEffect(() => {
if (logged && !firstTime) register();
}, [user.carrera, user.orientacion, user.finDeCarrera]);

React.useEffect(() => {
if (!nodes?.carrera || nodes.carrera !== user.carrera?.id) return;
Expand All @@ -46,7 +58,7 @@ const useGraph = (loginHook) => {
setLoadingGraph(false);
});
}
}, [shouldLoadGraph, graph]);
}, [shouldLoadGraph, nodes]);

React.useEffect(() => {
setShowLabels(logged);
Expand All @@ -57,22 +69,26 @@ const useGraph = (loginHook) => {
conNota.forEach((n) => {
getNode(n.id).showLabel({ nodes, showLabel: logged });
});

if (logged) setShouldLoadGraph(true);
}, [logged]);

const saveGraph = () => {
postGraph(nodes, user.carrera.creditos.checkbox);
};
const changeCarrera = async (id) => {
setUser(({ ...rest }) => {
const carrera = CARRERAS.find((c) => c.id === id);

const userdata = user.allLogins.find((l) => l.carreraid === id);
const userdata = logged
? user.allLogins.find((l) => l.carreraid === id)
: false;
setShouldLoadGraph(!!userdata);
const orientacion = carrera.orientaciones?.find(
(c) => c.nombre === userdata?.orientacionid
);
const finDeCarrera = carrera.finDeCarrera?.find(
(c) => c.id === userdata?.findecarreraid
);
const orientacion =
carrera.orientaciones?.find(
(c) => c.nombre === userdata?.orientacionid
) || null;
const finDeCarrera =
carrera.finDeCarrera?.find((c) => c.id === userdata?.findecarreraid) ||
null;

const graphNodes = [];
const graphEdges = [];
Expand Down Expand Up @@ -100,10 +116,6 @@ const useGraph = (loginHook) => {
setUser({ ...user, finDeCarrera });
};

React.useEffect(() => {
if (user.carrera) changeCarrera(user.carrera.id);
}, [user.carrera]);

React.useEffect(() => {
if (!nodes?.carrera || nodes.carrera !== user.carrera?.id) return;
aprobar("CBC", 0);
Expand Down Expand Up @@ -355,10 +367,6 @@ const useGraph = (loginHook) => {
if (network) network.redraw();
};

React.useEffect(() => {
changeCarrera(CARRERAS[0].id);
}, []);

const toggleCheckbox = (c) => {
const value = !!user.carrera.creditos.checkbox.find(
(ch) => ch.nombre === c.nombre
Expand All @@ -368,10 +376,6 @@ const useGraph = (loginHook) => {
).check = !value;
};

const saveGraph = () => {
postGraph(nodes, user.carrera.creditos.checkbox);
};

return {
graph,
toggleGroup,
Expand All @@ -396,6 +400,7 @@ const useGraph = (loginHook) => {
toggleCheckbox,
actualizarMetadata,
loadingGraph,
setFirstTime,
isGroupHidden,
};
};
Expand Down
37 changes: 23 additions & 14 deletions src/useLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const useLogin = () => {
const logged = user.padron !== "";

React.useEffect(() => {
if (!logged && window.localStorage.getItem("padron")) {
if (window.localStorage.getItem("padron")) {
setPadronInput(window.localStorage.getItem("padron"));
login(window.localStorage.getItem("padron"));
}
}, [logged]);
}, []);

const login = async (padron) => {
setLoading(true);
Expand Down Expand Up @@ -145,9 +145,9 @@ const useLogin = () => {
return JSON.parse(map);
};

const register = (padronInput) => {
const register = async (p) => {
const formData = new FormData();
const padron = padronInput || user.padron;
const padron = p || user.padron;
const carreraid = user.carrera.id;
const orientacionid = user.orientacion?.nombre;
const findecarreraid = user.finDeCarrera?.id;
Expand Down Expand Up @@ -177,16 +177,25 @@ const useLogin = () => {
return newAllLogins;
};

setUser({
padron,
carrera,
orientacion: carrera.orientaciones?.find(
(c) => c.nombre === orientacionid
),
finDeCarrera: carrera.finDeCarrera?.find((c) => c.id === findecarreraid),
allLogins: [...addToAllLogins()],
});
window.localStorage.setItem("padron", padron);
if (!logged) {
setUser({
padron,
carrera,
orientacion: carrera.orientaciones?.find(
(c) => c.nombre === orientacionid
),
finDeCarrera: carrera.finDeCarrera?.find(
(c) => c.id === findecarreraid
),
allLogins: [...addToAllLogins()],
});
window.localStorage.setItem("padron", padron);
} else {
setUser({
...user,
allLogins: [...addToAllLogins()],
});
}
};

const logout = () => {
Expand Down

0 comments on commit 893cd3a

Please sign in to comment.