Skip to content

Commit

Permalink
Merge pull request #6 from hendriksen-mark/main
Browse files Browse the repository at this point in the history
Add account
  • Loading branch information
hendriksen-mark authored Mar 5, 2024
2 parents a2f47ee + 4c10c31 commit 09fc4c2
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/containers/TheHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const TheHeader = ({ HOST_IP, showSidebar, setShowSidebar, API_KEY }) => {
};

const handleupdate = (state) => {
if (state == "anyreadytoinstall" || state == "allreadytoinstall") {
if (state === "anyreadytoinstall" || state === "allreadytoinstall") {
axios
.put(`${HOST_IP}/api/${API_KEY}/config`, {
swupdate2: { install: true },
Expand All @@ -83,7 +83,7 @@ const TheHeader = ({ HOST_IP, showSidebar, setShowSidebar, API_KEY }) => {
toast.error(`Error occurred: ${error.message}`);
});
}
if (state == "noupdates" || state == "unknown") {
if (state === "noupdates" || state === "unknown") {
axios
.put(`${HOST_IP}/api/${API_KEY}/config`, {
swupdate2: { checkforupdate: true, install: false },
Expand All @@ -100,25 +100,25 @@ const TheHeader = ({ HOST_IP, showSidebar, setShowSidebar, API_KEY }) => {
}

const getValueState = (state) => {
if (state == "anyreadytoinstall" || state == "allreadytoinstall") {
if (state === "anyreadytoinstall" || state === "allreadytoinstall") {
return "Update available";
}
else if (state == "noupdates" || state == "unknown") {
else if (state === "noupdates" || state === "unknown") {
return "No Update";
}
else if (state == "installing"){
else if (state === "installing"){
return "installing..."
}
}

const getClassState = (state) => {
if (state == "anyreadytoinstall" || state == "allreadytoinstall") {
if (state === "anyreadytoinstall" || state === "allreadytoinstall") {
return "updatebtn";
}
else if (state == "noupdates" || state == "unknown") {
else if (state === "noupdates" || state === "unknown") {
return "checkbtn";
}
else if (state == "installing"){
else if (state === "installing"){
return "installbtn"
}
}
Expand All @@ -139,8 +139,8 @@ const TheHeader = ({ HOST_IP, showSidebar, setShowSidebar, API_KEY }) => {
<form className="add-form" onSubmit={(e) => handleupdate(swstate, e)}>
<input
type="submit"
value={getValueState(swstate, "value")}
className={getClassState(swstate, "className")}
value={getValueState(swstate)}
className={getClassState(swstate)}
/>
</form>
</div>
Expand Down
7 changes: 7 additions & 0 deletions src/containers/TheSidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
FaSignOutAlt,
FaInfoCircle,
FaExclamationTriangle,
FaUser,
} from "react-icons/fa";
import { SiHomeassistant } from "react-icons/si";
import { MdSettingsRemote } from "react-icons/md";
Expand Down Expand Up @@ -123,6 +124,12 @@ const TheSidebar = ({ showSidebar, setShowSidebar, isMobile }) => {
<FaInfoCircle style={{ color: "#722371" }} /> <p>About</p>
</li>
</a>
<a href="#account">
<li className={`${currentElement === "account" ? "active" : ""}`}
onClick={() => itemClicked("account")}>
<FaUser style={{ color: "#00a6ff" }} /> <p>Account</p>
</li>
</a>
<a href="/logout">
<li>
<FaSignOutAlt style={{ color: "#7E7E7E" }} /> <p>Logout</p>
Expand Down
2 changes: 2 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const Bridge = React.lazy(() => import('./views/Bridge'));
const HueBridge = React.lazy(() => import('./views/HueBridge'));
const About = React.lazy(() => import('./views/About'));
const Settings = React.lazy(() => import('./views/Settings'));
const Account = React.lazy(() => import('./views/Account'));

const routes = [
{ path: '/', exact: true, name: 'Groups', component: Groups },
Expand All @@ -29,6 +30,7 @@ const routes = [
{ path: '/tradfri', exact: true, name: 'Tradfri', component: Tradfri },
{ path: '/about', exact: true, name: 'About', component: About },
{ path: '/settings', exact: true, name: 'Settings', component: Settings },
{ path: '/account', exact: true, name: 'Account', component: Account },
];

export default routes;
79 changes: 79 additions & 0 deletions src/views/Account.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { useState, useEffect } from "react";
import axios from "axios";
import { toast } from 'react-hot-toast';

const Account = ({ HOST_IP, API_KEY }) => {
const [email, setEmail] = useState("");
const [pass, setPass] = useState("");
const [pass1, setPass1] = useState("");

useEffect(() => {
axios
.get(`${HOST_IP}/api/${API_KEY}/config/users`)
.then((result) => {
setEmail(Object.keys(result.data)[0]);
})
.catch((error) => {
console.error(error);
toast.error(`Error: ${error.message}`);
});
}, [HOST_IP, API_KEY]);

const onSubmit = (e) => {
if (pass !== pass1) {
console.error("Password not the same");
toast.error("Password not the same");
} else if(pass === "") {
console.error("Password can not be empty");
toast.error("Password can not be empty");
} else if(pass === pass1) {
e.preventDefault();
axios
.put(`${HOST_IP}/api/${API_KEY}/config`, {
users: { [email]: { password: pass } },
})
.then((fetchedData) => {
console.log(fetchedData.data);
toast.success("Successfully saved");
})
.catch((error) => {
console.error(error);
toast.error(`Error: ${error.message}`);
});
}
};

return (
<div className="inner">

<div className="contentContainer">
<div className="headline">Change password for {email}</div>
<form className="add-form" onSubmit={(e) => onSubmit(e)}>
<div className="form-control">
<label>New Password</label>
<input
type="password"
placeholder=""
value={pass}
onChange={(e) => setPass(e.target.value)}
/>
</div>
<div className="form-control">
<label>Confirm Password</label>
<input
type="password"
placeholder=""
value={pass1}
onChange={(e) => setPass1(e.target.value)}
/>
</div>
<div className="form-control">
<input type="submit" value="Save" className="btn btn-block" />
</div>
</form>
</div>
</div>
);
};

export default Account;

0 comments on commit 09fc4c2

Please sign in to comment.