-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.tsx
111 lines (92 loc) · 3.08 KB
/
utils.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import axios from 'axios'
import { PostDetails } from './components/posts/PostDetailsTypes'
//- GET
export async function getAllPosts(): Promise<PostDetails[]> {
const response = await axios('https://zeitung-4e991-default-rtdb.firebaseio.com/posts.json')
return Object.values(await response.data)
}
export async function getNavMenuYears() {
const posts = await getAllPosts()
return Array.from(new Set(posts.map(post => post.year))).sort()
}
export async function getMostReadPosts() {
const posts = await getAllPosts()
//- sort by reads count and get 4 most read posts
return posts.sort((a, b) => b.reads - a.reads).slice(0, 4)
}
export async function getPostsByYear(year: number) {
const posts = await getAllPosts()
return posts.filter(post => post.year === year)
}
export async function getMostRecentPost() {
const currentYear = new Date().getFullYear()
let currentYearPosts
currentYearPosts = await getPostsByYear(currentYear)
if (currentYearPosts.length === 0) {
//- if no posts in current year get most recent post in last year
currentYearPosts = await getPostsByYear(currentYear - 1)
}
//- reduce that array to get the most recent post
return currentYearPosts.reduce((acc, current) => (current.month > acc.month ? current : acc))
}
export async function getPostBySlug(slug: string) {
const posts = await getAllPosts()
return posts.find(post => post.slug === slug)
}
export async function getPostByUserSub(userSub) {
const posts = await getAllPosts()
return posts.filter(post => post.userSub === userSub)
}
export async function getPostById(id: string) {
const posts = await getAllPosts()
return posts.find(post => post.id === id)
}
//- MUTATIONS
//- Newsletter
export async function subscribeToNewsletter(email: string) {
return axios.post('https://zeitung-4e991-default-rtdb.firebaseio.com/subscribers.json', { email })
}
//- Posts
//- create
export async function createPost(post: PostDetails) {
return axios.post('https://zeitung-4e991-default-rtdb.firebaseio.com/posts.json', post)
}
//- edit
export async function editPost(obj) {
const { data } = await axios('https://zeitung-4e991-default-rtdb.firebaseio.com/posts.json')
let response
for (const key in data) {
if (data[key].id === obj.id) {
response = axios.patch(
`https://zeitung-4e991-default-rtdb.firebaseio.com/posts/${key}.json`,
obj.postData
)
}
}
return response
}
//- Add one read
export async function addOneRead(obj) {
const { data } = await axios('https://zeitung-4e991-default-rtdb.firebaseio.com/posts.json')
let response
for (const key in data) {
if (data[key].id === obj.id) {
response = axios.patch(
`https://zeitung-4e991-default-rtdb.firebaseio.com/posts/${key}.json`,
{ ...obj.postData, reads: obj.postData.reads + 1 }
)
}
}
return response
}
//- delete
export async function deletePost(id) {
const { data } = await axios('https://zeitung-4e991-default-rtdb.firebaseio.com/posts.json')
let response
for (const key in data) {
if (data[key].id === id) {
response = axios.delete(`https://zeitung-4e991-default-rtdb.firebaseio.com/posts/${key}.json`)
}
}
return response
}