-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3rd part completed - connecting frontend and backend
1 parent
7aadf4f
commit cecf505
Showing
28 changed files
with
734 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,18 @@ | ||
module.exports = { | ||
root: true, | ||
env: { browser: true, es2020: true }, | ||
extends: [ | ||
'eslint:recommended', | ||
'plugin:react/recommended', | ||
'plugin:react/jsx-runtime', | ||
'plugin:react-hooks/recommended', | ||
], | ||
ignorePatterns: ['dist', '.eslintrc.cjs'], | ||
parserOptions: { ecmaVersion: 'latest', sourceType: 'module' }, | ||
settings: { react: { version: '18.2' } }, | ||
plugins: ['react-refresh'], | ||
rules: { | ||
'react-refresh/only-export-components': [ | ||
'warn', | ||
{ allowConstantExport: true }, | ||
], | ||
}, | ||
} | ||
root: true, | ||
env: { browser: true, es2020: true }, | ||
extends: [ | ||
"eslint:recommended", | ||
"plugin:react/recommended", | ||
"plugin:react/jsx-runtime", | ||
"plugin:react-hooks/recommended", | ||
], | ||
ignorePatterns: ["dist", ".eslintrc.cjs"], | ||
parserOptions: { ecmaVersion: "latest", sourceType: "module" }, | ||
settings: { react: { version: "18.2" } }, | ||
plugins: ["react-refresh"], | ||
rules: { | ||
"react-refresh/only-export-components": ["warn", { allowConstantExport: true }], | ||
"react/prop-types": "off", | ||
}, | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const MessageSkeleton = () => { | ||
return ( | ||
<> | ||
<div className='flex gap-3 items-center'> | ||
<div className='skeleton w-10 h-10 rounded-full shrink-0'></div> | ||
<div className='flex flex-col gap-1'> | ||
<div className='skeleton h-4 w-40'></div> | ||
<div className='skeleton h-4 w-40'></div> | ||
</div> | ||
</div> | ||
<div className='flex gap-3 items-center justify-end'> | ||
<div className='flex flex-col gap-1'> | ||
<div className='skeleton h-4 w-40'></div> | ||
</div> | ||
<div className='skeleton w-10 h-10 rounded-full shrink-0'></div> | ||
</div> | ||
</> | ||
); | ||
}; | ||
export default MessageSkeleton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { createContext, useContext, useState } from "react"; | ||
|
||
export const AuthContext = createContext(); | ||
|
||
// eslint-disable-next-line react-refresh/only-export-components | ||
export const useAuthContext = () => { | ||
return useContext(AuthContext); | ||
}; | ||
|
||
export const AuthContextProvider = ({ children }) => { | ||
const [authUser, setAuthUser] = useState(JSON.parse(localStorage.getItem("chat-user")) || null); | ||
|
||
return <AuthContext.Provider value={{ authUser, setAuthUser }}>{children}</AuthContext.Provider>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useEffect, useState } from "react"; | ||
import toast from "react-hot-toast"; | ||
|
||
const useGetConversations = () => { | ||
const [loading, setLoading] = useState(false); | ||
const [conversations, setConversations] = useState([]); | ||
|
||
useEffect(() => { | ||
const getConversations = async () => { | ||
setLoading(true); | ||
try { | ||
const res = await fetch("/api/users"); | ||
const data = await res.json(); | ||
if (data.error) { | ||
throw new Error(data.error); | ||
} | ||
setConversations(data); | ||
} catch (error) { | ||
toast.error(error.message); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
getConversations(); | ||
}, []); | ||
|
||
return { loading, conversations }; | ||
}; | ||
export default useGetConversations; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useEffect, useState } from "react"; | ||
import useConversation from "../zustand/useConversation"; | ||
import toast from "react-hot-toast"; | ||
|
||
const useGetMessages = () => { | ||
const [loading, setLoading] = useState(false); | ||
const { messages, setMessages, selectedConversation } = useConversation(); | ||
|
||
useEffect(() => { | ||
const getMessages = async () => { | ||
setLoading(true); | ||
try { | ||
const res = await fetch(`/api/messages/${selectedConversation._id}`); | ||
const data = await res.json(); | ||
if (data.error) throw new Error(data.error); | ||
setMessages(data); | ||
} catch (error) { | ||
toast.error(error.message); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
if (selectedConversation?._id) getMessages(); | ||
}, [selectedConversation?._id, setMessages]); | ||
|
||
return { messages, loading }; | ||
}; | ||
export default useGetMessages; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { useState } from "react"; | ||
import toast from "react-hot-toast"; | ||
import { useAuthContext } from "../context/AuthContext"; | ||
|
||
const useLogin = () => { | ||
const [loading, setLoading] = useState(false); | ||
const { setAuthUser } = useAuthContext(); | ||
|
||
const login = async (username, password) => { | ||
const success = handleInputErrors(username, password); | ||
if (!success) return; | ||
setLoading(true); | ||
try { | ||
const res = await fetch("/api/auth/login", { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ username, password }), | ||
}); | ||
|
||
const data = await res.json(); | ||
if (data.error) { | ||
throw new Error(data.error); | ||
} | ||
|
||
localStorage.setItem("chat-user", JSON.stringify(data)); | ||
setAuthUser(data); | ||
} catch (error) { | ||
toast.error(error.message); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return { loading, login }; | ||
}; | ||
export default useLogin; | ||
|
||
function handleInputErrors(username, password) { | ||
if (!username || !password) { | ||
toast.error("Please fill in all fields"); | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useState } from "react"; | ||
import { useAuthContext } from "../context/AuthContext"; | ||
import toast from "react-hot-toast"; | ||
|
||
const useLogout = () => { | ||
const [loading, setLoading] = useState(false); | ||
const { setAuthUser } = useAuthContext(); | ||
|
||
const logout = async () => { | ||
setLoading(true); | ||
try { | ||
const res = await fetch("/api/auth/logout", { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
const data = await res.json(); | ||
if (data.error) { | ||
throw new Error(data.error); | ||
} | ||
|
||
localStorage.removeItem("chat-user"); | ||
setAuthUser(null); | ||
} catch (error) { | ||
toast.error(error.message); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return { loading, logout }; | ||
}; | ||
export default useLogout; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useState } from "react"; | ||
import useConversation from "../zustand/useConversation"; | ||
import toast from "react-hot-toast"; | ||
|
||
const useSendMessage = () => { | ||
const [loading, setLoading] = useState(false); | ||
const { messages, setMessages, selectedConversation } = useConversation(); | ||
|
||
const sendMessage = async (message) => { | ||
setLoading(true); | ||
try { | ||
const res = await fetch(`/api/messages/send/${selectedConversation._id}`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ message }), | ||
}); | ||
const data = await res.json(); | ||
if (data.error) throw new Error(data.error); | ||
|
||
setMessages([...messages, data]); | ||
} catch (error) { | ||
toast.error(error.message); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return { sendMessage, loading }; | ||
}; | ||
export default useSendMessage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useState } from "react"; | ||
import toast from "react-hot-toast"; | ||
import { useAuthContext } from "../context/AuthContext"; | ||
|
||
const useSignup = () => { | ||
const [loading, setLoading] = useState(false); | ||
const { setAuthUser } = useAuthContext(); | ||
|
||
const signup = async ({ fullName, username, password, confirmPassword, gender }) => { | ||
const success = handleInputErrors({ fullName, username, password, confirmPassword, gender }); | ||
if (!success) return; | ||
|
||
setLoading(true); | ||
try { | ||
const res = await fetch("/api/auth/signup", { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ fullName, username, password, confirmPassword, gender }), | ||
}); | ||
|
||
const data = await res.json(); | ||
if (data.error) { | ||
throw new Error(data.error); | ||
} | ||
localStorage.setItem("chat-user", JSON.stringify(data)); | ||
setAuthUser(data); | ||
} catch (error) { | ||
toast.error(error.message); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return { loading, signup }; | ||
}; | ||
export default useSignup; | ||
|
||
function handleInputErrors({ fullName, username, password, confirmPassword, gender }) { | ||
if (!fullName || !username || !password || !confirmPassword || !gender) { | ||
toast.error("Please fill in all fields"); | ||
return false; | ||
} | ||
|
||
if (password !== confirmPassword) { | ||
toast.error("Passwords do not match"); | ||
return false; | ||
} | ||
|
||
if (password.length < 6) { | ||
toast.error("Password must be at least 6 characters"); | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
import React from 'react' | ||
import ReactDOM from 'react-dom/client' | ||
import App from './App.jsx' | ||
import './index.css' | ||
import React from "react"; | ||
import ReactDOM from "react-dom/client"; | ||
import App from "./App.jsx"; | ||
import "./index.css"; | ||
import { BrowserRouter } from "react-router-dom"; | ||
import { AuthContextProvider } from "./context/AuthContext.jsx"; | ||
|
||
ReactDOM.createRoot(document.getElementById('root')).render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode>, | ||
) | ||
ReactDOM.createRoot(document.getElementById("root")).render( | ||
<React.StrictMode> | ||
<BrowserRouter> | ||
<AuthContextProvider> | ||
<App /> | ||
</AuthContextProvider> | ||
</BrowserRouter> | ||
</React.StrictMode> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
export const funEmojis = [ | ||
"👾", | ||
"⭐", | ||
"🌟", | ||
"🎉", | ||
"🎊", | ||
"🎈", | ||
"🎁", | ||
"🎂", | ||
"🎄", | ||
"🎃", | ||
"🎗", | ||
"🎟", | ||
"🎫", | ||
"🎖", | ||
"🏆", | ||
"🏅", | ||
"🥇", | ||
"🥈", | ||
"🥉", | ||
"⚽", | ||
"🏀", | ||
"🏈", | ||
"⚾", | ||
"🎾", | ||
"🏐", | ||
"🏉", | ||
"🎱", | ||
"🏓", | ||
"🏸", | ||
"🥅", | ||
"🏒", | ||
"🏑", | ||
"🏏", | ||
"⛳", | ||
"🏹", | ||
"🎣", | ||
"🥊", | ||
"🥋", | ||
"🎽", | ||
"⛸", | ||
"🥌", | ||
"🛷", | ||
"🎿", | ||
"⛷", | ||
"🏂", | ||
"🏋️", | ||
"🤼", | ||
"🤸", | ||
"🤺", | ||
"⛹️", | ||
"🤾", | ||
"🏌️", | ||
"🏇", | ||
"🧘", | ||
]; | ||
|
||
export const getRandomEmoji = () => { | ||
return funEmojis[Math.floor(Math.random() * funEmojis.length)]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function extractTime(dateString) { | ||
const date = new Date(dateString); | ||
const hours = padZero(date.getHours()); | ||
const minutes = padZero(date.getMinutes()); | ||
return `${hours}:${minutes}`; | ||
} | ||
|
||
// Helper function to pad single-digit numbers with a leading zero | ||
function padZero(number) { | ||
return number.toString().padStart(2, "0"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { create } from "zustand"; | ||
|
||
const useConversation = create((set) => ({ | ||
selectedConversation: null, | ||
setSelectedConversation: (selectedConversation) => set({ selectedConversation }), | ||
messages: [], | ||
setMessages: (messages) => set({ messages }), | ||
})); | ||
|
||
export default useConversation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
import { defineConfig } from 'vite' | ||
import react from '@vitejs/plugin-react' | ||
import { defineConfig } from "vite"; | ||
import react from "@vitejs/plugin-react"; | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig({ | ||
plugins: [react()], | ||
}) | ||
plugins: [react()], | ||
server: { | ||
port: 3000, | ||
proxy: { | ||
"/api": { | ||
target: "http://localhost:5000", | ||
}, | ||
}, | ||
}, | ||
}); |