Skip to content
Snippets Groups Projects
Commit ac213428 authored by Chloé JACOB's avatar Chloé JACOB :alien:
Browse files

affichage du temps de publication d'un post + amélioration de calculateTime

parent c4ad89b0
Branches
Tags
No related merge requests found
......@@ -4,8 +4,10 @@ import { Instalike } from '@jmetterrothan/instalike';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { /* faEllipsisVertical, */ faTrash } from '@fortawesome/free-solid-svg-icons';
// AUTRES FICHIERS
import useAppDispatch from '../hooks/useAppDispatch';
import { deleteCommentFeedAsync } from '../redux/feed/thunks';
import { calculateTime } from '../redux/post/thunks';
type CommentProps = {
tab_comments: Instalike.Comment[];
......@@ -29,7 +31,7 @@ const Comment = ({ tab_comments }: CommentProps) => {
<div>
{/* <p><span className="font-bold mr-1">{comment.owner.userName}</span>{limitCommentText(comment.text)}</p> */}
<p><span className="font-bold mr-1">{comment.owner.userName}</span>{comment.text}</p>
<p className="text-sm">7 days ago</p>
<p className="text-sm">{calculateTime(comment.createdAt)}</p>
</div>
</div>
{comment.owner.isViewer && (
......
......@@ -23,18 +23,21 @@ export const fetchPostAsync = (postid: number): AppThunkAction<Promise<void>> =>
// Calcul temps de publication d'un post / commentaire
export const calculateTime = (createdAt: string): string => {
const createdDate = new Date(createdAt);
const currentDate = new Date();
const timeDiff = Math.abs(currentDate.getTime() - createdDate.getTime());
const diffMinutes = Math.floor(timeDiff / (1000 * 60)); //"Math.ceil" -> entier supérieur / "Math.floor" -> entier inférieur
const diffHours = Math.floor(timeDiff / (1000 * 60 * 60));
const diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
if (diffMinutes < 60) {
return diffMinutes === 1 ? `${diffMinutes} minute ago` : `${diffMinutes} minutes ago`;
} else if (diffHours < 24) {
return diffHours === 1 ? `${diffHours} hour ago` : `${diffHours} hours ago`;
} else {
return diffDays === 1 ? `${diffDays} day ago` : `${diffDays} days ago`;
}
}
const createdDate = new Date(createdAt);
const currentDate = new Date();
const timeDiff = Math.abs(currentDate.getTime() - createdDate.getTime());
const diffSeconds = Math.floor(timeDiff / 1000); // ajout des secondes
const diffMinutes = Math.floor(timeDiff / (1000 * 60));
const diffHours = Math.floor(timeDiff / (1000 * 60 * 60));
const diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
if (diffSeconds < 60) {
return diffSeconds === 1 ? `${diffSeconds} seconde ago` : `${diffSeconds} secondes ago`;
} else if (diffMinutes < 60) {
return diffMinutes === 1 ? `${diffMinutes} minute ago` : `${diffMinutes} minutes ago`;
} else if (diffHours < 24) {
return diffHours === 1 ? `${diffHours} heure ago` : `${diffHours} heures ago`;
} else {
return diffDays === 1 ? `${diffDays} day ago` : `${diffDays} days ago`;
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment