0

I am currently building a simple React application. I have some data passed down to a window.example object, from a laravel application. I would then like to access the data, and i have found out i can do that initially with props. But the problem is that when i submit a form, in AssignmentForm Component, i want to update the data in the AssignmentBox, which shows the data, and add a row of data. How would i do that?

So my structure looks like this:

  • DashboardApp
    • AssignmentBox
    • AssignmentFormNew

This is my code:

main.js:

import swal from 'sweetalert';
import $ from 'jquery';
import React from 'react';
import { render } from 'react-dom';
import DashboardApp from './components/Dashboard';

render(
    <DashboardApp tracks={window.tracks} assignments={window.assignments} />,
    document.getElementById('content')
);

Dashboard.js:

import React from 'react';
import {Grid, Row, Col} from 'react-bootstrap';
import TrackBox from './TrackBox';
import TrackFormStop from './TrackFormStop';
import TrackFormNew from './TrackFormNew';
import AssignmentBox from './AssignmentBox';
import AssignmentFormNew from './AssignmentFormNew';

class DashboardApp extends React.Component {
    render () {
        return (
            <Grid>
                <Row>
                    <Col md={12}>
                        <AssignmentBox assignments={this.props.assignments} />
                        <AssignmentFormNew />
                    </Col>
                </Row>
            </Grid>
        )
    }
}

export default DashboardApp;

AssignmentBox.js

import React from 'react';
import {Panel} from 'react-bootstrap';

const title = (
  <h2>Current Assignments</h2>
);

class AssignmentBox extends React.Component {
    render () {
        return (
            <Panel header={title}>
                <ul>
                    {this.props.assignments.map(item => (
                        <li key={item.id}><a href="assignment/{item.slug}">{item.title}</a></li>
                    ))}
                </ul>
            </Panel>
        )
    }
}

export default AssignmentBox;

AssignmentFormNew.js

import React from 'react';
import {Panel, Button, FormGroup, ControlLabel} from 'react-bootstrap';
import $ from 'jquery';

const title = (
    <h2>New Assignment</h2>
);

const footer = (
    <Button bsStyle="primary" type="submit" block>New Assignment</Button>
);

class AssignmentFormNew extends React.Component {
    constructor (props) {
        super(props);
        this.state = {
            title: '',
            description: '',
            customer: '',
            date: ''
        };

        this.handleSubmit = this.handleSubmit.bind(this);

        this.handleTitleChange = this.handleTitleChange.bind(this);
        this.handleDescriptionChange = this.handleDescriptionChange.bind(this);
        this.handleCustomerChange = this.handleCustomerChange.bind(this);
        this.handleDeadlineChange = this.handleDeadlineChange.bind(this);
    }

    handleSubmit (e) {
        e.preventDefault();

        console.log(window.Laravel.csrfToken);

        $.ajax({
            url: '/assignment',
            type: 'POST',
            data: {
                _token: window.Laravel.csrfToken,
                title: this.refs.title.value,
                description: this.refs.description.value,
                customer: this.refs.customer.value,
                deadline: this.refs.deadline.value
            },
            success: res => {
                this.setState({
                    title: '',
                    description: '',
                    customer: '',
                    deadline: ''
                });

                console.log(res);
            },
            error: (xhr, status, err) => {
                console.error(status, err.toString());
            }
        });
    }

    handleTitleChange (event) {
        this.setState({title: event.target.value});
    }

    handleDescriptionChange (event) {
        this.setState({description: event.target.value});
    }

    handleCustomerChange (event) {
        this.setState({customer: event.target.value});
    }

    handleDeadlineChange (event) {
        this.setState({deadline: event.target.value});
    }

    render () {
        return (
            <form onSubmit={this.handleSubmit}>
                <Panel header={title} footer={footer}>
                    <FormGroup controlId="assignmentTitle">
                        <ControlLabel>Title</ControlLabel>
                        <input type="text" ref="title" placeholder="e.g. Crowdfunding module for prestashop" className="form-control" value={this.state.title} onChange={this.handleTitleChange} />
                    </FormGroup>
                    <FormGroup controlId="assignmentDescription">
                        <ControlLabel>Description</ControlLabel>
                        <textarea className="form-control" ref="description" rows="10" value={this.state.description} onChange={this.handleDescriptionChange} />
                    </FormGroup>
                    <FormGroup controlId="assignmentCustomer">
                        <ControlLabel>Customer</ControlLabel>
                        <input type="text" placeholder="e.g. John Doe" ref="customer" className="form-control" value={this.state.customer} onChange={this.handleCustomerChange} />
                    </FormGroup>
                    <FormGroup controlId="assignmentDeadline">
                        <ControlLabel>Deadline</ControlLabel>
                        <input type="date" className="form-control" ref="deadline" value={this.state.deadline} onChange={this.handleDeadlineChange} />
                    </FormGroup>
                </Panel>
            </form>
        )
    }
}

export default AssignmentFormNew;

Thank you in advance.

3 Answers 3

1

Put your handleSubmit() function in Dashboard.js, and add the following code

 constructor (props) {
        super(props);
        this.state = {
            assignments:this.props.assignments
        };
}

handleSubmit (e) {
  ... your ajax code
  this.setState({assignments:res})
}

<AssignmentBox assignments={this.state.assignments} handleSubmit={this.handleSubmit}/>

Then in AssignmentFormNew.js change:

<form onSubmit={this.props.handleSubmit}>

Basically when you click submit, it call parent's handleSubmit function in Dashboard.js, then after your ajax call, update the state so the AssignmentBox will re-render it with new data.

Sign up to request clarification or add additional context in comments.

Comments

0

Create a method in assignment box to update the data and pass that function as a prop to the assignment form. Call the function within assignment form and pass the data.

Comments

0

in AssignmentFormNew.js

handleSubmit (e) {
    e.preventDefault();
    this.props.childValuesToParent(e);  
  .....
}

now this props is available inside parent - Dashboard.js like this

<AssignmentFormNew childValuesToParent={this.handleChange} />

somewhat like call back.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.