handle Done, Clear, Cancel in TagsChoosePopUp

This commit is contained in:
Mateusz Tylka 2023-05-19 11:58:33 +02:00
parent b75309f4af
commit ea64e06b3e
5 changed files with 51 additions and 44 deletions

View File

@ -51,19 +51,11 @@ const Submit = (props) => {
dispatch({ type: SUBMIT_ACTION.SET_REPO_BRANCH, payload: value }); dispatch({ type: SUBMIT_ACTION.SET_REPO_BRANCH, payload: value });
}; };
const toggleSubmissionTag = React.useCallback( const updateTags = (submissionTags, tags) => {
(tag) => { dispatch({
let actionType = ''; type: SUBMIT_ACTION.UPDATE_TAGS,
if (state.submissionTags.includes(tag)) payload: { submissionTags: submissionTags, tags: tags },
actionType = SUBMIT_ACTION.REMOVE_SUBMISSION_TAG; });
else actionType = SUBMIT_ACTION.ADD_SUBMISSION_TAG;
dispatch({ type: actionType, payload: tag });
},
[state.submissionTags]
);
const clearSubmissionTags = () => {
dispatch({ type: SUBMIT_ACTION.CLEAR_SUBMISSION_TAGS });
}; };
if (!state.submissionLoading) { if (!state.submissionLoading) {
@ -81,10 +73,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"
toggleSubmissionTag={toggleSubmissionTag} updateTags={updateTags}
tags={state.tags} tags={state.tags}
submissionTags={state.submissionTags} submissionTags={state.submissionTags}
clearSubmissionTags={clearSubmissionTags}
/> />
</FlexColumn> </FlexColumn>
<Button width="122px" height="44px" handler={challengeSubmissionSubmit}> <Button width="122px" height="44px" handler={challengeSubmissionSubmit}>

View File

@ -40,9 +40,8 @@ const TagsChoose = (props) => {
<TagsChoosePopUp <TagsChoosePopUp
tags={props.tags} tags={props.tags}
submissionTags={props.submissionTags} submissionTags={props.submissionTags}
toggleSubmissionTag={props.toggleSubmissionTag} updateTags={props.updateTags}
setTagsPopUp={setTagsPopUp} setTagsPopUp={setTagsPopUp}
clearSubmissionTags={props.clearSubmissionTags}
/>, />,
document.body document.body
)} )}

View File

