Skip to content

Commit

Permalink
create new component Message; refine components and logic; b00tc4mp#87
Browse files Browse the repository at this point in the history
  • Loading branch information
Samu2196 committed Aug 27, 2024
1 parent 24b84c2 commit e520c5a
Show file tree
Hide file tree
Showing 80 changed files with 167 additions and 138 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import jwt from 'jsonwebtoken'

import { errors } from 'com'
import { logic } from 'cor'
import { errors } from '../../com/index.js'
import { logic } from '../../cor/index.js'

const { SessionError } = errors

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { name, surname, username, email, password, passwordRepeat } = req.body
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
let { userId, query: { q, distance, coords } } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logic } from 'cor'
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req
Expand Down
6 changes: 3 additions & 3 deletions staff/samuele-spinetti/project/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { CronJob as cron } from 'cron'

import randomQuery from '../app/util/randomQuery.js'

import { logic } from 'cor'
import { logic } from '../cor/index.js'

import { mongoose } from 'cor'
import { mongoose } from '../cor/index.js'

import { cors, jsonBodyParser, jwtVerifier, errorHandler } from './middlewares/index.js'

Expand Down Expand Up @@ -89,7 +89,7 @@ mongoose.connect(process.env.MONGODB_URI)
api.delete('/comments/:commentId', jwtVerifier, deleteCommentHandler)

