Skip to content

Commit

Permalink
added share option and routes for it
Browse files Browse the repository at this point in the history
  • Loading branch information
ranaanjel committed Dec 5, 2024
1 parent 2932f6f commit eec213f
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 8 deletions.
20 changes: 18 additions & 2 deletions brainly_backend/dist/routes/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,25 @@ userRouter.post("/brain/share", authMiddlewares_1.authMiddleware, function (req,
let { share } = req.body;
let userId = req.userId;
try {
//in case already there then sending the value only
var resultShare = yield db_1.ShareLinkModel.findOne({
userId
});
if (resultShare) {
let URL = (req.protocol + "://" + req.get("host") + "/brain/" + resultShare.hash);
res.json({
URL
});
console.log(URL);
return;
}
if (share) {
const hash = crypto_1.default.createHash("sha256").update(userId).digest("hex");
const link = yield db_1.ShareLinkModel.create({
hash,
userId
});
let URL = (req.protocol + "//:" + req.get("host") + "/brain/" + link.hash);
let URL = (req.protocol + "://" + req.get("host") + "/brain/" + link.hash);
res.json({
URL,
message: "content shared"
Expand Down Expand Up @@ -180,6 +192,7 @@ userRouter.get("/brain/:shareLink", function (req, res) {
let hashValue = yield db_1.ShareLinkModel.findOne({
hash
}).populate("userId");
console.log(hashValue);
if (hashValue == null) {
res.status(404).json({
message: "no such content exist"
Expand All @@ -188,7 +201,10 @@ userRouter.get("/brain/:shareLink", function (req, res) {
}
//@ts-ignore
const username = hashValue.userId.name;
const content = yield db_1.ContentModel.find({ userId });
//@ts-ignore
const userValue = hashValue.userId._id;
const content = yield db_1.ContentModel.find({ userId: userValue });
console.log(username, userValue, content);
res.json({
value: {
username, content
Expand Down
29 changes: 26 additions & 3 deletions brainly_backend/src/routes/userRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,33 @@ userRouter.delete("/content", authMiddleware,async function (req:Request, res:Re
//sharing the particular content i.e all notes, tweets or videos and tags values
userRouter.post("/brain/share", authMiddleware, async function (req:Request, res:Response) {
let {share} : {share:boolean} = req.body;
let userId = req.userId;
let userId = req.userId;
try {

//in case already there then sending the value only

var resultShare = await ShareLinkModel.findOne({
userId
})
if(resultShare) {

let URL = ( req.protocol+"://"+ req.get("host")+"/brain/"+ resultShare.hash)
res.json({
URL
})
console.log(URL)
return;
}



if(share) {
const hash = crypto.createHash("sha256").update(userId as string).digest("hex");
const link = await ShareLinkModel.create({
hash,
userId
})
let URL = ( req.protocol+"//:"+ req.get("host")+"/brain/"+ link.hash)
let URL = ( req.protocol+"://"+ req.get("host")+"/brain/"+ link.hash)

res.json({
URL,
Expand Down Expand Up @@ -192,6 +210,8 @@ userRouter.get("/brain/:shareLink", async function (req:Request, res:Response) {
hash
}).populate("userId");

console.log(hashValue)

if(hashValue == null) {
res.status(404).json({
message:"no such content exist"
Expand All @@ -201,8 +221,11 @@ userRouter.get("/brain/:shareLink", async function (req:Request, res:Response) {

//@ts-ignore
const username = hashValue.userId.name;
const content = await ContentModel.find({userId})
//@ts-ignore
const userValue = hashValue.userId._id;
const content = await ContentModel.find({userId:userValue})

console.log(username, userValue, content)

res.json({
value : {
Expand Down
2 changes: 2 additions & 0 deletions brainly_frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Dashboard from "./pages/dashboard"
import { Signin } from "./pages/signin"
import { Signup } from "./pages/signup"
import ShareDashboard from "./pages/shareDashBoard"
import { BrowserRouter, Routes, Route } from "react-router-dom"

function App() {
Expand All @@ -12,6 +13,7 @@ function App() {
<Route index element={<Signin/>} ></Route>
<Route path="/signup" element={<Signup/>}></Route>
<Route path="/dashboard" element={<Dashboard/>}></Route>
<Route path="/brain/:hashValue" element={<ShareDashboard/>}></Route>
</Routes>
</BrowserRouter>

Expand Down
4 changes: 2 additions & 2 deletions brainly_frontend/src/components/ui/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ let variantStyles:{primary:string, secondary:string} = {
secondary:'bg-cs-purple-200 text-cs-purple-600'
}

let defaultStyles = "px-4 py-2 rounded-sm font-light flex items-center gap-2 h-10 "
let defaultStyles = "px-4 py-2 rounded-sm font-light flex justify-center items-center gap-2 h-10 "

export function Button({variant, text, startIcon, size, handlerClick, loading}:ButtonProps) {
return <button className={`${variantStyles[variant]} ${defaultStyles} ${size == "lg" ?"w-full":""} ${loading ?"opacity-50":""}`} disabled={loading} onClick={handlerClick}>
{startIcon}
{startIcon}
{text}
</button>
}
70 changes: 70 additions & 0 deletions brainly_frontend/src/components/ui/ShareModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { CloseIcon } from "../icons/close"
import { Button } from "./Button"
import axios from "axios";
import { backendURL, brainShareLink, brainShareURL, userContentURL } from "../../config";
import { ClipboarIcon } from "../icons/clipboard";
import { useRef, useState } from "react";

interface AddModalProps {
open:boolean,
onClose?:() => void,
}

export function ShareModal({open, onClose }:AddModalProps) {

let urlReference= useRef<string>(backendURL+brainShareURL);
const [stateURL, setStateURL] = useState(urlReference.current)


const jwt = localStorage.getItem("token");
async function sharePage() {
try {
var responseValue = await axios.post(backendURL + brainShareURL, {
share:true
}, {
headers: {
"Authorization": jwt
}
})
let url = responseValue.data.URL.replace("3000", "5173")
urlReference = url
setStateURL(url)

}catch (err) {
console.log(err)
}

}


return <div>
{ open && <div className="h-screen w-screen bg-opacity-50 bg-slate-800 fixed -top-0 -left-0 z-10 overscroll-contain flex justify-center items-center outside" onClick={function (eObj) {
//@ts-ignore
if (eObj.target.className.includes("outside")){
onClose?.()
}
}}>
<div className="min-h-96 max-h-[60%] w-[30%] bg-white rounded px-6 select-none py-8 modal" >
<div className="flex justify-end select-none cursor-pointer" onClick={onClose}>
<CloseIcon>
</CloseIcon>
</div>
<div className="shareValue font-mono bg-purple-200 text-center p-2 mt-6 text-nowrap overflow-x-scroll" >
URL : {stateURL}
</div>
<div className="flex justify-center gap-4 mt-10">
<Button variant="primary" text="Share" size="lg" handlerClick={function() {
sharePage()
}}></Button>
<Button variant="primary" startIcon={<ClipboarIcon/>} size="lg" text="Copy" handlerClick={function() {
navigator.clipboard.writeText(stateURL).then(m=> {
alert("copied!!")
})
}}></Button>
</div>
</div>
</div>
}
</div>

}
24 changes: 24 additions & 0 deletions brainly_frontend/src/hooks/shareContent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import axios from "axios";
import { useEffect, useState } from "react";
import { Location } from "react-router-dom";
import { backendURL } from "../config";

export function useShareContent(hashValue:Location) {
const [elements, setElements] = useState([])

const pathName = hashValue.pathname;

const URL = backendURL+ "/user"+pathName;
console.log(URL)

useEffect(function() {

axios.get(URL).then(m => {
setElements(m.data.value.content)
console.log(m.data.value.content)
})

},[])

return elements;
}
9 changes: 8 additions & 1 deletion brainly_frontend/src/pages/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useContent } from '../components/ui/useContent'
import { backendURL, userContentURL } from '../config'
import axios from 'axios'
import loadingPNG from "../assets/loading.png"
import { ShareModal } from '../components/ui/ShareModal'

function Dashboard() {

Expand All @@ -35,6 +36,7 @@ import loadingPNG from "../assets/loading.png"
//console.log(content, "empty array")

const [open, setClose] = useState(false)
const [share, setShare] = useState(false)

return <div className="flex">

Expand All @@ -52,7 +54,9 @@ import loadingPNG from "../assets/loading.png"
<Button variant='primary' text='Add Content' startIcon={<PlusIcon/>} handlerClick={function() {
setClose(true)
}}></Button>
<Button variant='secondary' text='Share Brain' startIcon={<ShareIcon/>}></Button>
<Button variant='secondary' text='Share Brain' startIcon={<ShareIcon/>} handlerClick={function() {
setShare(true)
}}></Button>
</div>
</div>
<div className='flex gap-8 flex-wrap justify-start m-auto '>
Expand Down Expand Up @@ -117,6 +121,9 @@ import loadingPNG from "../assets/loading.png"
setClose(false)
}
} />
<ShareModal open={share} onClose={function() {
setShare(false)
}}/>
</div>

</div>
Expand Down
79 changes: 79 additions & 0 deletions brainly_frontend/src/pages/shareDashBoard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import '../App.css'
import { Card, cardProps } from '../components/ui/card'
import brainImage from "../assets/brain.png"
import { backendURL, userContentURL } from '../config'
import axios from 'axios'
import { useShareContent } from '../hooks/shareContent'
import { useLocation } from 'react-router-dom'

function ShareDashboard() {

const url = useLocation()


const content = useShareContent(url);

return <div className="flex mx-auto">

<div className='px-10 flex-1 py-8 max-h-screen space-y-3.5 scroll-smooth overscroll-contain overflow-scroll'>
<div className='flex justify-start gap-2 hover:shadow-xl p-4'>
<div className='flex space-x-3 items-center'>
<img src={brainImage} alt="logo" className='size-10'/>
<span className='text-3xl font-semibold tracking-tighter font-sans'> Brainly</span>
</div>
</div>
<div className='flex gap-8 flex-wrap justify-center m-auto '>
{content.map(function (item:{type:string, tag:{title:string}[], title:string, link:string, _id:string}, index:number) {
console.log(item)
switch (item.type) {
case "video":
item.type = "yt"
break;
case "audio":
item.type = "yt"
break;
case "article":
item.type = "docs"
break;
case "tweet":
item.type = "tw"
break;
case "unknown":
item.type = "unknown"
break;
default:
break;
}
const contentId = (item._id);
return <Card deleteHandler={ async function () {

try {
let deleteResponse = await axios.delete(backendURL+userContentURL, {
data : {
contentId
},
headers :{
"Authorization": localStorage.getItem("token")
}
})
console.log(deleteResponse)
window.location.reload()

}catch (err) {
console.log(err)
}

}} contentId={item._id} tag={item.tag} key={index} type={item.type as cardProps["type"]} title={item.title} link={item.link} />
})}
{content.length ==0 ? <div className='h-96 w-full flex justify-center items-end relative'>
No content
</div> :"" }

</div>

</div>

</div>
}

export default ShareDashboard

0 comments on commit eec213f

Please sign in to comment.