Skip to content

Commit

Permalink
crud app with db.json
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseebUllahAbbasi committed Jun 3, 2021
1 parent 3457c41 commit 9e1058b
Show file tree
Hide file tree
Showing 25 changed files with 28,422 additions and 0 deletions.
104 changes: 104 additions & 0 deletions week_11/Crud_App/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# 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
21 changes: 21 additions & 0 deletions week_11/Crud_App/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Haseeb Ullah

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
74 changes: 74 additions & 0 deletions week_11/Crud_App/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#Crud App
### Step 1
First Of all Make Clone the https://github.com/HaseebUllahAbbasi/temp_react repository
then Make the Following Pages Simple with headings with title Name of Pages
Home
About
Contact
Then Add the Navbar from the bootstrap
Now use the NavLink from the (react-router-dom)
Example :
<NavLink className="nav-link" exact to="/about">
About
</NavLink>


#### Home Page :
First make the use of the local database using the json server
and and the data to the json file , like i have placed in the project in db.json then check the server
using the json-server --watch db.json --port 3003 // port can be different
then add the concurrent library to the app using the yarn or npm.
Use the useEffect method and call the funtion to load the data from the json file :: method should be sync so that data is fully loaded

const loadUsers = async () => {
const result = await axios.get("http://localhost:3003/users"); // calling api using axois
setUser(result.data);

use the UseState Method([]) then set method to use the user instance and store data in it
make the table and use tbody to populate the data
use the td as data.attribute
Use the map function with (value, index)
<td>{user.name}</td>
Deleting on Home Page
<Link class="btn btn-danger" onClick={() => deleteUser(user.id)} > Delete </Link>
where
const deleteUser = async id => {
await axios.delete(`http://localhost:3003/users/${id}`);
loadUsers();
}
For Editing Use the Reference of the id and get the data use the api and edit data on new page
<Link class="btn btn-outline-success mr-1" to={`/users/edit/${user.id}`} >

For View
<Link class="btn btn-info mr-1" to={`/users/${user.id}`}> View </Link>

#### Add new User
Placed BUtton in nav bar use the to instead of exact to
<Link className="btn btn-outline-light" to="/users/add">Add New User</Link>
where in the add user file
Use useHistory from the react-router-dom and useState and init the state to be written in from
write the onchange funtion and use the spread operator and use the setUser method to chage the value of the user at form input change

for inputs in forms
<div className="form-group">
<input
type="text"
className="form-control form-control-lg"
placeholder="Enter Your Name"
name="name"
value={name}
onChange={e => onInputChange(e)}
/>
</div>
calling inputChange method in every input
Form Submit
<form onSubmit={e => onSubmit(e)}>
Use the method
e.preventDefault();
then use the code to push the data on the db.json
await axios.post("http://localhost:3003/users", user);
history.push("/");

### Soon , more will be added
3 changes: 3 additions & 0 deletions week_11/Crud_App/db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"users": []
}
Loading

0 comments on commit 9e1058b

Please sign in to comment.