Newer
Older
import { Instalike } from '@jmetterrothan/instalike';
import { data } from 'autoprefixer';
import { AppThunkAction } from '../types';
import { failurePostAction, loadPostAction, setPost, sucessPostAction } from './actions';
// 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 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`;
}
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
}
// Réccupérer les posts
export const fetchPost = (postid: number): AppThunkAction<Promise<void>> => {
return async (dispatch, getState, api) => {
dispatch(loadPostAction());
try {
const { data } = await api.posts.find(postid).fetch();
dispatch(setPost(data));
dispatch(sucessPostAction());
} catch (eee) {
dispatch(failurePostAction());
throw eee;
}
};
};
// Ajouter un post
export const addPost = (resources: File[], location: string, caption: string): AppThunkAction<Promise<void>> => { //ajouter les autres éléments (accessibilityCaption, hasCommentsDisabled) ?
return async (dispatch, getState, api) => {
try {
const { data } = await api.posts.create({
resources: resources,
location: location,
caption: caption,
});
dispatch(setPost(data));
} catch (fff) {
throw fff;
}
};
};