Skip to content

Commit

Permalink
fixed: added firebase-realtime-database
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemir123 committed Nov 16, 2022
1 parent bf2a7bb commit fc568be
Show file tree
Hide file tree
Showing 14 changed files with 290 additions and 56 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@
"tailwindcss": "^3.2.4"
}
}

Binary file added src/asset/blog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/asset/konu.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 22 additions & 13 deletions src/auth/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@ import {
toastWarnNotify,
} from "../helper/Toastfy";
const firebaseConfig = {
apiKey: "AIzaSyDjSI_ROzIdejIheZ2twAYT46njNDS-hKg",
authDomain: "redux-app-85e9c.firebaseapp.com",
projectId: "redux-app-85e9c",
storageBucket: "redux-app-85e9c.appspot.com",
messagingSenderId: "777502188729",
appId: "1:777502188729:web:3ac62475f62cc342db2768",
apiKey: "AIzaSyAlHwfxnhNTqs0HjN9Dvch6hF2c3PJrfKs",
authDomain: "firedata-redux.firebaseapp.com",
databaseURL: "https://firedata-redux-default-rtdb.firebaseio.com",
projectId: "firedata-redux",
storageBucket: "firedata-redux.appspot.com",
messagingSenderId: "904564410585",
appId: "1:904564410585:web:370d932af74c09d86256c8"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
const firebase = initializeApp(firebaseConfig);
export default firebase;
export const auth = getAuth(firebase);
export const provider = new GoogleAuthProvider();

export const createUser = async (email, password, navigate, displayName) => {
export const createUser = async (email, password, navigate, displayName, dispatch) => {
try {
let userCredential = await createUserWithEmailAndPassword(
auth,
Expand All @@ -40,8 +41,16 @@ export const createUser = async (email, password, navigate, displayName) => {
await updateProfile(auth.currentUser, {
displayName: displayName,
});
dispatch(
setUser({
username: displayName,
email: email,
// password: password,
})
);

// SweetAlertsRegister();
navigate("/home");
navigate("/");
console.log(userCredential);
} catch (error) {
// SweetAlertsError(error);
Expand Down Expand Up @@ -86,7 +95,7 @@ export const signIn = async (username, email, password, navigate, dispatch) => {
password: password,
})
);
navigate("/home");
navigate("/");
toastSuccessNotify("Login successfully!");
} catch (error) {
toastErrorNotify(error.message);
Expand All @@ -105,7 +114,7 @@ export const signUpProvider = (navigate, dispatch) => {
// photoUrl: user.photoUrl,
})
);
navigate("/home");
navigate("/");
toastSuccessNotify("Login successfully!!");
})
.catch((error) => {
Expand Down
42 changes: 42 additions & 0 deletions src/auth/functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
getDatabase,
ref,
set,
push,
onValue,
remove,
update,
} from "firebase/database";
import { useEffect, useState } from "react";
import firebase from "./firebase";

export const useFetch = () => {
const [isLoading, setIsLoading] = useState(true);
const [cardList, setCardList] = useState();
useEffect(() => {
const db = getDatabase(firebase);
const userRef = ref(db, "user/");
onValue(userRef, (snapshot) => {
const data = snapshot.val();
const userArray = [];
for (let id in data) {
userArray.push({ id, ...data[id] });
}
setCardList(userArray);
setIsLoading(false);
});
}, []);
return { isLoading, cardList };
};
export const AddUser=(info,user,history)=>{
const db=getDatabase(firebase)
const userRef=ref(db,"user/")
const newUserRef=push(userRef)
set(newUserRef,{
Title:info.title,
ImgUrl:info.imgUrl,
content:info.content,
email:user?.email,
history:history
})
}
34 changes: 34 additions & 0 deletions src/conponents/Card.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react'
import { useNavigate } from 'react-router-dom'
import konu from "../asset/konu.jpg"

const Card = ({item}) => {
const navigate =useNavigate()
return (
<>
{
<div className="rounded-lg shadow-lg bg-white max-w-sm w-[350px] h-[350px]">
<div className='w-[90%] h-36'>
{item.ImgUrl ? (<img className="rounded-t-lg w-48 mx-auto" src={item.ImgUrl} alt="" />) : (<img className="rounded-t-lg w-48 mx-auto" src={konu} alt="" />)}
</div>
<div className="p-6">
<h5 className="text-gray-900 text-xl font-medium mb-2">{item.Title}</h5>
<p className="text-gray-700 text-base mb-4 w-[90%] text-ellipsis overflow-hidden">
{item.content}
</p>
<p className="text-gray-700 text-base mb-4 w-[90%] text-ellipsis overflow-hidden">
{item.history}
</p>
<p className="text-gray-700 text-base mb-4 w-[90%] text-ellipsis overflow-hidden">
{item.email}
</p>
<button type="button" className=" inline-block px-6 py-2.5 bg-blue-600 text-white font-medium text-xs leading-tight uppercase rounded shadow-md hover:bg-blue-700 hover:shadow-lg focus:bg-blue-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-blue-800 active:shadow-lg transition duration-150 ease-in-out" onClick={()=>navigate(`/${item?.Title}`,{state:item})}>Button</button>
</div>
<div><svg width="32" height="32" viewBox="0 0 36 36"><path fill="currentColor" d="M18 32.43a1 1 0 0 1-.61-.21C11.83 27.9 8 24.18 5.32 20.51C1.9 15.82 1.12 11.49 3 7.64c1.34-2.75 5.19-5 9.69-3.69A9.87 9.87 0 0 1 18 7.72a9.87 9.87 0 0 1 5.31-3.77c4.49-1.29 8.35.94 9.69 3.69c1.88 3.85 1.1 8.18-2.32 12.87c-2.68 3.67-6.51 7.39-12.07 11.71a1 1 0 0 1-.61.21ZM10.13 5.58A5.9 5.9 0 0 0 4.8 8.51c-1.55 3.18-.85 6.72 2.14 10.81A57.13 57.13 0 0 0 18 30.16a57.13 57.13 0 0 0 11.06-10.83c3-4.1 3.69-7.64 2.14-10.81c-1-2-4-3.59-7.34-2.65a8 8 0 0 0-4.94 4.2a1 1 0 0 1-1.85 0a7.93 7.93 0 0 0-4.94-4.2a7.31 7.31 0 0 0-2-.29Z" class="clr-i-outline clr-i-outline-path-1"/><path fill="none" d="M0 0h36v36H0z"/></svg></div>
</div>
}
</>
)
}

export default Card
27 changes: 18 additions & 9 deletions src/conponents/Navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import avatar from "../asset/avatar.png"
const Navbar = () => {
const navigate = useNavigate()
const dispatch = useDispatch()
const {user} = useSelector((state)=>state)
const {user} = useSelector((state)=>state.auth)
// console.log(user)
return (
<>
<nav className="w-full flex flex-wrap items-center justify-between py-3 bg-white dark:bg-gray-900 dark:text-white shadow-lg navbar navbar-expand-lg fixed-top">
Expand All @@ -20,7 +21,7 @@ const Navbar = () => {
<div className="flex items-center relative">
{/* Icon */}
{user && (
<h5 className="mr-2 capitalize">{user?.displayName}</h5>
<h5 className="mr-2 capitalize">{user.username}</h5>
)}
<div className="dropdown relative">
<span
Expand All @@ -43,38 +44,46 @@ const Navbar = () => {
className="dropdown-menu min-w-max absolute bg-white text-base z-50 float-left py-2 list-none text-left rounded-lg shadow-lg mt-1 hidden m-0 bg-clip-padding border-none left-auto right-0"
aria-labelledby="dropdownMenuButton2"
>
<li>
{!user?.email && <li>
<Link
className="dropdown-item text-sm py-2 px-4 font-normal block w-full whitespace-nowrap bg-transparent text-gray-700 hover:bg-gray-100"
to="/register"
>
Register
</Link>
</li>
<li>
</li>}
{!user?.email && <li>
<Link
className="dropdown-item text-sm py-2 px-4 font-normal block w-full whitespace-nowrap bg-transparent text-gray-700 hover:bg-gray-100"
to="/login"
>
Login
</Link>
</li>
<li>
</li>}
{user?.email && <li>
<Link
className="dropdown-item text-sm py-2 px-4 font-normal block w-full whitespace-nowrap bg-transparent text-gray-700 hover:bg-gray-100"
to="/new"
>
New
</Link>
</li>}
{user?.email && <li>
<span
className="dropdown-item text-sm py-2 px-4 font-normal block w-full whitespace-nowrap bg-transparent text-gray-700 hover:bg-gray-100"
role="button"
onClick={() => logOut(navigate,dispatch)}
>
Logout
</span>
</li>
</li>}
</ul>
</div>
</div>
{/* Right elements */}
</div>
</nav>
<div className="h-[52px]"></div>
<div className="h-[80px]"></div>
</>
);
};
Expand Down
39 changes: 39 additions & 0 deletions src/pages/Details.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react'
import { useSelector } from 'react-redux'
import { useLocation } from 'react-router-dom'
import konu from "../asset/konu.jpg"

const Details = () => {
const {state} = useLocation()
const {user} =useSelector((state)=>state.auth)
console.log(user?.email);
return (
<div>
{
<div className="rounded-lg shadow-lg bg-white max-w-sm w-[350px] h-[350px]">
<div className='w-[90%] h-36'>
{state.ImgUrl ? (<img className="rounded-t-lg w-48 mx-auto" src={state.ImgUrl} alt="" />) : (<img className="rounded-t-lg w-48 mx-auto" src={konu} alt="" />)}
</div>
<div className="p-6">
<h5 className="text-gray-900 text-xl font-medium mb-2">{state.Title}</h5>
<p className="text-gray-700 text-base mb-4 w-[90%] text-ellipsis overflow-hidden">
{state.content}
</p>
<p className="text-gray-700 text-base mb-4 w-[90%] text-ellipsis overflow-hidden">
{state.history}
</p>
<p className="text-gray-700 text-base mb-4 w-[90%] text-ellipsis overflow-hidden">
{state.email}
</p>

</div>
<div><svg width="32" height="32" viewBox="0 0 36 36"><path fill="currentColor" d="M18 32.43a1 1 0 0 1-.61-.21C11.83 27.9 8 24.18 5.32 20.51C1.9 15.82 1.12 11.49 3 7.64c1.34-2.75 5.19-5 9.69-3.69A9.87 9.87 0 0 1 18 7.72a9.87 9.87 0 0 1 5.31-3.77c4.49-1.29 8.35.94 9.69 3.69c1.88 3.85 1.1 8.18-2.32 12.87c-2.68 3.67-6.51 7.39-12.07 11.71a1 1 0 0 1-.61.21ZM10.13 5.58A5.9 5.9 0 0 0 4.8 8.51c-1.55 3.18-.85 6.72 2.14 10.81A57.13 57.13 0 0 0 18 30.16a57.13 57.13 0 0 0 11.06-10.83c3-4.1 3.69-7.64 2.14-10.81c-1-2-4-3.59-7.34-2.65a8 8 0 0 0-4.94 4.2a1 1 0 0 1-1.85 0a7.93 7.93 0 0 0-4.94-4.2a7.31 7.31 0 0 0-2-.29Z" class="clr-i-outline clr-i-outline-path-1"/><path fill="none" d="M0 0h36v36H0z"/></svg></div>

</div>
}
{(user?.email === state?.email) ? (<button>sadık</button>) :""}
</div>
)
}

export default Details
24 changes: 12 additions & 12 deletions src/pages/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import React from 'react'
import { useDispatch } from 'react-redux'
import { useNavigate } from 'react-router-dom'
import { ToastContainer } from 'react-toastify'
import { logOut } from '../auth/firebase'
import Navbar from '../conponents/Navbar'
import React from "react";
import Navbar from "../conponents/Navbar";
import { useFetch } from "../auth/functions";
import Card from "../conponents/Card";

const Home = () => {
const dispatch = useDispatch()
const navigate = useNavigate()
const { isLoading, cardList } = useFetch();
console.log(cardList);
return (
<div>
{/* <button onClick={()=>logOut(navigate,dispatch)}>sadık</button> */}
<Navbar />
<div className="flex flex-wrap gap-6 justify-center items-center">
{cardList?.map((item,index)=><Card item={item} key={index}/>)}
</div>
</div>
)
}
);
};

export default Home
export default Home;
98 changes: 98 additions & 0 deletions src/pages/New.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import React, { useEffect, useState } from "react";
import Navbar from "../conponents/Navbar";
import blog from "../asset/blog.png";
import { AddUser, useFetch } from "../auth/functions";
import { Navigate, useNavigate } from "react-router-dom";
import { toastWarnNotify } from "../helper/Toastfy";
import { useSelector } from "react-redux";

const New = () => {
const navigate = useNavigate();
const initialValues = {
title: "",
imgUrl: "",
content: "",
};
const {user} = useSelector((state)=>state.auth)
const [info, setInfo] = useState(initialValues);
const [count, setCount] = useState();




const handleSubmit = (e) => {
e.preventDefault();
let asd = new Date()
let date = new Date(`${asd.getFullYear()}-${asd.getMonth()+1}-${asd.getDate()}`)
let history = date.toLocaleDateString("tr-TR",{
weekday:"long",month:"long",day:"numeric",year:"numeric"
})
if(info.title && info.content && info.imgUrl){
if(count?.length>2){
AddUser(info,user,history);
setInfo(initialValues);
navigate("/");
}
else{
toastWarnNotify("Contente minimum 100 harf yazılmalıdır")
}

}
else{
toastWarnNotify("Form Boş Bırakılamaz")
}
};

const { isLoading, cardList } = useFetch();
useEffect(() => {
setCount(info.content.split(""));
}, [info]);

return (
<div>
<Navbar />
<form onSubmit={handleSubmit}>
<div className="flex flex-col justify-center items-center mt-20 gap-4">
<img src={blog} alt="" />
<input
type="text"
placeholder="Title *"
className="w-[400px] border-4 outline-none py-2 indent-2 shadow-md shadow-black rounded-md"
value={info.title}
onChange={(e) => setInfo({ ...info, title: e.target.value })}
/>
<input
type="text"
placeholder="Image URL *
"
className="w-[400px] border-4 outline-none py-2 indent-2 shadow-md shadow-black rounded-md"
value={info.imgUrl}
onChange={(e) => setInfo({ ...info, imgUrl: e.target.value })}
/>
<div className="relative">
<textarea
name=""
id=""
cols="30"
rows="10"
className=" w-[400px] h-[300px] border-4 outline-none py-2 indent-2 shadow-md shadow-black rounded-md"
placeholder="Content *"
value={info.content}
onChange={(e) => setInfo({ ...info, content: e.target.value })}
wrap="hard"
></textarea>

<p className="absolute bottom-4 right-4">
{`3000/ ${count?.length}`}
</p>
</div>
</div>
<button className="py-2 px-4 bg-teal-600 block mx-auto mt-2 rounded-lg text-white font-bold hover:bg-teal-400 hover:text-black duration-200">
Submit
</button>
</form>
</div>
);
};

export default New;
Loading

0 comments on commit fc568be

Please sign in to comment.