diff --git a/src/Components/ColCardPublicOptions.js b/src/Components/ColCardPublicOptions.js index 48a7bab2be1d264fb8681bb3e79637c68d08894f..6e374d7a159e18c51bc06a44038c0aa859c796d6 100644 --- a/src/Components/ColCardPublicOptions.js +++ b/src/Components/ColCardPublicOptions.js @@ -16,23 +16,23 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with Plataforma Integrada MEC. If not, see <http://www.gnu.org/licenses/>.*/ -import React, {useState} from 'react'; +import React, { useState } from 'react'; import Button from '@material-ui/core/Button'; import Menu from '@material-ui/core/Menu'; import ListItemIcon from '@material-ui/core/ListItemIcon'; import MenuItem from '@material-ui/core/MenuItem'; import OpenIcon from '@material-ui/icons/OpenInNew'; -import {Link} from 'react-router-dom' +import { Link } from 'react-router-dom' import MoreVertIcon from '@material-ui/icons/MoreVert'; import styled from 'styled-components' import ErrorIcon from '@material-ui/icons/Error'; import ReportModal from './ReportModal.js' import ReportProblemIcon from '@material-ui/icons/ReportProblem'; import OpenInBrowserIcon from '@material-ui/icons/OpenInBrowser'; -import {deleteRequest} from '../Components/HelperFunctions/getAxiosConfig' +import { deleteRequest } from '../Components/HelperFunctions/getAxiosConfig' -export default function ColCardPublicOptions (props) { - const [anchorEl, setAnchorEl] = React.useState(null); +export default function ColCardPublicOptions(props) { + const [anchorEl, setAnchorEl] = React.useState(null); function handleClick(event) { setAnchorEl(event.currentTarget); @@ -43,11 +43,13 @@ export default function ColCardPublicOptions (props) { } const [reportModalOpen, toggleReportModal] = useState(false) - const handleReportModal = (value) => {toggleReportModal(value)} + const handleReportModal = (value) => { toggleReportModal(value) } const handleUnfollow = () => { - - deleteRequest(`/collections/${props.id}/follow`, (data) => {console.log(data)}, (error) => {console.log(error)}) + if (props.currentUserId) + deleteRequest(`/collections/${props.id}/follow`, (data) => { console.log(data) }, (error) => { console.log(error) }) + else + props.handleLogin() } return ( @@ -104,7 +106,10 @@ export default function ColCardPublicOptions (props) { <StyledMenuItem onClick={() => { - handleReportModal(true); + if (props.currentUserId) + handleReportModal(true); + else + props.handleLogin() }} > <ListItemIcon> diff --git a/src/Components/CollectionCardFunction.js b/src/Components/CollectionCardFunction.js index fe0844f15ae24d38025de34d359c9cd30ba07463..ced5cb77028a259d4aa39136ff48f18f696ec114 100644 --- a/src/Components/CollectionCardFunction.js +++ b/src/Components/CollectionCardFunction.js @@ -42,263 +42,271 @@ import Snackbar from '@material-ui/core/Snackbar'; import MuiAlert from '@material-ui/lab/Alert'; export default function CollectionCardFunction(props) { - const { state } = useContext(Store) - - // eslint-disable-next-line - const [userAvatar] = useState(props.avatar ? (`${apiDomain}` + props.avatar) : noAvatar) - const [userFollowingCol, toggleUserFollowingCol] = useState(props.followed ? props.followed : false) - const handleToggleUserFollowingCol = () => { toggleUserFollowingCol(!userFollowingCol) } - - const [name, setName] = useState(props.name) - const changeColName = (newName) => {setName(newName)} - - const [privacy, setPrivacy] = useState(props.privacy) - const changeColPrivacy = (newPrivacy) => {setPrivacy(newPrivacy)} - - const [likesCount, setLikesCount] = useState(props.likeCount) - const [liked, toggleLiked] = useState(props.liked) - - const [signUpOpen, setSignUp] = useState(false) - const [loginOpen, setLogin] = useState(false) - const [successfulLoginOpen, handleSuccessfulLogin] = useState(false) - - - function handleSuccessLike(data) { - toggleLiked(!liked) - setLikesCount(data.count) - } - const handleLike = () => { - putRequest(`/collections/${props.id}/like`, {}, handleSuccessLike, (error) => { console.log(error) }) - } - - const [followingHover, handleFollowingHover] = useState(false) - const toggleFollowingHover = (value) => { handleFollowingHover(value) } - - const [slideIn, setSlide] = useState(false) - const controlSlide = () => { setSlide(!slideIn) } - - function handleSuccessFollow(data) { - handleToggleUserFollowingCol() - } - const handleFollow = () => { - putRequest(`/collections/${props.id}/follow`, {}, handleSuccessFollow, (error) => { console.log(error) }) - } + const { state } = useContext(Store) + + // eslint-disable-next-line + const [userAvatar] = useState(props.avatar ? (`${apiDomain}` + props.avatar) : noAvatar) + const [userFollowingCol, toggleUserFollowingCol] = useState(props.followed ? props.followed : false) + const handleToggleUserFollowingCol = () => { toggleUserFollowingCol(!userFollowingCol) } + + const [name, setName] = useState(props.name) + const changeColName = (newName) => { setName(newName) } + + const [privacy, setPrivacy] = useState(props.privacy) + const changeColPrivacy = (newPrivacy) => { setPrivacy(newPrivacy) } + + const [likesCount, setLikesCount] = useState(props.likeCount) + const [liked, toggleLiked] = useState(props.liked) + + const [signUpOpen, setSignUp] = useState(false) + const [loginOpen, setLogin] = useState(false) + const [successfulLoginOpen, handleSuccessfulLogin] = useState(false) + + + function handleSuccessLike(data) { + toggleLiked(!liked) + setLikesCount(data.count) + } + const handleLike = () => { + if (state.currentUser.id) + putRequest(`/collections/${props.id}/like`, {}, handleSuccessLike, (error) => { console.log(error) }) + else + handleLogin() + } + + const [followingHover, handleFollowingHover] = useState(false) + const toggleFollowingHover = (value) => { handleFollowingHover(value) } + + const [slideIn, setSlide] = useState(false) + const controlSlide = () => { setSlide(!slideIn) } + + function handleSuccessFollow(data) { + handleToggleUserFollowingCol() + } + const handleFollow = () => { + if (state.currentUser.id) + putRequest(`/collections/${props.id}/follow`, {}, handleSuccessFollow, (error) => { console.log(error) }) + else + handleLogin() + } + + const RenderFollowButton = () => { + return ( + <FollowButton onClick={handleFollow}> + <AddIcon /><span>SEGUIR</span> + </FollowButton> + ) + } - const RenderFollowButton = () => { - return ( - <FollowButton onClick={handleFollow}> - <AddIcon /><span>SEGUIR</span> - </FollowButton> - ) + useEffect(() => { + if (!state.currentUser.id) { + toggleLiked(false); + toggleUserFollowingCol(false); } + }, [state.currentUser.id]) - useEffect(() => { - if (!state.currentUser.id) { - toggleLiked(false); - toggleUserFollowingCol(false); + const RenderFollowingButton = () => { + return ( + <FollowingButton onMouseOver={() => toggleFollowingHover(true)} + onMouseLeave={() => toggleFollowingHover(false)} onClick={handleFollow}> + { + followingHover ? + ( + [ + <span>DEIXAR DE SEGUIR</span> + ] + ) + : + ( + [ + <React.Fragment> + <CheckIcon /><span>SEGUINDO</span> + </React.Fragment> + ] + ) } - }, [state.currentUser.id]) - - const RenderFollowingButton = () => { - return ( - <FollowingButton onMouseOver={() => toggleFollowingHover(true)} - onMouseLeave={() => toggleFollowingHover(false)} onClick={handleFollow}> - { - followingHover ? - ( - [ - <span>DEIXAR DE SEGUIR</span> - ] - ) - : - ( - [ - <React.Fragment> - <CheckIcon /><span>SEGUINDO</span> - </React.Fragment> - ] - ) - } - </FollowingButton> - ) - } + </FollowingButton> + ) + } - const SlideAnimationContent = () => { - return ( - <SlideContentDiv> - <HeaderContainer container="row" justify="flex-start" alignItems="center"> - <AvatarDiv item xs={2}> - <img className="img" src={userAvatar} alt="user avatar" /> - </AvatarDiv> - <EnviadoPor item xs={10}> - Enviado por: + const SlideAnimationContent = () => { + return ( + <SlideContentDiv> + <HeaderContainer container="row" justify="flex-start" alignItems="center"> + <AvatarDiv item xs={2}> + <img className="img" src={userAvatar} alt="user avatar" /> + </AvatarDiv> + <EnviadoPor item xs={10}> + Enviado por: <br /> - <p>{props.author}</p> - </EnviadoPor> - </HeaderContainer> - { - props.tags ? - <TagContainer container direction="row"> - { - props.tags.map((tag) => - <Grid item key={tag.id}> - <span >{tag.name}</span> - </Grid> - ) - } - </TagContainer> : - null - } - </SlideContentDiv> - ) - } + <p>{props.author}</p> + </EnviadoPor> + </HeaderContainer> + { + props.tags ? + <TagContainer container direction="row"> + { + props.tags.map((tag) => + <Grid item key={tag.id}> + <span >{tag.name}</span> + </Grid> + ) + } + </TagContainer> : + null + } + </SlideContentDiv> + ) + } - const handleSignUp = () => { - setSignUp(!signUpOpen) - } + const handleSignUp = () => { + setSignUp(!signUpOpen) + } - const handleLogin = () => { - setLogin(!loginOpen) - } + const handleLogin = () => { + setLogin(!loginOpen) + } - function Alert(props) { - return <MuiAlert elevation={6} variant="filled" {...props} />; - } + function Alert(props) { + return <MuiAlert elevation={6} variant="filled" {...props} />; + } - function toggleLoginSnackbar(reason) { - if (reason === 'clickaway') { - return; - } - handleSuccessfulLogin(false); + function toggleLoginSnackbar(reason) { + if (reason === 'clickaway') { + return; } - - return ( - <> - <SignUpModal open={signUpOpen} handleClose={handleSignUp} openLogin={handleLogin} - /> - <LoginModal open={loginOpen} handleClose={() => setLogin(false)} openSignUp={handleSignUp} - openSnackbar={() => { handleSuccessfulLogin(true) }} - /> - <Snackbar open={successfulLoginOpen} autoHideDuration={1000} onClose={toggleLoginSnackbar} - anchorOrigin={{ vertical: 'top', horizontal: 'center' }} - > - <Alert severity="success" style={{ backgroundColor: "#00acc1" }}>Você está conectado(a)!</Alert> - </Snackbar> - <StyledCard> - <CardDiv> - <CardReaDiv> - <Header onMouseEnter={controlSlide} onMouseLeave={controlSlide}> - <Slide direction="left" in={slideIn} timeout={1000}> - <div className={`slideContentLinkAfterActive${slideIn}`} style={{ width: '100%', height: "100%" }}> - <Link to={"/colecao-do-usuario/" + props.id} className="text" style={{ textDecoration: "none" }} > - {SlideAnimationContent()} - </Link> - </div> - </Slide> - <div className={`slideContentLinkBeforeActive${slideIn}`} style={{ width: '100%', height: '100%' }}> - <UserInfo> - {/* I(Luis) dont know why, but if i use styled components, sometimes the avatar will be deconfigured */} - <img src={userAvatar} alt="user avatar" style={{ - height: "70px", width: "70px", borderRadius: "50%", - zIndex: 1, border: "2px solid white", - boxShadow: "0 1px 3px rgba(0,0,0,.45)" - - }} /> - <UserAndTitle> - <span>{props.author}</span> - <span className={"col-name"}>{name}</span> - </UserAndTitle> - </UserInfo> - <StyledGrid container direction="row"> - { - props.thumbnails.map((thumb) => - <Grid item xs={props.thumbnails <= 4 && props.thumbnails > 0 ? 12 / props.thumbnails.length : 6}> - <div style={{ backgroundImage: `url(${`${apiDomain}` + thumb})`, height: "100%", width: "100%", backgroundSize: "cover", backgroundPosition: "center" }} /> - </Grid> - ) - } - </StyledGrid> - </div> - </Header> - - <Description> {/*renders rating, number of learning objects and likes count*/} - { - props.authorID !== state.currentUser.id && - <Rating - name="customized-empty" - value={props.rating} - readOnly - style={{ color: "#666" }} - emptyIcon={<StarBorderIcon fontSize="inherit" />} - /> - } - - <Footer> - <Type> - <FolderIcon /> - <span style={{ fontWeight: "bold" }}>{props.collections.length} </span> - <span>{props.collections.length !== 1 ? "Recursos" : "Recurso"}</span> - </Type> - <LikeCounter> - <span>{likesCount}</span> - <ButtonNoWidth onClick={handleLike}> - <FavoriteIcon style={{ color: liked ? "red" : "#666" }} /> - </ButtonNoWidth> - </LikeCounter> - </Footer> - </Description> - - </CardReaDiv> - - { - props.authorID === state.currentUser.id ? - ( - <CardReaFooter style={{ justifyContent: "space-between" }}> {/*renders following/unfollow and follow button*/} - <Grid container> - <Grid item xs={6} style={{ display: "flex", justifyContent: "center" }}> - { - privacy === 'private' && - <LockIcon style={{ color: "#666" }} /> - } - </Grid> - <Grid item xs={6} style={{ display: "flex", justifyContent: "flex-end" }}> - <ColCardOwnerOptions - id={props.id} - changeColName={changeColName} - changeColPrivacy={changeColPrivacy} - /> - </Grid> - </Grid> - </CardReaFooter> - ) - : - ( - <CardReaFooter> {/*renders following/unfollow and follow button*/} - { - userFollowingCol ? - ( - [ - RenderFollowingButton() - ] - ) - : - ( - [ - RenderFollowButton() - ] - ) - } - <ColCardPublicOptions - id={props.id} - /> - </CardReaFooter> - ) - } - - </CardDiv> - </StyledCard> - </> - ) + handleSuccessfulLogin(false); + } + + return ( + <> + <SignUpModal open={signUpOpen} handleClose={handleSignUp} openLogin={handleLogin} + /> + <LoginModal open={loginOpen} handleClose={() => setLogin(false)} openSignUp={handleSignUp} + openSnackbar={() => { handleSuccessfulLogin(true) }} + /> + <Snackbar open={successfulLoginOpen} autoHideDuration={1000} onClose={toggleLoginSnackbar} + anchorOrigin={{ vertical: 'top', horizontal: 'center' }} + > + <Alert severity="success" style={{ backgroundColor: "#00acc1" }}>Você está conectado(a)!</Alert> + </Snackbar> + <StyledCard> + <CardDiv> + <CardReaDiv> + <Header onMouseEnter={controlSlide} onMouseLeave={controlSlide}> + <Slide direction="left" in={slideIn} timeout={1000}> + <div className={`slideContentLinkAfterActive${slideIn}`} style={{ width: '100%', height: "100%" }}> + <Link to={"/colecao-do-usuario/" + props.id} className="text" style={{ textDecoration: "none" }} > + {SlideAnimationContent()} + </Link> + </div> + </Slide> + <div className={`slideContentLinkBeforeActive${slideIn}`} style={{ width: '100%', height: '100%' }}> + <UserInfo> + {/* I(Luis) dont know why, but if i use styled components, sometimes the avatar will be deconfigured */} + <img src={userAvatar} alt="user avatar" style={{ + height: "70px", width: "70px", borderRadius: "50%", + zIndex: 1, border: "2px solid white", + boxShadow: "0 1px 3px rgba(0,0,0,.45)" + + }} /> + <UserAndTitle> + <span>{props.author}</span> + <span className={"col-name"}>{name}</span> + </UserAndTitle> + </UserInfo> + <StyledGrid container direction="row"> + { + props.thumbnails.map((thumb) => + <Grid item xs={props.thumbnails <= 4 && props.thumbnails > 0 ? 12 / props.thumbnails.length : 6}> + <div style={{ backgroundImage: `url(${`${apiDomain}` + thumb})`, height: "100%", width: "100%", backgroundSize: "cover", backgroundPosition: "center" }} /> + </Grid> + ) + } + </StyledGrid> + </div> + </Header> + + <Description> {/*renders rating, number of learning objects and likes count*/} + { + props.authorID !== state.currentUser.id && + <Rating + name="customized-empty" + value={props.rating} + readOnly + style={{ color: "#666" }} + emptyIcon={<StarBorderIcon fontSize="inherit" />} + /> + } + + <Footer> + <Type> + <FolderIcon /> + <span style={{ fontWeight: "bold" }}>{props.collections.length} </span> + <span>{props.collections.length !== 1 ? "Recursos" : "Recurso"}</span> + </Type> + <LikeCounter> + <span>{likesCount}</span> + <ButtonNoWidth onClick={handleLike}> + <FavoriteIcon style={{ color: liked ? "red" : "#666" }} /> + </ButtonNoWidth> + </LikeCounter> + </Footer> + </Description> + + </CardReaDiv> + + { + props.authorID === state.currentUser.id ? + ( + <CardReaFooter style={{ justifyContent: "space-between" }}> {/*renders following/unfollow and follow button*/} + <Grid container> + <Grid item xs={6} style={{ display: "flex", justifyContent: "center" }}> + { + privacy === 'private' && + <LockIcon style={{ color: "#666" }} /> + } + </Grid> + <Grid item xs={6} style={{ display: "flex", justifyContent: "flex-end" }}> + <ColCardOwnerOptions + id={props.id} + changeColName={changeColName} + changeColPrivacy={changeColPrivacy} + /> + </Grid> + </Grid> + </CardReaFooter> + ) + : + ( + <CardReaFooter> {/*renders following/unfollow and follow button*/} + { + userFollowingCol ? + ( + [ + RenderFollowingButton() + ] + ) + : + ( + [ + RenderFollowButton() + ] + ) + } + <ColCardPublicOptions + id={props.id} + handleLogin={handleLogin} + currentUserId={state.currentUser.id} + /> + </CardReaFooter> + ) + } + + </CardDiv> + </StyledCard> + </> + ) } diff --git a/src/Pages/Search.js b/src/Pages/Search.js index 65dc38660dc52882e324ae361c0ec92ff9a4cf39..59c24ee39f9e043c024923372cf32ac141889bf3 100644 --- a/src/Pages/Search.js +++ b/src/Pages/Search.js @@ -135,7 +135,7 @@ export default function Search(props) { type: "HANDLE_SEARCH_BAR", opened: false, }); - }, [window.history.state.key]) + }, [window.history.state.key, state.currentUser.id]) useEffect(() => { setIsLoading(true); diff --git a/src/env.js b/src/env.js index b7359157e1c0e9d260976bbe2b2545a2e0ca51b0..82bf229b11fe78f3cb5ff8a58d096eec156cedf2 100644 --- a/src/env.js +++ b/src/env.js @@ -17,7 +17,7 @@ You should have received a copy of the GNU Affero General Public License along with Plataforma Integrada MEC. If not, see <http://www.gnu.org/licenses/>.*/ -var apiDomain = 'https://api.portalmec.c3sl.ufpr.br', +var apiDomain = 'https://api.portalmectest.c3sl.ufpr.br', apiVersion = 'v1', apiUrl = apiDomain + '/' + apiVersion;