From ee6c193c97e29d1c41a0886b380f54d75bb3120c Mon Sep 17 00:00:00 2001 From: realtradam Date: Fri, 7 Jun 2024 00:17:30 -0400 Subject: add tags --- src/components/Games.tsx | 75 ------------------------------------------- src/components/UploadGame.tsx | 44 +++++++++++++++++++++++-- src/main.tsx | 20 +++++++----- 3 files changed, 54 insertions(+), 85 deletions(-) (limited to 'src') diff --git a/src/components/Games.tsx b/src/components/Games.tsx index cfa6925..4ca8def 100644 --- a/src/components/Games.tsx +++ b/src/components/Games.tsx @@ -8,7 +8,6 @@ import { GameType } from "../types"; export default function Games () { const [games, setGames] = useState([]); - // old method useEffect(() => { const url = `${import.meta.env.VITE_API_TITLE}/api/v1/games`; fetch(url).then((response) => { @@ -23,88 +22,14 @@ export default function Games () { )); - const handleSubmit = (e: FormEvent) => { - e.preventDefault(); //stops submit from happening - - const target = e.target as typeof e.target & { - title: { value: string }; - img_rendering: { value: string }; - //game_files: { files: FileList }; - zip: { value: string, files: FileList }; - card_img: { value: string, files: FileList }; - char_img: { value: string, files: FileList }; - title_img: { value: string, files: FileList }; - }; - - //const form = e.target; - const formData = new FormData(); - formData.append('game[title]', target.title.value); - formData.append('game[img_rendering]', target.img_rendering.value); - //for(let i =0; i < target.game_files.files.length; i++) - //{ - //formData.append('game[game_files][]', target.game_files.files[i], target.game_files.files[i].name); - //} - formData.append('game[zip]', target.zip.files[0], target.zip.value); - formData.append('game[card_img]', target.card_img.files[0], target.card_img.value); - formData.append('game[char_img]', target.char_img.files[0], target.char_img.value); - formData.append('game[title_img]', target.title_img.files[0], target.title_img.value); - - //for (var pair of formData.entries()) { - // //console.log(pair[0] + ', ' + pair[1]) - //}; - - fetch(`${import.meta.env.VITE_API_TITLE}/api/v1/games`, { - method: 'post', - body: formData, - }); - }; return( <>
Games
-
-
-
- - -
-
- - -
- { /*
- - -
*/ } -
- -
-
- - -
-
- - -
-
- - -
-
- }/> -
- -
-
-
{ allGames } diff --git a/src/components/UploadGame.tsx b/src/components/UploadGame.tsx index 4801654..690834d 100644 --- a/src/components/UploadGame.tsx +++ b/src/components/UploadGame.tsx @@ -1,10 +1,35 @@ -import { useState, FormEvent } from "react"; +import { useState, useEffect, FormEvent } from "react"; import Button from "./Button"; import ReactMarkdown from 'react-markdown'; +import { TagPicker } from 'rsuite'; +type platformTag = { + label: string + value: string +}; + +type platformTagRaw = { + name: string +}; export default function UploadGame () { const [markdownInput, setMarkdownInput] = useState(""); + const [platformTags, setPlatformTags] = useState([]); + const [platformTagsData, setPlatformTagsData] = useState([]); + + useEffect(() => { + const url = `${import.meta.env.VITE_API_TITLE}/api/v1/tags?tag_type=platform`; + fetch(url).then((response) => { + if (response.ok) { + return response.json(); + } + throw new Error("Network response was not ok."); + }).then((response) => setPlatformTags( + response.map( + (item: platformTagRaw) => ({ label: item.name, value: item.name }) + ) + )); //.catch(() => navigate("/")); + }, []); const handleSubmit = (e: FormEvent) => { e.preventDefault(); //stops submit from happening @@ -34,6 +59,13 @@ export default function UploadGame () { formData.append('game[title_img]', target.title_img.files[0], target.title_img.value); formData.append('game[github_link]', target.github_link.value); formData.append('game[status]', `${target.status.value}`); + console.log("before the for loop"); + for(let i = 0; i < platformTagsData.length; i++) + { + console.log(platformTagsData[i]); + formData.append('game[platform_tag][]', platformTagsData[i]); + } + console.log("after the for loop"); fetch(`${import.meta.env.VITE_API_TITLE}/api/v1/games`, { credentials: 'include', @@ -61,7 +93,7 @@ export default function UploadGame () { className="min-h-32 grow p-4" name="description" value={markdownInput} - onChange={(e) => setMarkdownInput(e.target.value)} + onChange={(data) => setMarkdownInput(data.target.value)} />
@@ -77,6 +109,14 @@ export default function UploadGame () {
+ + setPlatformTagsData(items)} + /> +
+