Skip to content

Commit

Permalink
savebutton
Browse files Browse the repository at this point in the history
  • Loading branch information
tfloxolodeiro committed Nov 28, 2023
1 parent d08d81c commit 0e7f709
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions locales/en-us/creator.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"download": "Download",
"share": "Share challenge",
"shareUrl": "Share with url",
"save": "Save challenge",
"preview": "Challenge preview",
"previewShort": "Preview",
"keepEditing": "Keep editing",
Expand Down
1 change: 1 addition & 0 deletions locales/es-ar/creator.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 18 additions & 4 deletions src/components/creator/Editor/ActionButtons/ShareButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {

Expand All @@ -20,11 +22,12 @@ export const ShareButton = () => {
}

const ShareDialog = ({open, setDialogOpen} : {open: boolean, setDialogOpen: (open: boolean) => void}) => {

const [url, setUrl] = useState<any>()
const { setShareId } = useContext(CreatorContext)
const [url, setUrl] = useState<string>()

const handleShareClick = async () => {
const challengeId = (await shareChallenge())._id
const challengeId: string = (await shareChallenge())._id
setShareId(challengeId)

setUrl(`https://${window.location.hostname}/#/sharedChallenge/${challengeId}`)
}
Expand All @@ -45,12 +48,23 @@ const ShareDialog = ({open, setDialogOpen} : {open: boolean, setDialogOpen: (ope
const Buttons = ({handleShareClick}: {handleShareClick: () => void}) => <>
<Stack direction="row" justifyContent="space-between">
<CreatorActionButton onClick={handleShareClick} startIcon={<ShareIcon/>} variant='contained' nametag="shareUrl"/>
<SaveButton/>
<DownloadButton/>
</Stack>


</>

const SaveButton = () => {
const { shareId } = useContext(CreatorContext)

const handleClick = async () => {
PilasBloquesApi.saveChallenge(LocalStorage.getCreatorChallenge()!)
}

return <CreatorActionButton disabled={!!!!!shareId} onClick={handleClick} startIcon={<SaveIcon/>} variant='contained' nametag="save"/>
}

const shareChallenge = () => {
const challenge: SerializedChallenge = LocalStorage.getCreatorChallenge()!

Expand Down
11 changes: 8 additions & 3 deletions src/components/creator/Editor/CreatorContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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<CreatorContextType>(defaultCreatorContext);
Expand All @@ -39,6 +43,7 @@ export const CreatorContextProvider: React.FC<CreatorProviderProps> = ({ 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]

Expand All @@ -53,7 +58,7 @@ export const CreatorContextProvider: React.FC<CreatorProviderProps> = ({ childre
}, [maps, challenge])

return (
<CreatorContext.Provider value={{ selectedTool, setSelectedTool, currentMap, setCurrentMap, index, setIndex, setMaps, maps}}>
<CreatorContext.Provider value={{ selectedTool, setSelectedTool, currentMap, setCurrentMap, shareId, setShareId, index, setIndex, setMaps, maps}}>
{children}
</CreatorContext.Provider>
);
Expand Down
4 changes: 4 additions & 0 deletions src/pbApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export namespace PilasBloquesApi{
return await _send('GET', `sharedChallenge/${id}`)
}

export const saveChallenge = async (challenge: SerializedChallenge) => {
await _send<SerializedChallenge>('PUT', `share/${challenge.shareId}`, challenge)
}

export const baseURL = window.PBRuntime?.apiURL || process.env.REACT_APP_API_URL

async function _send<T>(method: HttpMethod, resource: string, body?: T) {
Expand Down

0 comments on commit 0e7f709

Please sign in to comment.