From 0e7f709e4de2d8806b68984a6d929668ddfd8973 Mon Sep 17 00:00:00 2001 From: Tomas Floxo Date: Tue, 28 Nov 2023 14:49:10 -0300 Subject: [PATCH] savebutton --- locales/en-us/creator.json | 1 + locales/es-ar/creator.json | 1 + .../Editor/ActionButtons/ShareButton.tsx | 22 +++++++++++++++---- .../creator/Editor/CreatorContext.tsx | 11 +++++++--- src/pbApi.ts | 4 ++++ 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/locales/en-us/creator.json b/locales/en-us/creator.json index e2411f05..60e1f24a 100644 --- a/locales/en-us/creator.json +++ b/locales/en-us/creator.json @@ -98,6 +98,7 @@ "download": "Download", "share": "Share challenge", "shareUrl": "Share with url", + "save": "Save challenge", "preview": "Challenge preview", "previewShort": "Preview", "keepEditing": "Keep editing", diff --git a/locales/es-ar/creator.json b/locales/es-ar/creator.json index 3825b41b..fb04bd6d 100644 --- a/locales/es-ar/creator.json +++ b/locales/es-ar/creator.json @@ -98,6 +98,7 @@ "download": "Descargar", "share": "Compartir desafío", "shareUrl": "Compartir por url", + "save": "Guardar desafío", "preview": "Ver desafío", "previewShort": "Ver", "keepEditing": "Seguir editando", diff --git a/src/components/creator/Editor/ActionButtons/ShareButton.tsx b/src/components/creator/Editor/ActionButtons/ShareButton.tsx index 74118923..5118a9a4 100644 --- a/src/components/creator/Editor/ActionButtons/ShareButton.tsx +++ b/src/components/creator/Editor/ActionButtons/ShareButton.tsx @@ -3,10 +3,12 @@ import { LocalStorage } from "../../../../localStorage"; import { CreatorActionButton } from "./CreatorActionButton"; import DownloadIcon from '@mui/icons-material/Download'; import ShareIcon from '@mui/icons-material/Share'; +import SaveIcon from '@mui/icons-material/Save'; import { PilasBloquesApi } from "../../../../pbApi"; -import { useState } from "react"; +import { useContext, useState } from "react"; import { Dialog, DialogContent, DialogTitle, Stack } from "@mui/material"; import { DownloadButton } from "./DownloadButton"; +import { CreatorContext } from "../CreatorContext"; export const ShareButton = () => { @@ -20,11 +22,12 @@ export const ShareButton = () => { } const ShareDialog = ({open, setDialogOpen} : {open: boolean, setDialogOpen: (open: boolean) => void}) => { - - const [url, setUrl] = useState() + const { setShareId } = useContext(CreatorContext) + const [url, setUrl] = useState() const handleShareClick = async () => { - const challengeId = (await shareChallenge())._id + const challengeId: string = (await shareChallenge())._id + setShareId(challengeId) setUrl(`https://${window.location.hostname}/#/sharedChallenge/${challengeId}`) } @@ -45,12 +48,23 @@ const ShareDialog = ({open, setDialogOpen} : {open: boolean, setDialogOpen: (ope const Buttons = ({handleShareClick}: {handleShareClick: () => void}) => <> } variant='contained' nametag="shareUrl"/> + +const SaveButton = () => { + const { shareId } = useContext(CreatorContext) + + const handleClick = async () => { + PilasBloquesApi.saveChallenge(LocalStorage.getCreatorChallenge()!) + } + + return } variant='contained' nametag="save"/> +} + const shareChallenge = () => { const challenge: SerializedChallenge = LocalStorage.getCreatorChallenge()! diff --git a/src/components/creator/Editor/CreatorContext.tsx b/src/components/creator/Editor/CreatorContext.tsx index d30c9ad5..cbdfbf07 100644 --- a/src/components/creator/Editor/CreatorContext.tsx +++ b/src/components/creator/Editor/CreatorContext.tsx @@ -11,7 +11,9 @@ export type CreatorContextType = { index: number; setIndex: (index: number) => void; setMaps: (maps: any) => void; - maps: SceneMap[] + maps: SceneMap[]; + shareId: string + setShareId: (id: string) => void }; const defaultCreatorContext = { @@ -22,7 +24,9 @@ const defaultCreatorContext = { index: 0, setIndex: () => { }, setMaps: () => { }, - maps: defaultChallenge('Duba').scene.maps + maps: defaultChallenge('Duba').scene.maps, + shareId: "", + setShareId: (id: string) => {} } export const CreatorContext = React.createContext(defaultCreatorContext); @@ -39,6 +43,7 @@ export const CreatorContextProvider: React.FC = ({ childre const challenge = LocalStorage.getCreatorChallenge() || defaultChallenge("Duba") const [maps, setMaps] = useState(challenge.scene.maps) const [index, setIndex] = useState(defaultIndex) + const [shareId, setShareId] = useState(challenge.shareId || "") const currentMap = maps[index] || challenge.scene.maps[index] @@ -53,7 +58,7 @@ export const CreatorContextProvider: React.FC = ({ childre }, [maps, challenge]) return ( - + {children} ); diff --git a/src/pbApi.ts b/src/pbApi.ts index a4360c69..0c642a2b 100644 --- a/src/pbApi.ts +++ b/src/pbApi.ts @@ -45,6 +45,10 @@ export namespace PilasBloquesApi{ return await _send('GET', `sharedChallenge/${id}`) } + export const saveChallenge = async (challenge: SerializedChallenge) => { + await _send('PUT', `share/${challenge.shareId}`, challenge) + } + export const baseURL = window.PBRuntime?.apiURL || process.env.REACT_APP_API_URL async function _send(method: HttpMethod, resource: string, body?: T) {