AdonisJS + React Hooks – CRUD API REST #2 Views component

AdonisJS

Controller

adonis make:controller Customer --type http

app/Controllers/Http/CustomerController.js

'use strict'

class CustomerController {
 
  test(){
    return "Hello world";
  }

  index ({ view }) {
    const message = "Developed by Artyom"
    return view.render('customer',{ message })
  }

}

module.exports = CustomerController

Route

Route.get("customer","CustomerController.index");
Route.get("customer/index","CustomerController.index");
Route.get("customer/list","CustomerController.index");
Route.get("customer/form","CustomerController.index");
Route.get("customer/edit/:id?","CustomerController.index");

View

adonis make:view customer

resources/views/customer.edge

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>Tutofox.com</title>
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
	<style> .navbar{margin-bottom: 20px;} </style>
</head>
<body>
<div class="container" style="padding:20px;">
	<h1 style="text-align:center;">
		<a href="/customer"> Full Stack - Adonis & React Hooks </a>
	</h1>
	<hr>

  <div id="component-main"></div>
  <script src="/dist/app.js"></script>

	<hr>

	<center>
		<p>{{message}}</p>
	</center>

</div>
</body>
</html>

React JS

React router

npm install react-router-dom

Components

Nav

resources/js/components/Nav.js

import React from 'react';
import { Link } from "react-router-dom";

function Nav(){
  return (
    <nav class="navbar navbar-expand-lg navbar-light bg-light rounded">
      <div class="collapse navbar-collapse" id="navbarsExample09">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item">
            <Link class="nav-link" to="/customer/index">List </Link>
          </li>
          <li class="nav-item">
            <Link class="nav-link" to="/customer/form">Create</Link>
          </li>
          <li class="nav-item">
            <Link class="nav-link" to="/customer/edit/5">Edit</Link>
          </li>
        </ul>
      </div>
    </nav>
  )
}


export default Nav;

Main

resources/js/components/Main.js

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

import Nav from "./Nav"
import Form from "./customer/Form"
import List from "./customer/List"
import Edit from "./customer/Edit"

import {
  BrowserRouter as Router,
  Switch,
  Route
} from "react-router-dom";

function Main(){
  return (
    <Router>
      <main>
        <Nav/>
        <Switch>
          <Route path="/customer/index" exact component={List} />
          <Route path="/customer/form"  component={Form} />
          <Route path="/customer/edit/:id" component={Edit} />
        </Switch>
      </main>
    </Router>
  )
}


export default Main;
ReactDOM.render(<Main />, document.getElementById('component-main'));

Form

resources/js/components/customer/Form.js

import React, { useEffect, useState  } from 'react';

function Form(){
  return(
    <div>
      <h4>Form customer</h4>
      <hr/>
      <div class="row">
        <div class="col-md-6 mb-3">
          <label for="firstName">Name</label>
          <input type="text" class="form-control" placeholder="Name" />
        </div>
      </div>

      <div class="row">
        <div class="col-md-6 mb-3">
          <label for="email">Email</label>
          <input type="email" class="form-control" placeholder="you@example.com" />
        </div>
      </div>

      <div class="row">
        <div class="col-md-6 mb-3">
          <label for="address">Address</label>
          <input type="text" class="form-control" placeholder="1234 Main St" />
        </div>
      </div>

      <div class="row">
        <div class="col-md-6 mb-3">
          <label for="phone">Phone </label>
          <input type="text" class="form-control" placeholder="123467890" />
        </div>
      </div>

      <div class="row">
        <div class="col-md-6 mb-3">
          <button class="btn btn-primary btn-block" type="submit">Save</button>
        </div>
      </div>
    </div>
  )
}

export default Form;

Edit

resources/js/components/customer/Edit.js

import React, { useEffect, useState  } from 'react';

function Edit(props){

  let userId = props.match.params.id;

  return (
    <div>
      <h4>Edit customer  {userId}  </h4>
      <hr/>
      <div class="row">
        <div class="col-md-6 mb-3">
          <label for="firstName">Name</label>
          <input type="text" class="form-control" placeholder="Name"/>
        </div>
      </div>

      <div class="row">
        <div class="col-md-6 mb-3">
          <label for="email">Email</label>
          <input type="email" class="form-control" placeholder="you@example.com" />
        </div>
      </div>

      <div class="row">
        <div class="col-md-6 mb-3">
          <label for="address">Address</label>
          <input type="text" class="form-control" placeholder="1234 Main St" />
        </div>
      </div>

      <div class="row">
        <div class="col-md-6 mb-3">
          <label for="address">Phone </label>
          <input type="text" class="form-control" placeholder="123467890" />
        </div>
      </div>

      <div class="row">
        <div class="col-md-6 mb-3">
          <button class="btn btn-primary btn-block" type="submit">Save</button>
        </div>
      </div>
    </div>
  )

}

export default Edit;

List

resources/js/components/customer/List.js

import React, { useEffect, useState  } from 'react';

function List(){
  return (
    <section>
      <table class="table">
        <thead class="thead-dark table-bordered">
          <tr>
            <th scope="col">#</th>
            <th scope="col">Name</th>
            <th scope="col">Email</th>
            <th scope="col">Address</th>
            <th scope="col">Phone</th>
            <th scope="col">Action</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <th scope="row">1</th>
            <td>John Doe</td>
            <td>john@example.com</td>
            <td>California Cll 100</td>
            <td>3101111111</td>
            <td>
              <a href="#" class="btn btn-light"> Edit </a>
              <a href="#" class="btn btn-danger"> Delete </a>
            </td>
          </tr>
          <tr>
            <th scope="row">2</th>
            <td>John Doe</td>
            <td>john@example.com</td>
            <td>California Cll 100</td>
            <td>3101111111</td>
            <td>
              <a href="#" class="btn btn-light"> Edit </a>
              <a href="#" class="btn btn-danger"> Delete </a>
            </td>
          </tr>
          <tr>
            <th scope="row">3</th>
            <td>John Doe</td>
            <td>john@example.com</td>
            <td>California Cll 100</td>
            <td>3101111111</td>
            <td>
              <a href="#" class="btn btn-light"> Edit </a>
              <a href="#" class="btn btn-danger"> Delete </a>
            </td>
          </tr>
        </tbody>
      </table>
    </section>
  )
}

export default List;

Run Webpack

sudo webpack --watch 

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *