浏览代码

Add functionality for creating a new game

Lee Morgan 3 周之前
父节点
当前提交
efa08976d7
共有 2 个文件被更改,包括 28 次插入8 次删除
  1. 27 2
      src/routes/(app)/game/new/+page.svelte
  2. 1 6
      vite.config.js

+ 27 - 2
src/routes/(app)/game/new/+page.svelte

@@ -1,9 +1,34 @@
 <script>
+    import {PUBLIC_API_URL} from "$env/static/public";
+    import {toast} from "$lib/toast.svelte.js";
+    import {goto} from "$app/navigation";
+
     let title = $state("");
     let gameContext = $state("");
 
     const submit = async ()=>{
-        console.log("submit");
+        const body = {
+            title: title,
+            game_context: gameContext
+        };
+
+        const response = await fetch(`${PUBLIC_API_URL}/game`, {
+            method: "POST",
+            headers: {"Content-Type": "application/json"},
+            credentials: "include",
+            body: JSON.stringify(body)
+        });
+
+        const data = await response.json();
+        if(!response.ok){
+            toast(data.error.message, "error");
+            if(response.status === 401){
+                goto("/");
+            }
+        }else{
+            toast("Game created");
+            goto("/game");
+        }
     }
 
     const autofocus = (node)=>{
@@ -24,7 +49,7 @@
     <main>
         <div class="heading">
             <h1>New Adventure</h1>
-            <p>Set the stage for your story. You can always change these later.</p>
+            <p>Set the stage for your story</p>
         </div>
 
         <form class="standardForm" onsubmit={submit}>

+ 1 - 6
vite.config.js

@@ -9,12 +9,7 @@ export default defineConfig({
 				// Force runes mode for the project, except for libraries. Can be removed in svelte 6.
 				runes: ({ filename }) =>
 					filename.split(/[/\\]/).includes('node_modules') ? undefined : true
-			},
-
-			// adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list.
-			// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
-			// See https://svelte.dev/docs/kit/adapters for more information about adapters.
-			adapter: adapter()
+			}
 		})
 	],
     server: {