diff --git a/src/api/challengeSubmissionPost.js b/src/api/challengeSubmissionPost.js index b0d5d10..7a0b5da 100644 --- a/src/api/challengeSubmissionPost.js +++ b/src/api/challengeSubmissionPost.js @@ -1,12 +1,13 @@ import KeyCloakService from '../services/KeyCloakService'; import { API } from '../utils/globals'; +import SUBMIT_ACTION from '../pages/Submit/model/SubmitActionEnum'; const challengeSubmission = ( challengeName, repoUrl, repoBranch, description, - setLoading + dispatch ) => { const details = { f1: description, @@ -30,7 +31,7 @@ const challengeSubmission = ( }) .then((resp) => resp.json()) .then((data) => { - setLoading(true); + dispatch({ type: SUBMIT_ACTION.TOGGLE_SUBMISSION_LOADING }); const processUrl = API.replace('/api', ''); window.location.replace(`${processUrl}/open-view-progress/${data}#form`); // console.log(data); diff --git a/src/api/getChallenges.js b/src/api/getChallenges.js index a5c790a..26d8a3f 100644 --- a/src/api/getChallenges.js +++ b/src/api/getChallenges.js @@ -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'; const getChallenges = (dispatch) => { diff --git a/src/api/getTags.js b/src/api/getTags.js index 4ed6e76..fc1f959 100644 --- a/src/api/getTags.js +++ b/src/api/getTags.js @@ -1,10 +1,11 @@ +import SUBMIT_ACTION from '../pages/Submit/model/SubmitActionEnum'; import { API } from '../utils/globals'; -const getTags = (setTags) => { +const getTags = (dispatch) => { fetch(`${API}/list-tags`) .then((response) => response.json()) .then((data) => { - setTags(data); + dispatch({ type: SUBMIT_ACTION.LOAD_TAGS, payload: data }); }); }; diff --git a/src/pages/Challanges/Challenges.js b/src/pages/Challanges/Challenges.js index 79daa46..ae98353 100644 --- a/src/pages/Challanges/Challenges.js +++ b/src/pages/Challanges/Challenges.js @@ -9,7 +9,7 @@ import ChallengesMobile from './components/ChallengesMobile'; import ChallengesDesktop from './components/ChallengesDesktop'; import challengeSearchQueryHandler from './functions/challengeSearchQueryHandler'; import ChallengesReducer from './model/ChallengesReducer'; -import CHALLENGES_ACTION from './model/ChallengesActionEnum'; +import CHALLENGES_ACTION from './model/ChallengesActions'; const Challenges = () => { const [state, dispatch] = React.useReducer(ChallengesReducer, { diff --git a/src/pages/Challanges/components/ChallengesMobile.js b/src/pages/Challanges/components/ChallengesMobile.js index 63cceaa..939da5f 100644 --- a/src/pages/Challanges/components/ChallengesMobile.js +++ b/src/pages/Challanges/components/ChallengesMobile.js @@ -7,7 +7,7 @@ import Search from '../../../components/generic/Search'; import { CALC_PAGES } from '../../../utils/globals'; import renderChallenges from '../functions/renderChallenges'; import Loading from '../../../components/generic/Loading'; -import CHALLENGES_ACTION from '../model/ChallengesActionEnum'; +import CHALLENGES_ACTION from '../model/ChallengesActions'; const ChallengesMobile = (props) => { return ( diff --git a/src/pages/Challanges/components/FiltersMenu/FiltersMenu.js b/src/pages/Challanges/components/FiltersMenu/FiltersMenu.js index 8e10e33..7f76e7c 100644 --- a/src/pages/Challanges/components/FiltersMenu/FiltersMenu.js +++ b/src/pages/Challanges/components/FiltersMenu/FiltersMenu.js @@ -6,7 +6,7 @@ import styled from 'styled-components'; import FilterBy from '../FilterBy'; import filterOptions from './filterOptions'; import Media from 'react-media'; -import CHALLENGES_ACTION from '../../model/ChallengesActionEnum'; +import CHALLENGES_ACTION from '../../model/ChallengesActions'; const FiltersMenuStyle = styled(FlexColumn)` position: fixed; diff --git a/src/pages/Challanges/functions/challengeSearchQueryHandler.js b/src/pages/Challanges/functions/challengeSearchQueryHandler.js index 90f279e..7cc4a90 100644 --- a/src/pages/Challanges/functions/challengeSearchQueryHandler.js +++ b/src/pages/Challanges/functions/challengeSearchQueryHandler.js @@ -1,4 +1,4 @@ -import CHALLENGES_ACTION from '../model/ChallengesActionEnum'; +import CHALLENGES_ACTION from '../model/ChallengesActions'; const challengeSearchQueryHandler = (event, challengesFromAPI, dispatch) => { let searchQuery = event.target.value; diff --git a/src/pages/Challanges/functions/statusFilterHandle.js b/src/pages/Challanges/functions/statusFilterHandle.js index 4e3bfef..48af319 100644 --- a/src/pages/Challanges/functions/statusFilterHandle.js +++ b/src/pages/Challanges/functions/statusFilterHandle.js @@ -1,5 +1,5 @@ import { CHALLENGES_STATUS_FILTER } from '../../../utils/globals'; -import CHALLENGES_ACTION from '../model/ChallengesActionEnum'; +import CHALLENGES_ACTION from '../model/ChallengesActions'; const dateIsOlder = (newerDate, olderDate) => { if (newerDate.year > olderDate.year) return true; diff --git a/src/pages/Challanges/model/ChallengesActionEnum.js b/src/pages/Challanges/model/ChallengesActions.js similarity index 100% rename from src/pages/Challanges/model/ChallengesActionEnum.js rename to src/pages/Challanges/model/ChallengesActions.js diff --git a/src/pages/Challanges/model/ChallengesReducer.js b/src/pages/Challanges/model/ChallengesReducer.js index 6b42f7a..99ac043 100644 --- a/src/pages/Challanges/model/ChallengesReducer.js +++ b/src/pages/Challanges/model/ChallengesReducer.js @@ -1,4 +1,4 @@ -import CHALLENGES_ACTION from './ChallengesActionEnum'; +import CHALLENGES_ACTION from './ChallengesActions'; const ChallengesReducer = (state, action) => { switch (action.type) { diff --git a/src/pages/Submit/Submit.js b/src/pages/Submit/Submit.js index 46eebfc..c1230e8 100644 --- a/src/pages/Submit/Submit.js +++ b/src/pages/Submit/Submit.js @@ -9,32 +9,51 @@ import challengeSubmission from '../../api/challengeSubmissionPost'; import Loading from '../../components/generic/Loading'; import getTags from '../../api/getTags'; import TagsChoose from './components/TagsChoose'; +import SubmitReducer from './model/SubmitReducer'; +import SUBMIT_ACTION from './model/SubmitActionEnum'; const Submit = (props) => { - const [description, setDescription] = React.useState(''); - const [repoUrl, setRepoUrl] = React.useState(''); - const [repoBranch, setRepoBranch] = React.useState(''); - const [loading, setLoading] = React.useState(false); - const [tags, setTags] = React.useState([]); - // eslint-disable-next-line no-unused-vars - const [submissionTags, setSubmissionTags] = React.useState([]); + const [state, dispatch] = React.useReducer(SubmitReducer, { + description: '', + repoUrl: '', + repoBranch: '', + submissionLoading: false, + tags: [], + submissionTags: [], + }); React.useMemo(() => { - getTags(setTags); + getTags(dispatch); }, []); const challengeSubmissionSubmit = () => { - setLoading(true); + dispatch({ type: SUBMIT_ACTION.TOGGLE_SUBMISSION_LOADING }); challengeSubmission( props.challengeName, - repoUrl, - repoBranch, - description, - setLoading + state.repoUrl, + state.repoBranch, + state.description, + 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 ( {