Skip to content

Commit

Permalink
reward points are added for certain actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
vedang8 committed Mar 8, 2024
1 parent 14297f9 commit d10d071
Show file tree
Hide file tree
Showing 13 changed files with 467 additions and 371 deletions.
2 changes: 0 additions & 2 deletions client/src/pages/CarbonCredits/Bids.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ function Bids({ showBidsModal, setShowBidsModal, selectedSellCredit }) {
if (response.ok) {
const data = await response.json();
console.log("dataaa", data);
message.success("🪙5 Credits are rewarded! 🎊");

setBidsData(data.data);
}
} catch (error) {
Expand Down
52 changes: 43 additions & 9 deletions client/src/pages/CarbonCredits/CCredits.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function Carbon_Credits() {
const fetchCredits = async () => {
try {
setLoading(true);
dispatch(SetLoader(true))
dispatch(SetLoader(true));
const token = localStorage.getItem("usersdatatoken");
const creditsResponse = await fetch("/get-credits/user", {
method: "GET",
Expand All @@ -38,8 +38,15 @@ function Carbon_Credits() {
});

setCredits(updatedCredits);
const activeApprovedCredits = updatedCredits.filter(credit => credit.status === "Active");
setTotalCredits(activeApprovedCredits.reduce((total, credit) => total + credit.amount, 0));
const activeApprovedCredits = updatedCredits.filter(
(credit) => credit.status === "Active"
);
setTotalCredits(
activeApprovedCredits.reduce(
(total, credit) => total + credit.amount,
0
)
);

const rewardCreditsResponse = await fetch("/get-reward-credits-user", {
method: "GET",
Expand All @@ -65,15 +72,17 @@ function Carbon_Credits() {

useEffect(() => {
fetchCredits();

updateCredits();
const interval = setInterval(fetchCredits, 60000);

return () => clearInterval(interval);
}, []);

const updateCredits = async () => {
try {
const expiredCredits = credits.filter((credit) => credit.status === "Expired");
const expiredCredits = credits.filter(
(credit) => credit.status === "Expired"
);

if (expiredCredits.length > 0) {
const token = localStorage.getItem("usersdatatoken");
Expand All @@ -89,7 +98,7 @@ function Carbon_Credits() {
});

if (response.ok) {
console.log("Credits status updated in the database");
console.log("Credits status updated");
} else {
throw new Error("Failed to update credits status in the database");
}
Expand Down Expand Up @@ -123,7 +132,17 @@ function Carbon_Credits() {
title: "Project Name",
dataIndex: "projectName",
key: "projectName",
render: (text) => <span style={{ fontSize: '16px', color: "#652A0E", backgroundColor: "#FFF39A"}}>{text}</span>
render: (text) => (
<span
style={{
fontSize: "16px",
color: "#652A0E",
backgroundColor: "#FFF39A",
}}
>
{text}
</span>
),
},
{
title: "Credit Amount",
Expand All @@ -134,15 +153,30 @@ function Carbon_Credits() {
title: "Status",
dataIndex: "status",
key: "status",
render: (text, record) => <span style={{ fontSize: '16px', backgroundColor: record.status === "Expired" ? "#FF7F50" :"greenyellow", color: record.status === "Expired" ? "#FF7F50" :"123524"}}>{text}</span>
render: (text, record) => (
<span
style={{
fontSize: "16px",
backgroundColor:
record.status === "Expired" ? "#FF7F50" : "greenyellow",
color: record.status === "Expired" ? "#FF7F50" : "123524",
}}
>
{text}
</span>
),
},
{
title: "Remaining Time",
dataIndex: "expiryDate",
key: "expiryDate",
render: (expiryDate, record) => {
const { status, remainingTime } = calculateTimeLeft(expiryDate);
return status === "Active" ? <CountdownTimer targetDate={remainingTime} /> : <span>{record.status}</span>;
return status === "Active" ? (
<CountdownTimer targetDate={remainingTime} />
) : (
<span>{record.status}</span>
);
},
},
];
Expand Down
1 change: 0 additions & 1 deletion client/src/pages/CarbonCredits/Credits.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ function Credits() {
const data = await res.json();
console.log(data);
message.success(data.message);
message.success("🪙5 Credits are rewarded! 🎊")
getData();
} else {
console.error("Error deleting form");
Expand Down
3 changes: 1 addition & 2 deletions client/src/pages/CarbonCredits/CreditsForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ const CreditsForm = ({ setShowCreditsForm, selectedCredit, getData, editMode, ha
const data = await res.json();
//console.log(data);
message.success(data.message);
message.success("🪙5 Credits are rewarded! 🎊")
getData(); // Update the data after successful edit
setShowCreditsForm(false);
}else{
Expand All @@ -163,7 +162,7 @@ const CreditsForm = ({ setShowCreditsForm, selectedCredit, getData, editMode, ha
baselineEmissionAmount: "", numOfTrees: "", numOfSolarPanels: "", electricity: "", status: "", images: []});
await getData();
message.success(data.message);
message.success("🪙50 Credits are rewarded! 🎊")
message.success("🪙50 points are rewarded! 🎊")
setShowCreditsForm(false);
} else {
message.error('Error submitting the form. Please try again.');
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/CarbonCredits/SellCreditsForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const SellCreditsForm = ({
});
await getData();
message.success(data.message);
message.success("🪙25 Credits are rewarded! 🎊")
message.success("🪙25 Points are rewarded! 🎊")
setShowSellCreditsForm(false);
} else {
message.error("Error submitting the form. Please try again.");
Expand Down
5 changes: 2 additions & 3 deletions client/src/pages/Dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ const Dashboard = () => {
Authorization: token,
},
});
if(resnotify.ok){
if (resnotify.ok) {
const responseData = await resnotify.json();
setNotifications(responseData.data);
}else{
} else {
throw new Error(resnotify.message);
}
} catch (error) {
Expand Down Expand Up @@ -109,7 +109,6 @@ const Dashboard = () => {
localStorage.removeItem("usersdatatoken");
setLoginData(false);
message.success("Logged out Successfully");
message.success("🪙 1 Credit rewarded! 🎊");
navigate("/login");
}
};
Expand Down
204 changes: 111 additions & 93 deletions client/src/pages/Login.js
Original file line number Diff line number Diff line change
@@ -1,104 +1,122 @@
import React, {useState} from 'react'
import { NavLink } from 'react-router-dom';
import { useNavigate } from 'react-router-dom';
import "./mix.css"
import { useDispatch } from 'react-redux';
import { SetLoader } from '../redux/loadersSlice';
import { message } from 'antd';
import React, { useState } from "react";
import { NavLink } from "react-router-dom";
import { useNavigate } from "react-router-dom";
import "./mix.css";
import { useDispatch } from "react-redux";
import { SetLoader } from "../redux/loadersSlice";
import { message } from "antd";

const Login = () => {
const [passShow, setPassShow] = useState(false);

const [passShow, setPassShow] = useState(false);

const [inpval, setInpval] = useState({
email:"",
password:""
const [inpval, setInpval] = useState({
email: "",
password: "",
});
const navigate = useNavigate();
const dispatch = useDispatch();
const setVal = (e) => {
const { name, value } = e.target;
setInpval(() => {
return {
...inpval,
[name]: value,
};
});
const navigate = useNavigate();
const dispatch = useDispatch();
const setVal = (e) =>{

const {name, value} = e.target;
setInpval(()=>{
return{
...inpval,
[name]:value
}
};

const loginuser = async (e) => {
e.preventDefault();
const { email, password } = inpval;

if (email === "") {
message.error("Please enter your email");
} else if (!email.includes("@gmail.com")) {
message.error("Please enter a valid email");
} else if (password === "") {
message.error("Enter your password");
} else if (password.length < 6) {
message.error("Password must be at least 6 characters");
} else {
dispatch(SetLoader(true));
try {
const data = await fetch("/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ email, password }),
});
};


const loginuser = async (e) => {
e.preventDefault();
const { email, password } = inpval;

if (email === "") {
message.error("Please enter your email");
} else if (!email.includes("@gmail.com")) {
message.error("Please enter a valid email");
} else if (password === "") {
message.error("Enter your password");
} else if (password.length < 6) {
message.error("Password must be at least 6 characters");
const res = await data.json();
dispatch(SetLoader(false));
console.log(res);
if (res.status === 201) {
localStorage.setItem("usersdatatoken", res.result.token);
message.success("Welcome to CARBON CREDIT MARKETPLACE");
navigate("/home");
setInpval({ ...inpval, email: "", password: "" });
} else if (res.error === "User account is blocked") {
message.error("Your account is blocked!!!");
} else {
dispatch(SetLoader(true));
try {
const data = await fetch("/login", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ email, password })
});
const res = await data.json();
dispatch(SetLoader(false));
console.log(res);
if (res.status === 201) {
localStorage.setItem("usersdatatoken", res.result.token);
message.success("Welcome to CARBON CREDIT MARKETPLACE");
message.success("🪙 2 Credits are rewarded! 🎊")
navigate("/home");
setInpval({ ...inpval, email: "", password: "" });
} else if (res.error === "User account is blocked") {
message.error("Your account is blocked!!!");
} else {
message.error("Invalid Details");
}
} catch (error) {
dispatch(SetLoader(false));
message.error("An error occurred. Please try again later.");
console.error("Login error:", error);
}
message.error("Invalid Details");
}
};

return (
} catch (error) {
dispatch(SetLoader(false));
message.error("An error occurred. Please try again later.");
console.error("Login error:", error);
}
}
};

return (
<>
<section>
<div className='form_data'>
<div className='form_heading'>
<h1>Welcome Back, Log In</h1>
<section>
<div className="form_data">
<div className="form_heading">
<h1>Welcome Back, Log In</h1>
</div>
<form>
<div className="form_input">
<label htmlFor="email">Email</label>
<input
type="email"
onChange={setVal}
value={inpval.email}
name="email"
id="email"
placeholder="Enter Your Email Address"
/>
</div>
<div className="form_input">
<label htmlFor="password">Password</label>
<div className="two">
<input
type={!passShow ? "password" : "text"}
onChange={setVal}
value={inpval.password}
name="password"
id="password"
placeholder="Enter Your Password"
/>
<div
className="showpass"
onClick={() => setPassShow(!passShow)}
>
{!passShow ? "Show" : "Hide"}
</div>
<form>
<div className='form_input'>
<label htmlFor="email">Email</label>
<input type="email" onChange={setVal} value={inpval.email} name="email" id="email" placeholder='Enter Your Email Address' />
</div>
<div className='form_input'>
<label htmlFor="password">Password</label>
<div className="two">
<input type={!passShow ? "password" : "text"} onChange={setVal} value={inpval.password} name="password" id="password" placeholder='Enter Your Password' />
<div className="showpass" onClick={()=>setPassShow(!passShow)}>
{!passShow ? "Show" : "Hide"}
</div>
</div>
</div>
<button className='btn' onClick={loginuser}>Login</button>
<p>Don't have an Account? <NavLink to="/register">Sign Up</NavLink></p>
</form>
</div>
</div>
</section>
<button className="btn" onClick={loginuser}>
Login
</button>
<p>
Don't have an Account? <NavLink to="/register">Sign Up</NavLink>
</p>
</form>
</div>
</section>
</>
)
}
);
};

export default Login
export default Login;
Loading

0 comments on commit d10d071

Please sign in to comment.