-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df736c3
commit f9906ca
Showing
10 changed files
with
510 additions
and
25 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { configureStore } from '@reduxjs/toolkit' | ||
import userSlice from "./../users/userSlice" | ||
|
||
export const store = configureStore({ | ||
reducer: { | ||
user: userSlice | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React, { useState } from "react"; | ||
import { useSelector, useDispatch } from "react-redux"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { addUser } from "./../users/userSlice"; | ||
|
||
function AddUser() { | ||
const [name, setname] = useState(""); | ||
const [address, setaddress] = useState(""); | ||
|
||
const users = useSelector((state) => state.user.user); | ||
const dispatch = useDispatch(); | ||
const navigate = useNavigate(); | ||
|
||
const id = users.length; | ||
return ( | ||
<> | ||
<h5>User Name</h5> | ||
<input | ||
type="text" | ||
value={name} | ||
onChange={(e) => { | ||
setname(e.target.value); | ||
}} | ||
/> | ||
|
||
<h5>User Address</h5> | ||
<input | ||
type="text" | ||
value={address} | ||
onChange={(e) => { | ||
setaddress(e.target.value); | ||
}} | ||
/> | ||
<br /> | ||
<br /> | ||
<button | ||
onClick={() => { | ||
dispatch(addUser({ id, name, address })); | ||
navigate("/view-users"); | ||
}} | ||
> | ||
Add User | ||
</button> | ||
</> | ||
); | ||
} | ||
|
||
export default AddUser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from "react"; | ||
import { Link } from "react-router-dom"; | ||
import { useSelector } from "react-redux"; | ||
|
||
function Home() { | ||
const users = useSelector((state) => state.user.user); | ||
console.log(users); | ||
return ( | ||
<> | ||
<h1>Heliverse Task 1</h1> | ||
<h4>Number of Users : {users.length}</h4> | ||
<Link to="/view-users"> | ||
<button>View Users</button> | ||
</Link> | ||
</> | ||
); | ||
} | ||
|
||
export default Home; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React, { useState } from "react"; | ||
import { useDispatch } from "react-redux"; | ||
import { useNavigate, useParams } from "react-router-dom"; | ||
import { editUser } from "./../users/userSlice"; | ||
|
||
function UpdateUser() { | ||
const [name, setname] = useState(""); | ||
const [address, setaddress] = useState(""); | ||
|
||
const dispatch = useDispatch(); | ||
const navigate = useNavigate(); | ||
let { id } = useParams(); | ||
|
||
return ( | ||
<> | ||
<h5>User Name</h5> | ||
<input | ||
type="text" | ||
value={name} | ||
onChange={(e) => { | ||
setname(e.target.value); | ||
}} | ||
/> | ||
|
||
<h5>User Address</h5> | ||
<input | ||
type="text" | ||
value={address} | ||
onChange={(e) => { | ||
setaddress(e.target.value); | ||
}} | ||
/> | ||
<br /> | ||
<br /> | ||
<button | ||
onClick={() => { | ||
dispatch(editUser({ id, name, address })); | ||
navigate("/view-users"); | ||
}} | ||
> | ||
Update User | ||
</button> | ||
</> | ||
); | ||
} | ||
|
||
export default UpdateUser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from "react"; | ||
import { Link } from "react-router-dom"; | ||
import { useSelector, useDispatch } from "react-redux"; | ||
import { removeUser } from "./../users/userSlice"; | ||
|
||
function ViewUser() { | ||
const users = useSelector((state) => state.user.user); | ||
const dispatch = useDispatch(); | ||
|
||
return ( | ||
<> | ||
<h1>My Users</h1> | ||
<h4>Number of Users : {users.length}</h4> | ||
<Link to="/add-users"> | ||
<button>Add New Users</button> | ||
</Link> | ||
<br /> | ||
{/* Mapping the Users */} | ||
<div> | ||
<span> | ||
<b>ID </b> | ||
<b>Name </b> | ||
<b>Address </b> | ||
<b>Operations </b> | ||
</span> | ||
{users.map((item, index) => { | ||
return ( | ||
<div | ||
key={item.id} | ||
style={{ display: "flex", alignItems: "center", gap: "0.4rem" }} | ||
> | ||
<p>{item.id}</p> | ||
<p>{item.name}</p> | ||
<p>{item.address}</p> | ||
<div> | ||
<Link to={"/update-users/" + index}> | ||
<button>Update</button> | ||
</Link> | ||
<button onClick={() => dispatch(removeUser(index))}> | ||
Delete | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
export default ViewUser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { createSlice } from "@reduxjs/toolkit"; | ||
|
||
const initialState = { | ||
user: [ | ||
{ | ||
id: 0, | ||
name: "Anmol Garg", | ||
address: "Saraswati Nagar", | ||
}, | ||
{ | ||
id: 1, | ||
name: "Amandeep Singh", | ||
address: "Station", | ||
}, | ||
], | ||
}; | ||
|
||
export const userSlice = createSlice({ | ||
name: "users", | ||
initialState, | ||
reducers: { | ||
addUser: (state, action) => { | ||
state.user.push(action.payload); | ||
}, | ||
removeUser: (state, action) => { | ||
state.user.splice(action.payload, 1); | ||
}, | ||
editUser: (state, action) => { | ||
state.user[action.payload.id] = action.payload; | ||
}, | ||
}, | ||
}); | ||
|
||
// Action creators are generated for each case reducer function | ||
export const { addUser, removeUser, editUser } = userSlice.actions; | ||
|
||
export default userSlice.reducer; |