Skip to content

Commit

Permalink
#2 add autocomplete field for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
tibor-postek-m2ms committed Mar 10, 2020
1 parent bfb0f9b commit a2d95c0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 32 deletions.
69 changes: 42 additions & 27 deletions js/components/projects/projectModal/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import React, { memo } from 'react';
import Modal from '../../common/Modal';
import { useDispatch, useSelector } from 'react-redux';
import { setProjectModalOpen } from '../redux/actions';
import { makeStyles, RadioGroup, Radio, Grid, Typography, Chip, MenuItem, Button } from '@material-ui/core';
import { resetProjectState, setProjectModalOpen } from '../redux/actions';
import { makeStyles, RadioGroup, Radio, Grid, Typography, MenuItem } from '@material-ui/core';
import { Title, Description, Label, Link } from '@material-ui/icons';
import { Autocomplete } from '@material-ui/lab';
import { DJANGO_CONTEXT } from '../../../utils/djangoContext';
import { InputFieldAvatar } from './inputFieldAvatar';
import { ProjectCreationType } from '../redux/constants';
import { Formik, Form } from 'formik';
import { TextField, Select } from 'formik-material-ui';
import { Button } from '../../common/Inputs/Button';

const useStyles = makeStyles(theme => ({
body: {
Expand Down Expand Up @@ -39,22 +41,21 @@ export const ProjectModal = memo(({}) => {
setValue(event.target.value);
};

const handleCloseModal = () => dispatch(setProjectModalOpen(false));

const [chipData, setChipData] = React.useState(['Custom tag']);

const handleDelete = chipToDelete => () => {
setChipData(chips => chips.filter(chip => chip.key !== chipToDelete.key));
const handleCloseModal = () => {
dispatch(resetProjectState());
dispatch(setProjectModalOpen(false));
};

const [selectedTags, setSelectedTags] = React.useState([]);

return (
<Modal open={isProjectModalOpen} onClose={handleCloseModal}>
<Typography variant="h3">Create project</Typography>
<Formik
initialValues={{
title: '',
description: '',
target: undefined,
target: '',
tags: []
}}
validate={values => {
Expand Down Expand Up @@ -124,32 +125,46 @@ export const ProjectModal = memo(({}) => {
<InputFieldAvatar
icon={<Label />}
field={
<TextField
className={classes.input}
name="tags"
label="Tags"
onKeyPress={e => {
if (e.key === 'Enter') {
e.preventDefault();
setChipData([...chipData, e.target.value]);
}
<Autocomplete
multiple
freeSolo
id="tags-standard"
options={selectedTags}
getOptionLabel={option => option}
onChange={(e, data) => {
setSelectedTags(data);
}}
renderInput={params => (
<TextField
{...params}
className={classes.input}
name="tags"
label="Tags"
fullWidth
onKeyPress={e => {
if (e.key === 'Enter') {
setSelectedTags([...selectedTags, e.target.value]);
}
}}
/>
)}
/>
}
/>
</Grid>
</Grid>
<Grid container justify="flex-end" direction="row">
<Grid item>
<Button color="secondary" disabled={isSubmitting} onClick={handleCloseModal}>
Cancel
</Button>
</Grid>
<Grid item>
<InputFieldAvatar
icon={<Link style={{ opacity: 0 }} />}
field={chipData.map((data, index) => (
<Chip key={index} label={data} onDelete={handleDelete(data)} />
))}
/>
<Button color="primary" disabled={isSubmitting} onClick={submitForm}>
Create
</Button>
</Grid>
</Grid>
<Button variant="contained" color="primary" disabled={isSubmitting} onClick={submitForm}>
Submit
</Button>
</Form>
)}
</Formik>
Expand Down
2 changes: 1 addition & 1 deletion js/components/projects/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const setCurrentProjectProperty = (key, value) => ({
});

export const resetProjectState = () => ({
type: constants.RESET_PROJECT_STATE
type: constants.RESET_CURRENT_PROJECT_STATE
});

export const setProjectModalOpen = isOpen => ({
Expand Down
2 changes: 1 addition & 1 deletion js/components/projects/redux/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const prefix = 'PROJECTS_';
export const constants = {
SET_CURRENT_PROJECT: prefix + 'SET_CURRENT_PROJECT',
SET_CURRENT_PROJECT_PROPERTY: prefix + 'SET_CURRENT_PROJECT_PROPERTY',
RESET_PROJECT_STATE: prefix + 'RESET_PROJECT_STATE',
RESET_CURRENT_PROJECT_STATE: prefix + 'RESET_CURRENT_PROJECT_STATE',
SET_PROJECT_MODAL_OPEN: prefix + 'SET_PROJECT_MODAL_OPEN'
};

Expand Down
6 changes: 3 additions & 3 deletions js/components/projects/redux/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export const projectReducers = (state = INITIAL_STATE, action = {}) => {

return Object.assign({}, state, { currentProject: currProject });

case constants.RESET_PROJECT_STATE:
const initState = JSON.parse(JSON.stringify(INITIAL_STATE));
return Object.assign({}, state, initState);
case constants.RESET_CURRENT_PROJECT_STATE:
const currProj = JSON.parse(JSON.stringify(INITIAL_STATE.currentProject));
return Object.assign({}, state, { currProject: currProj });

case constants.SET_PROJECT_MODAL_OPEN:
return Object.assign({}, state, { isProjectModalOpen: action.payload });
Expand Down

0 comments on commit a2d95c0

Please sign in to comment.