Skip to content

Commit

Permalink
fix: clunky login in kids app #17
Browse files Browse the repository at this point in the history
Signed-off-by: Jeet Viramgama <[email protected]>
  • Loading branch information
jv18creator committed Jul 3, 2024
1 parent c99b83d commit 1f8af4a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
12 changes: 1 addition & 11 deletions src/frontend_kids/contexts/ChildContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ export default function ChildProvider({ children }) {
useCheckIsUserNewToTransactions({ handleUpdateCalloutState });
useCheckIsUserNewToSwipeActions({ handleUpdateCalloutState });

const handleUpdateChild = (...args) => {
setChild((prevState) => ({ ...prevState, ...args?.[0] }));
};

async function getBalance(childID) {
return new Promise((resolve, reject) => {
get("balance-" + childID, store)
Expand Down Expand Up @@ -147,11 +143,7 @@ export default function ChildProvider({ children }) {
const balance = actor
?.getBalance(child.id)
.then(async (returnedBalance) => {
setChild({
id: child.id,
balance: parseInt(returnedBalance),
name: child.name,
});
setChild((prevState) => ({ ...prevState, balance: parseInt(returnedBalance) }));
});

promises.push(balance);
Expand Down Expand Up @@ -268,7 +260,6 @@ export default function ChildProvider({ children }) {
blockingChildUpdate,
setTransactions,
transactions,
handleUpdateChild,
setTasks,
tasks,
setRewards,
Expand All @@ -289,7 +280,6 @@ export default function ChildProvider({ children }) {
setBlockingChildUpdate,
setTransactions,
transactions,
handleUpdateChild,
setTasks,
tasks,
setRewards,
Expand Down
12 changes: 7 additions & 5 deletions src/frontend_kids/screens/LoggedOut.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ function checkForIOS() {

function LoggedOut() {
const { login, isAuthenticated, isLoading, logout, store, actor } = useAuth();
const { getBalance, handleUpdateChild } = React.useContext(ChildContext);
const { getBalance, child, setChild } = React.useContext(ChildContext);
const [code, setCode] = useState(null);
const [error, setError] = useState("");
const clearContextState = useClearContextState();
const [checkingCode, setCheckingCode] = useState(false)

const [isAuthenticatedWithChildData, setIsAuthenticatedWithChildData] = useState(!!(isAuthenticated && child?.id))

React.useEffect(() => {
if (!isLoading && !isAuthenticated) {
logout();
Expand All @@ -60,7 +62,6 @@ function LoggedOut() {
}
setCheckingCode(true)
const data = await login(code);
setCheckingCode(false)

if (!data) {
setError("Incorrect or expired magic code.");
Expand All @@ -75,11 +76,12 @@ function LoggedOut() {
const balance = await getBalance(data);
const name = await actor.getChild(data);
set("selectedChildName", name, store)

handleUpdateChild({ id: data, name: name, balance: parseInt(balance) })
setChild({ name: name, id: data, balance: parseInt(balance) })
setCheckingCode(false)
setIsAuthenticatedWithChildData(true)
};

if (!isLoading && isAuthenticated) {
if (!isLoading && isAuthenticatedWithChildData) {
return <Navigate to="/" replace />;
}

Expand Down

0 comments on commit 1f8af4a

Please sign in to comment.