From 05b62d19ce4aed2b5f97eed231c71f6a418f30f1 Mon Sep 17 00:00:00 2001 From: Sofia Egan <108308587+EerierGosling@users.noreply.github.com> Date: Wed, 9 Oct 2024 22:34:40 -0400 Subject: [PATCH] Making the new game button always create a new game (#2310) * making progress on fixing * game saves on login * removing debug stuff * removing more debug stuff * finished css-ing * improved redirect to editor from 'make game' button * Update Maze-escape.js * Update Maze-escape.js * Update memory_game.js * fixing error + adding comment --- .../big-interactive-pages/login.tsx | 27 +- .../project-creation-not-logged-in.tsx | 35 + .../popups-etc/project-name-creation.tsx | 5 +- .../project-name-creator.module.css | 25 +- src/pages/index.astro | 23 +- src/pages/~/new-game.astro | 3 +- src/pages/~/new.ts | 4 +- yarn.lock | 2808 +++++++---------- 8 files changed, 1303 insertions(+), 1627 deletions(-) create mode 100644 src/components/popups-etc/project-creation-not-logged-in.tsx diff --git a/src/components/big-interactive-pages/login.tsx b/src/components/big-interactive-pages/login.tsx index 86d6bbfa2c..38218cb45d 100644 --- a/src/components/big-interactive-pages/login.tsx +++ b/src/components/big-interactive-pages/login.tsx @@ -1,7 +1,8 @@ import { useSignalEffect } from '@preact/signals' import { IoPaperPlaneOutline } from 'react-icons/io5' import { SessionInfo } from '../../lib/game-saving/account' -import {DevEmail, useAuthHelper} from '../../lib/game-saving/auth-helper' +import { DevEmail, useAuthHelper } from '../../lib/game-saving/auth-helper' +import { defaultExampleCode } from "../../lib/examples"; import Button from '../design-system/button' import Input from '../design-system/input' import LinkButton from '../design-system/link-button' @@ -16,8 +17,30 @@ interface LoginProps { export default function Login({ session, email, to }: LoginProps) { const auth = useAuthHelper('EMAIL_ENTRY', email) + useSignalEffect(() => { - if (auth.stage.value === 'LOGGED_IN') window.location.replace(to) + const handleLogin = async () => { + if (auth.stage.value === 'LOGGED_IN') { + // This code saves the user's unsaved work in the editor as a game on their account when they log in + const savedGame = localStorage.getItem("sprigMemory") + if (savedGame && savedGame !== defaultExampleCode) { + try { + await fetch('/api/games/new', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + code: savedGame + }) + }) + localStorage.setItem("sprigMemory", defaultExampleCode) + } catch (error) { + console.error('Failed to save game:', error) + } + } + window.location.replace(to) + } + } + handleLogin() }) return ( diff --git a/src/components/popups-etc/project-creation-not-logged-in.tsx b/src/components/popups-etc/project-creation-not-logged-in.tsx new file mode 100644 index 0000000000..8c9359afbb --- /dev/null +++ b/src/components/popups-etc/project-creation-not-logged-in.tsx @@ -0,0 +1,35 @@ +import styles from "./project-name-creator.module.css"; +import Button from "../design-system/button"; +import LinkButton from '../design-system/link-button'; +import { defaultExampleCode } from "../../lib/examples"; + +export default function ProjectNameCreatorNotLoggedIn() { + + return ( +
You already have a game stored, but it hasn't been saved to an account. You can either log in to save it and create a new game, or open your existing game.
+ + +Explore 430+ games built by other teenagers,
or make your own and we'll send you a console!