new cron(
'*/30 * * * *',
'*/2 * * * *',
() => {

console.log('news at ' + new Date(Date.now()))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { errors } from 'com'
import { errors } from '../../com/index.js'

const { ValidationError, CredentialsError, NotFoundError, DuplicityError, SessionError, OwnerShipError, PermissionError } = errors

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import jwt from 'jsonwebtoken'

import { errors } from 'com'
import { errors } from '../../com/index.js'

const { SessionError } = errors

Expand Down
4 changes: 3 additions & 1 deletion staff/samuele-spinetti/project/app/logic/getUser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { errors } from '../../com/index.js'
import { errors, validate } from '../../com/index.js'

const { SystemError } = errors

export default targetUserId => {
validate.id(targetUserId, 'targetUserId')

return fetch(`http://localhost:8080/users/${targetUserId}`, {
headers: {
Authorization: `Bearer ${sessionStorage.token}`
Expand Down
3 changes: 1 addition & 2 deletions staff/samuele-spinetti/project/app/view/common/Alert.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ export default function Alert({ message, onAccept }) {
<Paragraph className="text-xl font-semibold text-gray-800 text-center">{message}</Paragraph>
<Button
className="bg-gradient-to-br from-green-400 to-fuchsia-500 text-white font-bold py-2 px-4 rounded-lg"
onClick={onAccept}
>
onClick={onAccept}>
Accept
</Button>
</Container>
Expand Down
41 changes: 12 additions & 29 deletions staff/samuele-spinetti/project/app/view/home/Chat.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { useState, useEffect, useRef, Fragment } from 'react'
import { useState, useEffect, useRef } from 'react'
import { useNavigate, useParams } from 'react-router-dom'

import useContext from '../context.js'

import logic from '../../logic'
import formatTime from '../../util/formatTime'

import Container from '../library/Container'
import Paragraph from '../library/Paragraph'
import Button from '../library/Button'
import Heading from '../library/Heading'
import Image from '../library/Image'
import Form from '../library/Form'
import Message from './Message'


export default function Chat() {
const [messages, setMessages] = useState([])
const [userId, setUserId] = useState(null)
const [avatar, setAvatar] = useState(null)
const navigate = useNavigate()
const { alert } = useContext()

const messagesEndRef = useRef(null)

Expand Down Expand Up @@ -124,33 +128,12 @@ export default function Chat() {
</Container>
</Container>

<Container className="flex flex-col overflow-y-auto p-4 bg-gray-100">
{messages.length === 0 ? (
<Paragraph className="text-center text-gray-500">No messages yet.</Paragraph>
) : (
messages.map(message => (
(message.author.id === logic.getUserId()) ? (

<Fragment key={message.id}>
<Container className="flex p-2 mb-1 rounded-lg max-w-[70%] bg-green-300 text-black self-end">
<Paragraph className="p-1 text-lg">{message.message}</Paragraph>
</Container>
<Container className="self-end mb-2">
<Paragraph className="text-xs text-gray-400">{formatTime(new Date(message.date))}</Paragraph>
</Container>
</Fragment>
) : (
<Fragment key={message.id}>
<Container className="flex p-2 mb-1 rounded-lg max-w-[70%] bg-fuchsia-200 text-black self-start">
<Paragraph className="p-1 text-lg">{message.message}</Paragraph>
</Container>
<Container className="self-start mb-2">
<Paragraph className="text-xs text-gray-400">{formatTime(new Date(message.date))}</Paragraph>
</Container>
</Fragment>
)
))
)}
<Container className="flex flex-col h-screen w-screen overflow-y-auto p-4 bg-gray-100">
{messages.length === 0
? <Paragraph className="text-center text-gray-500">No messages yet.</Paragraph>
: messages.map(message => <Message key={message.id} message={message} />)
}

<div ref={messagesEndRef} />
</Container>

Expand Down
3 changes: 3 additions & 0 deletions staff/samuele-spinetti/project/app/view/home/Chats.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useState, useEffect } from 'react'
import { useNavigate } from 'react-router-dom'

import useContext from '../context.js'

import logic from '../../logic'

import Paragraph from '../library/Paragraph'
Expand All @@ -12,6 +14,7 @@ import Heading from '../library/Heading'
export default function Chats() {
const [chats, setChats] = useState([])
const navigate = useNavigate()
const { alert } = useContext()

useEffect(() => {

Expand Down
2 changes: 2 additions & 0 deletions staff/samuele-spinetti/project/app/view/home/Comment.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState, useEffect } from 'react'
import useContext from '../context.js'

import logic from '../../logic'

Expand All @@ -16,6 +17,7 @@ import Confirm from '../common/Confirm'
export default function Comment({ comment, onCommentDeleted }) {
const [confirmMessage, setConfirmMessage] = useState(null)
const [user, setUser] = useState(null)
const { alert } = useContext()

useEffect(() => {
try {
Expand Down
2 changes: 2 additions & 0 deletions staff/samuele-spinetti/project/app/view/home/Community.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState, useEffect } from 'react'
import useContext from '../context.js'

import logic from '../../logic'

Expand All @@ -13,6 +14,7 @@ import Post from './Post'
export default function Community() {
const [createPostVisible, setCreatePostVisible] = useState(false)
const [posts, setPosts] = useState([])
const { alert } = useContext()

useEffect(() => {
loadPosts()
Expand Down
3 changes: 3 additions & 0 deletions staff/samuele-spinetti/project/app/view/home/CreatePost.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import logic from '../../logic'

import useContext from '../context.js'

import Container from '../library/Container'
import Button from '../library/Button'
import Form from '../library/Form'
import Heading from '../library/Heading'

export default function CreatePost({ onCancelCreatePost, onPostCreated }) {
const { alert } = useContext()

const handleCreatePostSubmit = event => {
event.preventDefault()
Expand Down
2 changes: 2 additions & 0 deletions staff/samuele-spinetti/project/app/view/home/Header.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useLocation } from 'react-router-dom'
import useContext from '../context.js'

import logic from '../../logic'

Expand All @@ -7,6 +8,7 @@ import Button from '../library/Button'
import Image from '../library/Image'

export default function Header({ onProfileSettingsClicked, onNewsArticlesListClicked, onNewsArticlesSavedListClicked, onPrivateChatsClicked, onLogout }) {
const { alert } = useContext()
const location = useLocation()

const handleLogoutClick = () => {
Expand Down
19 changes: 19 additions & 0 deletions staff/samuele-spinetti/project/app/view/home/Message.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import logic from '../../logic'

import formatTime from '../../util/formatTime'

import Container from '../library/Container'
import Paragraph from '../library/Paragraph'

export default function Message({ message }) {

return <>
<Container className={`flex p-2 mb-1 rounded-lg max-w-[70%] text-black ${(message.author.id === logic.getUserId()) ? 'bg-green-300 self-end' : 'bg-fuchsia-200 self-start'}`} >
<Paragraph className="p-1 text-lg">{message.message}</Paragraph>
</Container>
<Container className={`${(message.author.id === logic.getUserId()) ? 'self-end' : 'seld-start'} mb-2`}>
<Paragraph className="text-xs text-gray-400">{formatTime(new Date(message.date))}</Paragraph>
</Container>
</>

}
3 changes: 3 additions & 0 deletions staff/samuele-spinetti/project/app/view/home/NewsArticle.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import logic from '../../logic'

import useContext from '../context.js'

import Container from '../library/Container'
import Heading from '../library/Heading'
import Image from '../library/Image'
import Paragraph from '../library/Paragraph'
import Button from '../library/Button'

export default function NewsArticle({ newsArticle, onNewsArticleSaveToggled }) {
const { alert } = useContext()

const handleSaveNewsClick = () => {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useState } from 'react'
import useContext from '../context.js'

import logic from '../../logic'

Expand All @@ -10,6 +11,7 @@ import Paragraph from '../library/Paragraph'
export default function NewsArticlesList() {
const [articles, setArticles] = useState([])
const [loading, setLoading] = useState(true)
const { alert } = useContext()

useEffect(() => {
setLoading(true)
Expand Down
Loading

0 comments on commit e520c5a

Please sign in to comment.