-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'url-share' of github.com:Program-AR/pilas-bloques-react…
… into url-share
- Loading branch information
Showing
6 changed files
with
189 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 0 additions & 101 deletions
101
src/components/creator/Editor/ActionButtons/ShareButton.tsx
This file was deleted.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
src/components/creator/Editor/ActionButtons/ShareChallenge/ShareButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { CreatorActionButton } from "../CreatorActionButton"; | ||
import DownloadIcon from '@mui/icons-material/Download'; | ||
import { useContext, useState } from "react"; | ||
import { Dialog, DialogContent, DialogTitle, InputAdornment, Stack, TextField } from "@mui/material"; | ||
import { CreatorContext } from "../../CreatorContext"; | ||
import { Buttons, CopyToClipboardButton } from "./ShareModalButtons"; | ||
import { useTranslation } from "react-i18next"; | ||
|
||
export const ShareButton = () => { | ||
|
||
const [dialogOpen, setDialogOpen] = useState<boolean>(false) | ||
|
||
return <> | ||
<ShareDialog open={dialogOpen} setDialogOpen={setDialogOpen} /> | ||
<CreatorActionButton onClick={() => { setDialogOpen(true) }} startIcon={<DownloadIcon />} nametag='share' isshortversion={true} /> | ||
</> | ||
|
||
} | ||
|
||
const ShareDialog = ({ open, setDialogOpen }: { open: boolean, setDialogOpen: (open: boolean) => void }) => { | ||
|
||
const { t } = useTranslation('creator'); | ||
|
||
return <> | ||
<Dialog open={open} onClose={() => { setDialogOpen(false) }}> | ||
<DialogTitle>{t('editor.buttons.share')}</DialogTitle> | ||
<DialogContent > | ||
<ShareModal /> | ||
</DialogContent> | ||
</Dialog > | ||
</> | ||
} | ||
|
||
const ShareModal = () => { | ||
const { shareId } = useContext(CreatorContext) | ||
|
||
const APP_URL = 'https://pilasbloques.program.ar/online' | ||
const DEV_URL = 'localhost:3000' | ||
|
||
const sharedLink = (process.env.NODE_ENV === 'production' ? APP_URL : DEV_URL) + `/#/desafio/guardado/${shareId}` | ||
|
||
//const link: string = `http://localhost:3000/#/desafio/guardado/${shareId}` | ||
|
||
return <Stack> | ||
{shareId ? | ||
<Stack direction='row'> | ||
<TextField | ||
sx={{ width: '100%', margin: 1}} | ||
defaultValue={sharedLink} | ||
InputProps={{ | ||
readOnly: true, | ||
endAdornment: ( | ||
<InputAdornment position="end"> | ||
<CopyToClipboardButton textToCopy={sharedLink} /> | ||
</InputAdornment> | ||
) | ||
}} | ||
/> | ||
</Stack> | ||
: <></> | ||
} | ||
<Buttons /> | ||
</Stack> | ||
} | ||
|
||
|
||
|
||
|
106 changes: 106 additions & 0 deletions
106
src/components/creator/Editor/ActionButtons/ShareChallenge/ShareModalButtons.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import ShareIcon from '@mui/icons-material/Share'; | ||
import SaveIcon from '@mui/icons-material/Save'; | ||
import ContentCopyIcon from '@mui/icons-material/ContentCopy'; | ||
import { ReactNode, useContext, useState } from "react" | ||
import { IconButtonTooltip } from "../../SceneEdition/IconButtonTooltip" | ||
import { Snackbar, Stack, Tooltip } from "@mui/material" | ||
import { CreatorContext } from '../../CreatorContext'; | ||
import { LocalStorage } from '../../../../../localStorage'; | ||
import { PilasBloquesApi } from '../../../../../pbApi'; | ||
import { CreatorActionButton } from '../CreatorActionButton'; | ||
import { DialogSnackbar } from '../../../../dialogSnackbar/DialogSnackbar'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { SerializedChallenge } from '../../../../serializedChallenge'; | ||
import { DownloadButton } from '../DownloadButton'; | ||
|
||
export const CopyToClipboardButton = ({ textToCopy }: { textToCopy: string }) => { | ||
|
||
const [openSnackbar, setOpenSnackbar] = useState(false) | ||
|
||
const { t } = useTranslation('creator'); | ||
|
||
const handleClick = () => { | ||
setOpenSnackbar(true) | ||
navigator.clipboard.writeText(textToCopy) | ||
} | ||
|
||
return <> | ||
<IconButtonTooltip icon={<ContentCopyIcon />} onClick={handleClick} tooltip={t('editor.buttons.copyToClipboard')} /> | ||
<Snackbar | ||
open={openSnackbar} | ||
onClose={() => setOpenSnackbar(false)} | ||
autoHideDuration={2000} | ||
message={t('editor.buttons.copiedToClipboard')} | ||
/> | ||
</> | ||
} | ||
|
||
export const Buttons = () => { | ||
const { shareId } = useContext(CreatorContext) | ||
|
||
return <> | ||
<Stack direction="row" justifyContent="space-between" alignItems='center'> | ||
{shareId ? <SaveButton /> : <ShareUrlButton />} | ||
<DownloadButton /> | ||
</Stack> | ||
</> | ||
} | ||
|
||
const ShareUrlButton = () => { | ||
|
||
const shareChallenge = async (): Promise<string> => { | ||
const challenge: SerializedChallenge = LocalStorage.getCreatorChallenge()! | ||
const sharedChallenge = await PilasBloquesApi.shareChallenge(challenge) | ||
|
||
return sharedChallenge.sharedId | ||
} | ||
|
||
return <ChallengeUpsertButton Icon={<ShareIcon />} nametag="shareUrl" challengeUpsert={shareChallenge} /> | ||
} | ||
const SaveButton = () => { | ||
const [openSnackbar, setOpenSnackbar] = useState(false) | ||
|
||
const { t } = useTranslation('creator'); | ||
|
||
const saveChallenge = async (): Promise<string> => { | ||
const savedChallenge = await PilasBloquesApi.saveChallenge(LocalStorage.getCreatorChallenge()!) | ||
setOpenSnackbar(true) | ||
return savedChallenge.sharedId | ||
} | ||
|
||
return <> | ||
<ChallengeUpsertButton Icon={<SaveIcon />} nametag="save" challengeUpsert={saveChallenge} /> | ||
<Snackbar | ||
open={openSnackbar} | ||
onClose={() => setOpenSnackbar(false)} | ||
autoHideDuration={2000} | ||
message={t('editor.buttons.savedCorrectly')} | ||
/> | ||
</> | ||
} | ||
|
||
const ChallengeUpsertButton = ({ Icon, challengeUpsert, nametag }: { Icon: ReactNode, nametag: string, challengeUpsert: () => Promise<string> }) => { | ||
|
||
const { setShareId } = useContext(CreatorContext) | ||
const userLoggedIn = !!LocalStorage.getUser() | ||
const [serverError, setServerError] = useState<boolean>(false) | ||
const { t } = useTranslation('creator'); | ||
|
||
const handleClick = async () => { | ||
try { | ||
setShareId(await challengeUpsert()) | ||
} | ||
catch (error) { | ||
setServerError(true) | ||
} | ||
} | ||
|
||
return <> | ||
<Tooltip title={!userLoggedIn ? t('editor.loginWarning') : ''} followCursor> | ||
<div> | ||
<CreatorActionButton onClick={handleClick} disabled={!userLoggedIn} startIcon={Icon} variant='contained' nametag={nametag} /> | ||
</div> | ||
</Tooltip> | ||
<DialogSnackbar open={serverError} onClose={() => setServerError(false)} message={t('editor.serverError')} /> | ||
</> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters