From 970a948cecca7b0f22fbd2264196f611704f1b2d Mon Sep 17 00:00:00 2001 From: Mateusz Tylka Date: Fri, 29 Sep 2023 16:07:36 +0200 Subject: [PATCH] get current user profile info to disable edit and delete submission --- .../TableRowFooter/TableRowFooter.js | 20 ++++- src/pages/Challenge/Challenge.js | 21 +++-- src/pages/{ => Readme}/Readme.js | 82 +++++-------------- src/pages/Readme/ReadmeStyle.js | 64 +++++++++++++++ src/pages/Readme/components/ReadmeDesktop.js | 32 ++++++++ src/pages/Readme/components/ReadmeMobile.js | 32 ++++++++ src/pages/Readme/index.js | 1 + src/services/KeyCloakService.js | 14 +++- 8 files changed, 194 insertions(+), 72 deletions(-) rename src/pages/{ => Readme}/Readme.js (62%) create mode 100644 src/pages/Readme/ReadmeStyle.js create mode 100644 src/pages/Readme/components/ReadmeDesktop.js create mode 100644 src/pages/Readme/components/ReadmeMobile.js create mode 100644 src/pages/Readme/index.js diff --git a/src/components/generic/Table/components/TableRowFooter/TableRowFooter.js b/src/components/generic/Table/components/TableRowFooter/TableRowFooter.js index da98f34..a976c75 100644 --- a/src/components/generic/Table/components/TableRowFooter/TableRowFooter.js +++ b/src/components/generic/Table/components/TableRowFooter/TableRowFooter.js @@ -7,6 +7,24 @@ import deleteIco from '../../../../../assets/delete_ico.svg'; import KeyCloakService from '../../../../../services/KeyCloakService'; const TableRowFooter = ({ rowFooter, item, i, deleteItem, editItem }) => { + const [profileInfo, setProfileInfo] = React.useState(null); + + React.useEffect(() => { + KeyCloakService.getProfileInfo(setProfileInfo); + }, []); + + const isActive = () => { + if (!KeyCloakService.isLoggedIn()) return false; + if (profileInfo) { + if ( + profileInfo?.preferred_username !== item.submitter && + profileInfo?.name !== item.submitter + ) + return false; + } + return true; + }; + if (rowFooter) { return ( @@ -16,7 +34,7 @@ const TableRowFooter = ({ rowFooter, item, i, deleteItem, editItem }) => { { icon: pensilIco, handler: () => editItem() }, { icon: deleteIco, handler: () => deleteItem() }, ]} - active={KeyCloakService.isLoggedIn()} + active={isActive()} i={i} /> diff --git a/src/pages/Challenge/Challenge.js b/src/pages/Challenge/Challenge.js index 038a4c0..4b7ff20 100644 --- a/src/pages/Challenge/Challenge.js +++ b/src/pages/Challenge/Challenge.js @@ -4,10 +4,10 @@ import { useParams } from 'react-router-dom'; import { H1, Medium } from '../../utils/fonts'; import theme from '../../utils/theme'; import MobileChallengeMenu from './components/MobileChallengeMenu'; -import Leaderboard from '../Leaderboard/Leaderboard'; +import Leaderboard from '../Leaderboard'; import Readme from '../Readme'; -import HowTo from '../HowTo/HowTo'; -import MyEntries from '../MyEntries/MyEntries'; +import HowTo from '../HowTo'; +import MyEntries from '../MyEntries'; import Submit from '../Submit'; import Media from 'react-media'; import DesktopChallengeMenu from './components/DesktopChallengeMenu'; @@ -15,7 +15,7 @@ import { CHALLENGE_SECTIONS, RENDER_ICO } from '../../utils/globals'; import textIco from '../../assets/text_ico.svg'; import getChallengeInfo from '../../api/getChallengeInfo'; import Loading from '../../components/generic/Loading'; -import AllEntries from '../AllEntries/AllEntries'; +import AllEntries from '../AllEntries'; const Challenge = (props) => { const challengeName = useParams().challengeId; @@ -37,7 +37,11 @@ const Challenge = (props) => { ); case CHALLENGE_SECTIONS.ALL_ENTRIES: return ( - + ); case CHALLENGE_SECTIONS.README: return ( @@ -56,7 +60,12 @@ const Challenge = (props) => { /> ); case CHALLENGE_SECTIONS.MY_ENTRIES: - return ; + return ( + + ); case CHALLENGE_SECTIONS.SUBMIT: return ; default: diff --git a/src/pages/Readme.js b/src/pages/Readme/Readme.js similarity index 62% rename from src/pages/Readme.js rename to src/pages/Readme/Readme.js index 463d625..58a36b8 100644 --- a/src/pages/Readme.js +++ b/src/pages/Readme/Readme.js @@ -1,65 +1,13 @@ import React from 'react'; -import { FlexColumn } from '../utils/containers'; -import { Body, H2 } from '../utils/fonts'; +import { FlexColumn } from '../../utils/containers'; +import { H2 } from '../../utils/fonts'; import Media from 'react-media'; -import theme from '../utils/theme'; -import getChallengeFullDescription from '../api/getChallengeFullDescription'; -import styled from 'styled-components'; -import InfoList from '../components/generic/InfoList'; -import Loading from '../components/generic/Loading'; +import theme from '../../utils/theme'; +import getChallengeFullDescription from '../../api/getChallengeFullDescription'; +import InfoList from '../../components/generic/InfoList'; +import Loading from '../../components/generic/Loading'; import { marked } from 'marked'; - -const ReadmeStyle = styled(Body)` - * { - font-weight: inherit; - } - - h2 { - font-family: 'Kanit', sans-serif; - margin: 32px 0; - } - - h3 { - font-family: 'Kanit', sans-serif; - font-weight: inherit; - font-size: 18px; - line-height: 22px; - margin: 24px 0; - - @media (min-width: ${({ theme }) => theme.overMobile}) { - font-size: 22px; - line-height: 26px; - } - } - - p { - font-family: 'Roboto', sans-serif; - font-weight: 300; - font-size: 14px; - line-height: 20px; - - @media (min-width: ${({ theme }) => theme.overMobile}) { - font-weight: 400; - font-size: 16px; - line-height: 22px; - } - } - - a { - font-family: 'Roboto', sans-serif; - font-weight: 400; - font-size: 14px; - line-height: 20px; - color: ${({ theme }) => theme.colors.dark}; - text-decoration: none; - - @media (min-width: ${({ theme }) => theme.overMobile}) { - font-size: 16px; - line-height: 22px; - font-weight: 500; - } - } -`; +import ReadmeStyle from './ReadmeStyle'; const Readme = (props) => { const [fullDescription, setFullDescription] = React.useState(''); @@ -117,8 +65,13 @@ const Readme = (props) => { const desktopRender = () => { return ( - - + +

Info

{ deadline={props.deadline} />
- + theme.overMobile}) { + gap: 64px; + } + + /* .ReadmeStyle__content { */ + * { + font-weight: inherit; + } + + h2 { + font-family: 'Kanit', sans-serif; + margin: 32px 0; + } + + h3 { + font-family: 'Kanit', sans-serif; + font-weight: inherit; + font-size: 18px; + line-height: 22px; + margin: 24px 0; + + @media (min-width: ${({ theme }) => theme.overMobile}) { + font-size: 22px; + line-height: 26px; + } + } + + p { + font-family: 'Roboto', sans-serif; + font-weight: 300; + font-size: 14px; + line-height: 20px; + + @media (min-width: ${({ theme }) => theme.overMobile}) { + font-weight: 400; + font-size: 16px; + line-height: 22px; + } + } + + a { + font-family: 'Roboto', sans-serif; + font-weight: 400; + font-size: 14px; + line-height: 20px; + color: ${({ theme }) => theme.colors.dark}; + text-decoration: none; + + @media (min-width: ${({ theme }) => theme.overMobile}) { + font-size: 16px; + line-height: 22px; + font-weight: 500; + } + } + /* } */ +`; + +export default ReadmeStyle; diff --git a/src/pages/Readme/components/ReadmeDesktop.js b/src/pages/Readme/components/ReadmeDesktop.js new file mode 100644 index 0000000..35970f1 --- /dev/null +++ b/src/pages/Readme/components/ReadmeDesktop.js @@ -0,0 +1,32 @@ +import React from 'react'; +import { FlexColumn } from '../../../utils/containers'; +import { H2 } from '../../../utils/fonts'; +import InfoList from '../../../components/generic/InfoList'; +import ReadmeStyle from '../ReadmeStyle'; + +const ReadmeDesktop = (props) => { + return ( + + +

Info

+ +
+ + + +
+ ); +}; + +export default ReadmeDesktop; diff --git a/src/pages/Readme/components/ReadmeMobile.js b/src/pages/Readme/components/ReadmeMobile.js new file mode 100644 index 0000000..77e8952 --- /dev/null +++ b/src/pages/Readme/components/ReadmeMobile.js @@ -0,0 +1,32 @@ +import React from 'react'; +import { FlexColumn } from '../../../utils/containers'; +import { H2 } from '../../../utils/fonts'; +import InfoList from '../../../components/generic/InfoList'; +import ReadmeStyle from '../ReadmeStyle'; + +const ReadmeMobile = (props) => { + return ( + + +

Info

+ +
+ + + +
+ ); +}; + +export default ReadmeMobile; diff --git a/src/pages/Readme/index.js b/src/pages/Readme/index.js new file mode 100644 index 0000000..5af9306 --- /dev/null +++ b/src/pages/Readme/index.js @@ -0,0 +1 @@ +export { default } from './Readme'; diff --git a/src/services/KeyCloakService.js b/src/services/KeyCloakService.js index 9a6c3d2..fde8bea 100644 --- a/src/services/KeyCloakService.js +++ b/src/services/KeyCloakService.js @@ -1,5 +1,6 @@ import Keycloak from 'keycloak-js'; import { POLICY_PRIVACY_PAGE, ROOT_URL } from '../utils/globals'; +import SESSION_STORAGE from '../utils/sessionStorage'; const _kc = new Keycloak({ url: process.env.REACT_APP_KC_URL, @@ -30,14 +31,17 @@ const doLogin = () => { if (privacyPolicyAccept !== 'accept') { window.location.replace(`${ROOT_URL}${POLICY_PRIVACY_PAGE}/login`); } else { - sessionStorage.setItem('logout', ''); + sessionStorage.setItem(SESSION_STORAGE.LOGOUT, ''); _kc.login(); } }; const doLogout = () => { sessionStorage.clear(); - sessionStorage.setItem('logout', 'yes'); + sessionStorage.setItem( + SESSION_STORAGE.LOGOUT, + SESSION_STORAGE.STATIC_VALUE.YES + ); _kc.logout(); }; @@ -62,10 +66,13 @@ const getUsername = () => _kc.tokenParsed?.preferred_username; const hasRole = (roles) => roles.some((role) => _kc.hasRealmRole(role)); const goToProfile = () => { - console.log(_kc.loadUserProfile()); _kc.accountManagement(); }; +const getProfileInfo = async (setProfileInfo) => { + _kc.loadUserInfo().then((response) => setProfileInfo(response)); +}; + const KeyCloakService = { initKeycloak, doLogin, @@ -77,6 +84,7 @@ const KeyCloakService = { hasRole, doRegister, goToProfile, + getProfileInfo, }; export default KeyCloakService;