import React, { useState, useEffect } from "react";
import { Form, Button, Row, Col } from "react-bootstrap";
import MainScreen from "../../components/MainScreen";
import "./Kusapi.css";
import { useDispatch, useSelector } from "react-redux";
import { updateProfile } from "../../actions/userActions";
import Loading from "../../components/Loading";
import ErrorMessage from "../../components/ErrorMessage";
const Kusapi = ({ location, history }) => {
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const [pic, setPic] = useState();
const [password, setPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [picMessage, setPicMessage] = useState();
const dispatch = useDispatch();
const userLogin = useSelector((state) => state.userLogin);
const { userInfo } = userLogin;
const userUpdate = useSelector((state) => state.userUpdate);
const { loading, error, success } = userUpdate;
useEffect(() => {
if (!userInfo) {
history.push("/");
} else {
setName(userInfo.name);
setEmail(userInfo.email);
setPic(userInfo.pic);
}
}, [history, userInfo]);
const postDetails = (pics) => {
setPicMessage(null);
if (pics.type === "image/jpeg" || pics.type === "image/png") {
const data = new FormData();
data.append("file", pics);
data.append("upload_preset", "notezipper");
data.append("cloud_name", "piyushproj");
fetch("https://api.cloudinary.com/v1_1/piyushproj/image/upload", {
method: "post",
body: data,
})
.then((res) => res.json())
.then((data) => {
setPic(data.url.toString());
console.log(pic);
})
.catch((err) => {
console.log(err);
});
} else {
return setPicMessage("Please Select an Image");
}
};
const submitHandler = (e) => {
e.preventDefault();
dispatch(updateProfile({ name, email, password, pic }));
};
return (
<MainScreen title="EDIT PROFILE">
<div>
<Row className="profileContainer">
<Col md={6}>
<Form onSubmit={submitHandler}>
{loading && <Loading />}
{success && (
<ErrorMessage variant="success">
Updated Successfully
</ErrorMessage>
)}
{error && <ErrorMessage variant="danger">{error}</ErrorMessage>}
<Form.Group controlId="pic">
<Form.Label>Change Profile Picture</Form.Label>
<Form.File
onChange={(e) => postDetails(e.target.files[0])}
id="custom-file"
type="image/png"
label="Upload Profile Picture"
custom
/>
</Form.Group>
<Button type="submit" varient="primary">
Update
</Button>
</Form>
</Col>
<Col
style={{
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<img src={pic} alt={name} className="profilePic" />
</Col>
</Row>
</div>
</MainScreen>
);
};
export default Kusapi;