Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Github Task 2 completed #1

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 35 additions & 18 deletions src/components/Tutorials/NewTutorial/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useState, useRef } from "react";
import { AppstoreAddOutlined } from "@ant-design/icons";
import { useDispatch, useSelector } from "react-redux";
import { createTutorial } from "../../../store/actions";
Expand All @@ -19,7 +19,6 @@ import DescriptionIcon from "@mui/icons-material/Description";
import MovieIcon from "@mui/icons-material/Movie";
import Select from "react-select";
import { common } from "@mui/material/colors";

const useStyles = makeStyles(theme => ({
root: {
display: "flex",
Expand All @@ -34,7 +33,6 @@ const useStyles = makeStyles(theme => ({
backgroundColor: deepPurple[500]
}
}));

const NewTutorial = ({ viewModal, onSidebarClick, viewCallback, active }) => {
const firebase = useFirebase();
const firestore = useFirestore();
Expand All @@ -43,11 +41,13 @@ const NewTutorial = ({ viewModal, onSidebarClick, viewCallback, active }) => {
const [visible, setVisible] = useState(false);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(false);
const [imageIconClicked,setImageIconClicked]=useState(false)
const [formValue, setformValue] = useState({
title: "",
summary: "",
owner: ""
});
const [tutorialImage, setTutorialImage] = useState(null);

const loadingProp = useSelector(
({
Expand All @@ -63,43 +63,36 @@ const NewTutorial = ({ viewModal, onSidebarClick, viewCallback, active }) => {
}
}) => error
);

useEffect(() => {
setLoading(loadingProp);
}, [loadingProp]);

useEffect(() => {
setError(errorProp);
}, [errorProp]);

const organizations = useSelector(
({
profile: {
data: { organizations }
}
}) => organizations
);

const userHandle = useSelector(
({
firebase: {
profile: { handle }
}
}) => handle
);

const displayName = useSelector(
({
firebase: {
profile: { displayName }
}
}) => displayName
);

//This name should be replaced by displayName when implementing backend
const sampleName = "User Name Here";
const allowOrgs = organizations && organizations.length > 0;

const orgList =
allowOrgs > 0
? organizations
Expand All @@ -115,20 +108,21 @@ const NewTutorial = ({ viewModal, onSidebarClick, viewCallback, active }) => {

useEffect(() => {
setVisible(viewModal);
setImageIconClicked(false)
}, [viewModal]);

const onSubmit = formData => {
formData.preventDefault();
const tutorialData = {
...formValue,
tutorialImage: tutorialImage,
created_by: userHandle,
is_org: userHandle !== formValue.owner,
completed: false
};
console.log(tutorialData);
createTutorial(tutorialData)(firebase, firestore, dispatch, history);
};

const onOwnerChange = value => {
setformValue(prev => ({
...prev,
Expand All @@ -138,13 +132,33 @@ const NewTutorial = ({ viewModal, onSidebarClick, viewCallback, active }) => {

const handleChange = e => {
const { name, value } = e.target;

setformValue(prev => ({
...prev,
[name]: value
}));
};

const fileInputRef = useRef(null);

const handleIconClick = () => {
fileInputRef.current.click();
};

const handleFileChange = event => {
setImageIconClicked(true)
const selectedFile = event.target.files[0];

if (selectedFile) {
const reader = new FileReader();

reader.onloadend = () => {
const base64Result = reader.result;
setTutorialImage(base64Result);
};
reader.readAsDataURL(selectedFile);
}
};

const classes = useStyles();
return (
<Modal
Expand Down Expand Up @@ -194,7 +208,6 @@ const NewTutorial = ({ viewModal, onSidebarClick, viewCallback, active }) => {
/>
</Typography>
</Box>

<form id="tutorialNewForm">
<TextField
prefix={
Expand All @@ -210,7 +223,6 @@ const NewTutorial = ({ viewModal, onSidebarClick, viewCallback, active }) => {
style={{ marginBottom: "2rem" }}
onChange={e => handleChange(e)}
/>

<TextField
prefix={
<AppstoreAddOutlined style={{ color: "rgba(0,0,0,.25)" }} />
Expand All @@ -227,15 +239,21 @@ const NewTutorial = ({ viewModal, onSidebarClick, viewCallback, active }) => {
/>

<IconButton>
<ImageIcon />
<ImageIcon onClick={handleIconClick} sx={{color : imageIconClicked ? '#1876d3' : ''}} />
<input
type="file"
accept="image/png, image/jpeg"
ref={fileInputRef}
style={{ display: "none" }}
onChange={handleFileChange}
/>
</IconButton>
<IconButton>
<MovieIcon />
</IconButton>
<IconButton>
<DescriptionIcon />
</IconButton>

<div className="mb-0">
<div style={{ float: "right" }}>
<Button
Expand Down Expand Up @@ -277,5 +295,4 @@ const NewTutorial = ({ viewModal, onSidebarClick, viewCallback, active }) => {
</Modal>
);
};

export default NewTutorial;
export default NewTutorial;
Loading