adding submission tags

This commit is contained in:
Mateusz Tylka 2023-05-15 11:31:02 +02:00
parent ce92499528
commit 9458061f29
17 changed files with 119 additions and 32 deletions

View File

@ -1,12 +1,13 @@
import KeyCloakService from '../services/KeyCloakService'; import KeyCloakService from '../services/KeyCloakService';
import { API } from '../utils/globals'; import { API } from '../utils/globals';
import SUBMIT_ACTION from '../pages/Submit/model/SubmitActionEnum';
const challengeSubmission = ( const challengeSubmission = (
challengeName, challengeName,
repoUrl, repoUrl,
repoBranch, repoBranch,
description, description,
setLoading dispatch
) => { ) => {
const details = { const details = {
f1: description, f1: description,
@ -30,7 +31,7 @@ const challengeSubmission = (
}) })
.then((resp) => resp.json()) .then((resp) => resp.json())
.then((data) => { .then((data) => {
setLoading(true); dispatch({ type: SUBMIT_ACTION.TOGGLE_SUBMISSION_LOADING });
const processUrl = API.replace('/api', ''); const processUrl = API.replace('/api', '');
window.location.replace(`${processUrl}/open-view-progress/${data}#form`); window.location.replace(`${processUrl}/open-view-progress/${data}#form`);
// console.log(data); // console.log(data);

View File

@ -1,4 +1,4 @@
import CHALLENGES_ACTION from '../pages/Challanges/model/ChallengesActionEnum'; import CHALLENGES_ACTION from '../pages/Challanges/model/ChallengesActions';
import { API } from '../utils/globals'; import { API } from '../utils/globals';
const getChallenges = (dispatch) => { const getChallenges = (dispatch) => {

View File

@ -1,10 +1,11 @@
import SUBMIT_ACTION from '../pages/Submit/model/SubmitActionEnum';
import { API } from '../utils/globals'; import { API } from '../utils/globals';
const getTags = (setTags) => { const getTags = (dispatch) => {
fetch(`${API}/list-tags`) fetch(`${API}/list-tags`)
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
setTags(data); dispatch({ type: SUBMIT_ACTION.LOAD_TAGS, payload: data });
}); });
}; };

View File

@ -9,7 +9,7 @@ import ChallengesMobile from './components/ChallengesMobile';
import ChallengesDesktop from './components/ChallengesDesktop'; import ChallengesDesktop from './components/ChallengesDesktop';
import challengeSearchQueryHandler from './functions/challengeSearchQueryHandler'; import challengeSearchQueryHandler from './functions/challengeSearchQueryHandler';
import ChallengesReducer from './model/ChallengesReducer'; import ChallengesReducer from './model/ChallengesReducer';
import CHALLENGES_ACTION from './model/ChallengesActionEnum'; import CHALLENGES_ACTION from './model/ChallengesActions';
const Challenges = () => { const Challenges = () => {
const [state, dispatch] = React.useReducer(ChallengesReducer, { const [state, dispatch] = React.useReducer(ChallengesReducer, {

View File

@ -7,7 +7,7 @@ import Search from '../../../components/generic/Search';
import { CALC_PAGES } from '../../../utils/globals'; import { CALC_PAGES } from '../../../utils/globals';
import renderChallenges from '../functions/renderChallenges'; import renderChallenges from '../functions/renderChallenges';
import Loading from '../../../components/generic/Loading'; import Loading from '../../../components/generic/Loading';
import CHALLENGES_ACTION from '../model/ChallengesActionEnum'; import CHALLENGES_ACTION from '../model/ChallengesActions';
const ChallengesMobile = (props) => { const ChallengesMobile = (props) => {
return ( return (

View File

@ -6,7 +6,7 @@ import styled from 'styled-components';
import FilterBy from '../FilterBy'; import FilterBy from '../FilterBy';
import filterOptions from './filterOptions'; import filterOptions from './filterOptions';
import Media from 'react-media'; import Media from 'react-media';
import CHALLENGES_ACTION from '../../model/ChallengesActionEnum'; import CHALLENGES_ACTION from '../../model/ChallengesActions';
const FiltersMenuStyle = styled(FlexColumn)` const FiltersMenuStyle = styled(FlexColumn)`
position: fixed; position: fixed;

View File

@ -1,4 +1,4 @@
import CHALLENGES_ACTION from '../model/ChallengesActionEnum'; import CHALLENGES_ACTION from '../model/ChallengesActions';
const challengeSearchQueryHandler = (event, challengesFromAPI, dispatch) => { const challengeSearchQueryHandler = (event, challengesFromAPI, dispatch) => {
let searchQuery = event.target.value; let searchQuery = event.target.value;

View File

@ -1,5 +1,5 @@
import { CHALLENGES_STATUS_FILTER } from '../../../utils/globals'; import { CHALLENGES_STATUS_FILTER } from '../../../utils/globals';
import CHALLENGES_ACTION from '../model/ChallengesActionEnum'; import CHALLENGES_ACTION from '../model/ChallengesActions';
const dateIsOlder = (newerDate, olderDate) => { const dateIsOlder = (newerDate, olderDate) => {
if (newerDate.year > olderDate.year) return true; if (newerDate.year > olderDate.year) return true;

View File

@ -1,4 +1,4 @@
import CHALLENGES_ACTION from './ChallengesActionEnum'; import CHALLENGES_ACTION from './ChallengesActions';
const ChallengesReducer = (state, action) => { const ChallengesReducer = (state, action) => {
switch (action.type) { switch (action.type) {

View File

@ -9,32 +9,51 @@ import challengeSubmission from '../../api/challengeSubmissionPost';
import Loading from '../../components/generic/Loading'; import Loading from '../../components/generic/Loading';
import getTags from '../../api/getTags'; import getTags from '../../api/getTags';
import TagsChoose from './components/TagsChoose'; import TagsChoose from './components/TagsChoose';
import SubmitReducer from './model/SubmitReducer';
import SUBMIT_ACTION from './model/SubmitActionEnum';
const Submit = (props) => { const Submit = (props) => {
const [description, setDescription] = React.useState(''); const [state, dispatch] = React.useReducer(SubmitReducer, {
const [repoUrl, setRepoUrl] = React.useState(''); description: '',
const [repoBranch, setRepoBranch] = React.useState(''); repoUrl: '',
const [loading, setLoading] = React.useState(false); repoBranch: '',
const [tags, setTags] = React.useState([]); submissionLoading: false,
// eslint-disable-next-line no-unused-vars tags: [],
const [submissionTags, setSubmissionTags] = React.useState([]); submissionTags: [],
});
React.useMemo(() => { React.useMemo(() => {
getTags(setTags); getTags(dispatch);
}, []); }, []);
const challengeSubmissionSubmit = () => { const challengeSubmissionSubmit = () => {
setLoading(true); dispatch({ type: SUBMIT_ACTION.TOGGLE_SUBMISSION_LOADING });
challengeSubmission( challengeSubmission(
props.challengeName, props.challengeName,
repoUrl, state.repoUrl,
repoBranch, state.repoBranch,
description, state.description,
setLoading dispatch
); );
}; };
if (!loading) { const setDescription = (value) => {
dispatch({ type: SUBMIT_ACTION.SET_DESCRIPTION, payload: value });
};
const setRepoUrl = (value) => {
dispatch({ type: SUBMIT_ACTION.SET_REPO_URL, payload: value });
};
const setRepoBranch = (value) => {
dispatch({ type: SUBMIT_ACTION.SET_REPO_BRANCH, payload: value });
};
const addSubmissionTag = React.useCallback((value) => {
dispatch({ type: SUBMIT_ACTION.ADD_SUBMISSION_TAG, payload: value });
}, []);
if (!state.submissionLoading) {
return ( return (
<FlexColumn <FlexColumn
margin="40px 0 0 0" margin="40px 0 0 0"
@ -57,8 +76,9 @@ const Submit = (props) => {
<SubmitInput label="Submission repo branch" handler={setRepoBranch} /> <SubmitInput label="Submission repo branch" handler={setRepoBranch} />
<TagsChoose <TagsChoose
label="Submission tags" label="Submission tags"
handler={setSubmissionTags} addSubmissionTag={addSubmissionTag}
tags={tags} tags={state.tags}
submissionTags={state.submissionTags}
/> />
</FlexColumn> </FlexColumn>
<Button width="122px" height="44px" handler={challengeSubmissionSubmit}> <Button width="122px" height="44px" handler={challengeSubmissionSubmit}>

View File

@ -28,7 +28,12 @@ const TagsChoose = (props) => {
</Grid> </Grid>
{tagsPopUp && {tagsPopUp &&
createPortal( createPortal(
<TagsChoosePopUp tags={props.tags} setTagsPopUp={setTagsPopUp} />, <TagsChoosePopUp
tags={props.tags}
submissionTags={props.submissionTags}
addSubmissionTag={props.addSubmissionTag}
setTagsPopUp={setTagsPopUp}
/>,
document.body document.body
)} )}
</TagsChooseStyle> </TagsChooseStyle>

View File

@ -5,6 +5,7 @@ import Search from '../../../../components/generic/Search';
import theme from '../../../../utils/theme'; import theme from '../../../../utils/theme';
import Button from '../../../../components/generic/Button'; import Button from '../../../../components/generic/Button';
import TagsChoosePopUpStyle from './TagsChoosePopUpStyle'; import TagsChoosePopUpStyle from './TagsChoosePopUpStyle';
import tagColorHandle from './functions/tagColorHandle';
const TagsChoosePopUp = (props) => { const TagsChoosePopUp = (props) => {
return ( return (
@ -21,10 +22,14 @@ const TagsChoosePopUp = (props) => {
return ( return (
<FlexRow <FlexRow
key={`tag-${index}`} key={`tag-${index}`}
onClick={() => props.addSubmissionTag(tag.name)}
className="TagsChoosePopUpStyle__tag-item" className="TagsChoosePopUpStyle__tag-item"
backgroundColor={ backgroundColor={tagColorHandle(
index % 2 === 0 ? theme.colors.dark01 : theme.colors.white theme,
} index,
tag,
props.submissionTags
)}
> >
{tag.name} {tag.name}
</FlexRow> </FlexRow>

View File

@ -15,10 +15,16 @@ const TagsChoosePopUpStyle = styled(FlexColumn)`
} }
.TagsChoosePopUpStyle__tag-item { .TagsChoosePopUpStyle__tag-item {
height: 48px; height: 42px;
width: 100%; width: 100%;
justify-content: flex-start; justify-content: flex-start;
padding: 12px; padding: 12px;
cursor: pointer;
transition: background-color 0.3s ease-in-out;
&:hover {
background-color: ${({ theme }) => theme.colors.green03};
}
} }
`; `;

View File

@ -0,0 +1,7 @@
const tagColorHandle = (theme, index, tag, tags) => {
if (tags.includes(tag.name)) return theme.colors.green;
if (index % 2 === 0) return theme.colors.dark01;
return theme.colors.white;
};
export default tagColorHandle;

View File

@ -0,0 +1,12 @@
const SUBMIT_ACTION = {
SET_DESCRIPTION: 'set_description',
SET_REPO_URL: 'set_repo_url',
SET_REPO_BRANCH: 'set_repo_branch',
TOGGLE_SUBMISSION_LOADING: 'toggle_submission_loading',
LOAD_TAGS: 'load_tags',
ADD_SUBMISSION_TAGS: 'add_submission_tag',
REMOVE_SUBMISSION_TAGS: 'remove_submission_tag',
CLEAR_SUBMISSION_TAGS: 'clear_submission_tags',
};
export default SUBMIT_ACTION;

View File

@ -0,0 +1,30 @@
import SUBMIT_ACTION from './SubmitActionEnum';
const SubmitReducer = (state, action) => {
console.log('SubmitReducer');
let initTags = state.tags;
let newSubmissionTags = state.submissionTags;
switch (action.type) {
case SUBMIT_ACTION.SET_DESCRIPTION:
return { ...state, description: action.payload };
case SUBMIT_ACTION.SET_REPO_BRANCH:
return { ...state, repoBranch: action.payload };
case SUBMIT_ACTION.SET_REPO_URL:
return { ...state, repoUrl: action.payload };
case SUBMIT_ACTION.TOGGLE_SUBMISSION_LOADING:
return { ...state, submissionLoading: !state.submissionLoading };
case SUBMIT_ACTION.LOAD_TAGS:
if (state.tags.length === 0) initTags = action.payload;
return { ...state, tags: initTags };
case SUBMIT_ACTION.ADD_SUBMISSION_TAG:
if (!newSubmissionTags.includes(action.payload))
newSubmissionTags.push(action.payload);
return { ...state, submissionTags: newSubmissionTags };
case SUBMIT_ACTION.CLEAR_SUBMISSION_TAGS:
return { ...state, submissionTags: [] };
default:
throw new Error('Undefined action in SubmitReducer!');
}
};
export default SubmitReducer;