feat(crud): initial commit

This commit is contained in:
dancingCycle 2022-06-09 14:29:12 +02:00
parent e27bae66a8
commit 497db065d3
15 changed files with 486 additions and 4 deletions

6
crud/.babelrc Normal file
View File

@ -0,0 +1,6 @@
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
]
}

108
crud/.gitignore vendored Normal file
View File

@ -0,0 +1,108 @@
# Others
package-lock.json
build*
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# Next.js build output
.next
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port

11
crud/README.md Normal file
View File

@ -0,0 +1,11 @@
# React.js Example
## Table of Contents
0. [General](#general)
1. [Links](#links)
# General
# Links
* [React setup with webpack for beginners](https://dev.to/deepanjangh/react-setup-with-webpack-for-beginners-2a8k)

35
crud/package.json Normal file
View File

@ -0,0 +1,35 @@
{
"private": true,
"name": "react-example",
"description": "React.js example",
"version": "0.0.1",
"main": "index.js",
"keywords": [
"react",
"webpack"
],
"author": "Software Ingenieur Begerad <dialog@SwIngBe.de>",
"license": "GPL-3.0-or-later",
"engines": {
"node": ">=10"
},
"scripts": {
"start": "webpack serve --config ./webpack.config.js --mode development"
},
"devDependencies": {
"@babel/core": "^7.18.2",
"@babel/preset-env": "^7.18.2",
"@babel/preset-react": "^7.17.12",
"babel-loader": "^8.2.5",
"webpack": "^5.73.0",
"webpack-cli": "^4.9.2",
"webpack-dev-server": "^4.9.2"
},
"dependencies": {
"prop-types": "^15.8.1",
"react": "^18.1.0",
"react-bootstrap": "^2.4.0",
"react-dom": "^18.1.0",
"react-router-dom": "^6.3.0"
}
}

10
crud/public/index.html Normal file
View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>React Example</title>
</head>
<body>
<div id="root"></div>
<script src="./bundle.js"></script>
</body>
</html>

22
crud/src/app.js Normal file
View File

@ -0,0 +1,22 @@
import React from 'react'
import { BrowserRouter as Router,Route, Routes }
from 'react-router-dom';
import Create from './components/create';
import Edit from './components/edit';
import Home from './components/home';
function App() {
return (
<div className='App'>
<Router>
<Routes>
<Route path='/' element={<Home/>}/>
<Route path='/create' element={<Create/>}/>
<Route path='/edit' element={<Edit/>}/>
</Routes>
</Router>
</div>
);
}
export default App;

View File

@ -0,0 +1,20 @@
// Javascript object named array with 3 key-values
const array = [
{
id:'1',
Name:'Shivansh',
Age:'23'
},
{
id:'2',
Name:'Simran',
Age:'22'
},
{
id:'3',
Name:'Aakash',
Age:'23'
}
]
export default array

View File

@ -0,0 +1,77 @@
import React, { useState } from 'react'
import { Button, Form } from 'react-bootstrap'
//TODO import 'bootstrap/dist/css/bootstrap.min.css';
import array from './array';
import { v4 as uuid } from 'uuid';
import { Link, useNavigate } from 'react-router-dom';
function Create() {
// Making usestate for setting and
// fetching a value in jsx
const [name, setname] = useState('');
const [age, setage] = useState('');
// Using useNavigation for redirecting to pages
let history = useNavigate();
// Function for creating a post/entry
const handelSubmit = (e) =>{
e.preventDefault(); // Prevent reload
const ids = uuid() // Creating unique id
let uni = ids.slice(0,8) // Slicing unique id
// Fetching a value from usestate and
// pushing to javascript object
let a = name, b=age
array.push({id:uni,Name:a,Age:b})
// Redirecting to home page after creation done
history('/')
}
return (
<div >
<Form className="d-grid gap-2" style={{margin:'15rem'}}>
{/* Fetching a value from input textfirld
in a setname using usestate*/}
<Form.Group className="mb-3" controlId="formBasicName">
<Form.Control onChange={e => setname(e.target.value)}
type="text"
placeholder="Enter Name" required/>
</Form.Group>
{/* Fetching a value from input textfirld in
a setage using usestate*/}
<Form.Group className="mb-3" controlId="formBasicAge">
<Form.Control onChange={e => setage(e.target.value)}
type="text"
placeholder="Age" required/>
</Form.Group>
{/* handing a onclick event in button for
firing a function */}
<Button
onClick={e => handelSubmit(e)}
variant="primary" type="submit">
Submit
</Button>
{/* Redirecting back to home page */}
<Link className="d-grid gap-2" to='/'>
<Button variant="info" size="lg">
Home
</Button>
</Link>
</Form>
</div>
)
}
export default Create

View File

@ -0,0 +1,81 @@
import React, { useEffect, useState } from 'react'
import { Button, Form } from 'react-bootstrap';
//TODO import 'bootstrap/dist/css/bootstrap.min.css';
import array from './array';
import { Link} from 'react-router-dom';
import {useNavigate} from 'react-router-dom';
function Edit() {
// Here usestate has been used in order
// to set and get values from the jsx
const [name, setname] = useState('');
const [age, setage] = useState('');
const[id,setid] = useState('');
// used for navigation with logic in javascript
let history = useNavigate()
// getting an index of an entry with an id
var index = array.map(function(e) { return e.id; }).indexOf(id);
// function for handling the edit and
// pushing changes of editing/updating
const handelSubmit = (e) =>{
e.preventDefault(); // preventing from reload
let a = array[index] // getting an index of an array
// putting the value from the input textfield and
// replacing it from existing for updation
a.Name = name
a.Age = age
// redirecting to main page
history('/')
}
// Useeffect take care that page will be rendered only once
useEffect(() => {
setname(localStorage.getItem('Name'))
setage(localStorage.getItem('Age'))
setid(localStorage.getItem('id'))
}, [])
return (
<div>
<Form className="d-grid gap-2" style={{margin:'15rem'}}>
{/* setting a name from the input textfiled */}
<Form.Group className="mb-3" controlId="formBasicEmail">
<Form.Control value={name}
onChange={e => setname(e.target.value)}
type="text" placeholder="Enter Name" />
</Form.Group>
{/* setting a age from the input textfiled */}
<Form.Group className="mb-3" controlId="formBasicPassword">
<Form.Control value={age}
onChange={e => setage(e.target.value)}
type="text" placeholder="Age" />
</Form.Group>
{/* Hadinling an onclick event running an edit logic */}
<Button
onClick={e => handelSubmit(e)}
variant="primary" type="submit" size="lg">
Update
</Button>
{/* Redirecting to main page after editing */}
<Link className="d-grid gap-2" to='/'>
<Button variant="warning" size="lg">Home</Button>
</Link>
</Form>
</div>
)
}
export default Edit

View File

@ -0,0 +1,76 @@
import React from 'react'
import { Button,Table } from 'react-bootstrap'
//TODO import 'bootstrap/dist/css/bootstrap.min.css';
import array from './array';
import { Link, useNavigate } from 'react-router-dom';
function Home() {
let history = useNavigate()
// You may skip this part if you're
// using react-context api or redux
function setID(id,name,age){
localStorage.setItem('id', id);
localStorage.setItem('Name', name);
localStorage.setItem('Age', age);
}
// Deleted function - functionality
// for deleting the entry
function deleted(id){
var index = array.map(function(e) { return e.id; }).indexOf(id);
// deleting the entry with index
array.splice(index,1)
// We need to re-render the page for getting
// the results so redirect to same page.
history('/')
}
return (
<div style={{margin:'10rem'}}>
<Table striped bordered hover size="sm">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
{/* Mapping though every element in the array
and showing the data in the form of table */}
{array.map((item) => {
return(
<tr>
<td>{item.Name}</td>
<td>{item.Age}</td>
{/* getting theid,name, and age for storing these
value in the jsx with onclick event */}
<td><Link to={`/edit`}><Button onClick={(e) =>
setID(item.id,item.Name,item.Age)} variant="info">
Update</Button></Link></td>
{/* Using thr deleted function passing
the id of an entry */}
<td><Button onClick={e => deleted(item.id)}
variant="danger">Delete</Button></td>
</tr>
)})}
</tbody>
</Table>
{/* Button for redirecting to create page for
insertion of values */}
<Link className="d-grid gap-2" to='/create'>
<Button variant="warning" size="lg">Create</Button>
</Link>
</div>
)
}
export default Home

10
crud/src/index.js Normal file
View File

@ -0,0 +1,10 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './app';
//since react 18
import { createRoot } from 'react-dom/client';
//create root container
const root = createRoot(document.getElementById("root"));
//render root app
root.render(<App />);

26
crud/webpack.config.js Normal file
View File

@ -0,0 +1,26 @@
//path is used to resolve properly across the OS
const path = require('path');
module.exports = {
//bundle *.js from this entry point
entry: path.resolve(__dirname, './src/index.js'),
module: {
rules: [
{
//test all *.js using babel-loader
//test all *.jsx (e.g. React.js) using babel-loader
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ['babel-loader'],
}
]
},
resolve: {
extensions: ['*', '.js', '.jsx'],
},
//create output file to be linked to index.html
output: {
path: path.resolve(__dirname, './public'),
filename: 'bundle.js',
},
};

View File

@ -1,4 +1,4 @@
# webpack-react
# React.js Example
## Table of Contents
0. [General](#general)

View File

@ -1,7 +1,7 @@
{
"private": true,
"name": "webpack-react",
"description": "example using webpack for react",
"name": "react-example",
"description": "React.js example",
"version": "0.0.1",
"main": "index.js",
"keywords": [

View File

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>Hello React</title>
<title>React Example</title>
</head>
<body>
<div id="root"></div>