Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds transcript form #25

Merged
merged 13 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions packages/components/FormModal/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import Modal from 'react-bootstrap/Modal';
import ItemForm from './ItemForm/index.js';
import ItemForm from '../ItemForm/index.js';
import TranscriptForm from '../TranscriptForm/index.js';

const ItemFormModal = (props) => {


const [ showModal, toggleShowModal ] = useState(props.showModal);

const form = () => {
allishultes marked this conversation as resolved.
Show resolved Hide resolved
if (props.type === 'transcript') {
return (
<TranscriptForm
{ ...props }
/>
);
} else {
return (
<ItemForm
{ ...props }
/>
);
}
};

return (
<Modal show={ showModal } onHide={ () => toggleShowModal(!showModal) }>
<Modal.Header closeButton>
<Modal.Title>{props.modalTitle}</Modal.Title>
</Modal.Header>
<Modal.Body>
<ItemForm
{ ...props }
/>
{form()}
</Modal.Body>
</Modal>
);
Expand Down
25 changes: 15 additions & 10 deletions packages/components/FormModal/stories/index.stories.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import React from 'react';

import { storiesOf } from '@storybook/react';
import { actions } from '@storybook/addon-actions';
import StoryRouter from 'storybook-react-router';
import FormModal from '../index.js';
import ItemForm from '../ItemForm';

export const modalItems = [ {
id: 1,
itemType: 'project',
showModal: true,
title: 'Example Transcript Title',
title: 'Example Project Title',
description: 'This is a sample card description. This is fun!',
url: '/projects/1/transcripts/1234',
modalTitle: 'Edit Project',
}, {
showModal: true,
modalTitle: 'New Project',
id: 2
}, {
projectId: 123,
title: '',
description: '',
uploadCompleted: true,
showModal: true,
modalTitle: 'New Transcript',
id: 3,
type: 'transcript'
} ];

export const modalActions = actions({ handleSaveForm: 'Form saved' });
Expand All @@ -42,16 +50,13 @@ storiesOf('Form Modal', module)
/>
</section>
);
});