@ -8,6 +8,31 @@ import TagsChoosePopUpStyle from './TagsChoosePopUpStyle';
import renderTagItems from './functions/renderTagItems'; import renderTagItems from './functions/renderTagItems';
const TagsChoosePopUp = (props) => { const TagsChoosePopUp = (props) => {
const [tagsChoosed, setTagsChoosed] = React.useState([]);
const [tags, setTags] = React.useState([]);
React.useEffect(() => {
setTags(props.tags.slice());
setTagsChoosed(props.submissionTags.slice());
}, [props.tags, props.submissionTags]);
const toggleTagChoose = (clickedTag) => {
let newTagsChoosed = tagsChoosed;
let newTags = tags;
if (tagsChoosed.includes(clickedTag)) {
newTagsChoosed = newTagsChoosed.filter(
(tag) => tag.name !== clickedTag.name
);
newTags.push(clickedTag);
newTags = newTags.sort((a, b) => a.name.localeCompare(b.name));
} else {
newTagsChoosed.push(clickedTag);
newTags = newTags.filter((tag) => tag.name !== clickedTag.name);
}
setTagsChoosed(newTagsChoosed);
setTags(newTags);
};
return ( return (
<PopUp <PopUp
width="50%" width="50%"
@ -19,23 +44,30 @@ const TagsChoosePopUp = (props) => {
<Search /> <Search />
<FlexColumn as="ul" className="TagsChoosePopUpStyle__tags-list"> <FlexColumn as="ul" className="TagsChoosePopUpStyle__tags-list">
{renderTagItems( {renderTagItems(
props.submissionTags, tagsChoosed,
props.toggleSubmissionTag, toggleTagChoose,
`1px dotted ${theme.colors.green}`, `1px dotted ${theme.colors.green}`,
theme.colors.green03, theme.colors.green03,
true true
)} )}
{renderTagItems(props.tags, props.toggleSubmissionTag, 'none')} {renderTagItems(tags, toggleTagChoose, 'none')}
</FlexColumn> </FlexColumn>
<FlexRow width="100%" gap="20px" alignmentX="flex-start"> <FlexRow width="100%" gap="20px" alignmentX="flex-start">
<Button height="32px" width="76px"> <Button
height="32px"
width="76px"
handler={() => {
props.updateTags(tagsChoosed, tags);
props.setTagsPopUp(false);
}}
>
Done Done
</Button> </Button>
<Button <Button
height="32px" height="32px"
width="76px" width="76px"
backgroundColor={theme.colors.dark08} backgroundColor={theme.colors.dark08}
handler={() => props.clearSubmissionTags()} handler={() => setTagsChoosed([])}
> >
Clear Clear
</Button> </Button>

View File

@ -4,9 +4,7 @@ const SUBMIT_ACTION = {
SET_REPO_BRANCH: 'set_repo_branch', SET_REPO_BRANCH: 'set_repo_branch',
TOGGLE_SUBMISSION_LOADING: 'toggle_submission_loading', TOGGLE_SUBMISSION_LOADING: 'toggle_submission_loading',
LOAD_TAGS: 'load_tags', LOAD_TAGS: 'load_tags',
ADD_SUBMISSION_TAG: 'add_submission_tag', UPDATE_TAGS: 'update_tags',
REMOVE_SUBMISSION_TAG: 'remove_submission_tag',
CLEAR_SUBMISSION_TAGS: 'clear_submission_tags',
}; };
export default SUBMIT_ACTION; export default SUBMIT_ACTION;

View File

@ -2,7 +2,6 @@ import SUBMIT_ACTION from './SubmitActionEnum';
const SubmitReducer = (state, action) => { const SubmitReducer = (state, action) => {
console.log(`SubmitReducer: ${action.type}`); console.log(`SubmitReducer: ${action.type}`);
let newSubmissionTags = state.submissionTags;
let newTags = state.tags; let newTags = state.tags;
switch (action.type) { switch (action.type) {
case SUBMIT_ACTION.SET_DESCRIPTION: case SUBMIT_ACTION.SET_DESCRIPTION:
@ -16,24 +15,12 @@ const SubmitReducer = (state, action) => {
case SUBMIT_ACTION.LOAD_TAGS: case SUBMIT_ACTION.LOAD_TAGS:
if (state.tags.length === 0) newTags = action.payload; if (state.tags.length === 0) newTags = action.payload;
return { ...state, tags: newTags }; return { ...state, tags: newTags };
case SUBMIT_ACTION.ADD_SUBMISSION_TAG: case SUBMIT_ACTION.UPDATE_TAGS:
if (!newSubmissionTags.includes(action.payload)) { return {
newTags = state.tags; ...state,
newSubmissionTags.push(action.payload); submissionTags: action.payload.submissionTags,
newTags = newTags.filter((tag) => tag.name !== action.payload.name); tags: action.payload.tags,
} };
return { ...state, submissionTags: newSubmissionTags, tags: newTags };
case SUBMIT_ACTION.REMOVE_SUBMISSION_TAG:
if (newSubmissionTags.includes(action.payload)) {
newSubmissionTags = newSubmissionTags.filter(
(tag) => tag.name !== action.payload.name
);
newTags.push(action.payload);
newTags = newTags.sort((a, b) => a.name.localeCompare(b.name));
}
return { ...state, submissionTags: newSubmissionTags, tags: newTags };
case SUBMIT_ACTION.CLEAR_SUBMISSION_TAGS:
return { ...state, submissionTags: [] };
default: default:
throw new Error('Undefined action in SubmitReducer!'); throw new Error('Undefined action in SubmitReducer!');
} }