storiesOf('Form Modal / Item Form', module)
.addDecorator(StoryRouter())
.add('Edit Project', () => {
})
.add('New Transcript', () => {
return (
<section style={ { height: '90vh', overflow: 'scroll' } }>
<ItemForm
<FormModal
{ ...modalActions }
{ ...modalItems[0] }
{ ...modalItems[2] }
/>
</section>
);
Expand Down
18 changes: 18 additions & 0 deletions packages/components/ItemForm/stories/index.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import { storiesOf } from '@storybook/react';
import StoryRouter from 'storybook-react-router';
import ItemForm from '../index.js';
import { modalActions, modalItems } from '../../FormModal/stories/index.stories.js';

storiesOf('Item Form', module)
.addDecorator(StoryRouter())
.add('Edit Project', () => {
return (
<section style={ { height: '90vh', overflow: 'scroll' } }>
<ItemForm
{ ...modalActions }
{ ...modalItems[0] }
/>
</section>
);
});
1 change: 0 additions & 1 deletion packages/components/List/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import TranscriptCard from '../TranscriptCard';

const List = ({ projectItems, handleEdit, handleDelete }) => {

console.log(projectItems);
const [ items, setItems ] = useState(projectItems);

const includesText = (text, subsetText) => {
Expand Down
48 changes: 48 additions & 0 deletions packages/components/TranscriptForm/CustomAlert/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import Alert from 'react-bootstrap/Alert';

const CustomAlert = ({ ...props } ) => {

const [ showAlert, toggleShowAlert ] = useState(props.show);

const handleDismiss = () => {
toggleShowAlert(false);
};

const setAlertHeading = () => {
return props.heading ? <Alert.Heading>{props.heading}</Alert.Heading> : null;
};

const setAlert = () => {
if (showAlert) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this logic of showAlert should be in the place where it's importing this CustomAlert. I wonder if in storybooks you can add a button to toggle the alert to come up?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh yeah - I think that's the knobs. I'll try and add one :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I'm not sure knobs work as expected with functional components — I've managed to allow users to toggle a button to turn the alert on and off, but the component doesn't re-render when the props change using the add-on.

I have separated the CustomAlert from the TranscriptCard; I think you're right in that that will be handled by the parent component.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The showAlert should probably be a state of the parent component 🤔 which also implies that maybe the actual handling functions should also be in the parent component.

return (
<Alert
variant={ props.variant }
onClose={ handleDismiss }
dismissible
>
{setAlertHeading()}
{props.message}
</Alert>
);
} else {
return null;
}
};

return setAlert();
};

CustomAlert.propTypes = {
show: PropTypes.bool.isRequired,
heading: PropTypes.string,
message: PropTypes.string,
vatiant: PropTypes.string,
};

CustomAlert.defaultProps = {
show: true,
};

export default CustomAlert;
164 changes: 164 additions & 0 deletions packages/components/TranscriptForm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import Form from 'react-bootstrap/Form';
import Button from 'react-bootstrap/Button';
import Modal from 'react-bootstrap/Modal';
import CustomAlert from './CustomAlert';

const TranscriptForm = ({ ...props }) => {

const [ title, setTitle ] = useState(props.title);
const [ description, setDescription ] = useState(props.description);
const [ isValidated, setValidationStatus ] = useState(false);
const [ formData, setFormData ] = useState({});
const [ uploadCompleted, setUploadCompletion ] = useState(props.uploadCompleted);
const [ notificationMessage, updateNotificationMessage ] = useState(props.notification);

useEffect(() => {
if (!props.uploadCompleted) {
const alert = <CustomAlert
show={ true }
dismissable={ true }
variant={ 'danger' }
heading={ 'Error could not contact the server' }
message={ <p>There was an error trying to create this transcript on the server</p> }
/>;
updateNotificationMessage(alert);
}
}, [ uploadCompleted ]);

const handleFileUpload = e => {
const file = e.target.files[0];

if (!title) {
setTitle(file.name);
}

const fileObj = {
title: title,
description: description,
file: file,
type: file.type
};

if (file.path) {
allishultes marked this conversation as resolved.
Show resolved Hide resolved
fileObj.path = file.path;
}
setFormData(fileObj);
};

const sendRequest = () => {
const tmpObj = {
...formData,
title: title,
description: description
};

setFormData(tmpObj);

props.handleSaveForm(tmpObj);
};

const handleSubmit = (event) => {
const form = event.currentTarget;
event.preventDefault();
event.stopPropagation();

if (!form.checkValidity()) {
setValidationStatus(true);
}
else {
setValidationStatus(true);

allishultes marked this conversation as resolved.
Show resolved Hide resolved
sendRequest();
}
};

return (<>
{ notificationMessage }
<Form
noValidate
validated={ isValidated }
onSubmit={ e => handleSubmit(e) }
>
<Form.Group controlId="formTranscriptTitle">
<Form.Label>Title</Form.Label>
<Form.Control
required
type="text"
placeholder="Enter a transcript title"
value={ title }
onChange={ e => setTitle(e.target.value) }
/>
<Form.Text className="text-muted">
Chose a title for your Transcript
allishultes marked this conversation as resolved.
Show resolved Hide resolved
</Form.Text>
<Form.Control.Feedback>Looks good!</Form.Control.Feedback>
<Form.Control.Feedback type="invalid">
Please chose a title for your transcript
allishultes marked this conversation as resolved.
Show resolved Hide resolved
</Form.Control.Feedback>
</Form.Group>

<Form.Group controlId="formTranscriptDescription">
<Form.Label>Description</Form.Label>
<Form.Control
type="text"
placeholder="Enter a Transcript description"
value={ description }
onChange={ e => setDescription(e.target.value) }
/>
<Form.Text className="text-muted">
Chose an optional description for your Transcript
allishultes marked this conversation as resolved.
Show resolved Hide resolved
</Form.Text>
<Form.Control.Feedback>Looks good!</Form.Control.Feedback>
<Form.Control.Feedback type="invalid">
Please chose a description for your transcript
allishultes marked this conversation as resolved.
Show resolved Hide resolved
</Form.Control.Feedback>
</Form.Group>
<Form.Group controlId="formTranscriptMediaFile">
<Form.Control
required
type="file"
label="Upload"
accept="audio/*,video/*"
onChange={ e => handleFileUpload(e) }
/>
<Form.Text className="text-muted">
Select an audio or video file to upload
</Form.Text>
<Form.Control.Feedback>Looks good!</Form.Control.Feedback>
<Form.Control.Feedback type="invalid">
Please chose a audio or video file to upload
allishultes marked this conversation as resolved.
Show resolved Hide resolved
</Form.Control.Feedback>
</Form.Group>
<Modal.Footer>
<Button variant="primary" type="submit">
Save
</Button>
</Modal.Footer>
</Form>
</>
);
};

TranscriptForm.propTypes = {
id: PropTypes.number.isRequired,
projectId: PropTypes.number.isRequired,
title: PropTypes.string.isRequired,
description: PropTypes.string,
uploadCompleted: PropTypes.bool.isRequired,
handleSubmitForm: PropTypes.func.isRequired,
};

TranscriptForm.defaultProps = {
id: 456,
projectId: 123,
title: 'Sample Transcript Title',
description: 'Sample Transcript Description',
uploadCompleted: true,
handleSaveForm: () => {
console.log('Form saved');
},
};

export default TranscriptForm;
Loading