-
Notifications
You must be signed in to change notification settings - Fork 0
/
actionRequests.js
47 lines (36 loc) · 1.28 KB
/
actionRequests.js
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
import { GIPHY_KEY, GIPHY_API_URL } from '../config/api';
import { setRequestOptions } from '../utils';
export const imageSearchReq = async params => {
const { query } = params;
let limit = 'limit=200';
let phrase = `q=${encodeURIComponent(query)}`;
let searchUrl = `${GIPHY_API_URL}?api_key=${GIPHY_KEY}&${phrase}&${limit}`;
return await fetch(searchUrl, setRequestOptions({}))
.then(res => res.json())
.then(res => res.data)
.catch(err => console.log(err));
};
export const retrieveFavorites = () => {
let favorites = JSON.parse(localStorage.getItem('favorites'));
let items = [];
if (Object.keys(favorites).length > 0) {
items = Object.keys(favorites).map(key => favorites[key]);
}
return { favorites, items };
};
export const setupFavorites = params => {
const { favorites } = params;
localStorage.setItem('favorites', JSON.stringify(favorites));
return favorites;
};
export const syncFavorites = async params => {
const { action, item } = params;
let favorites = JSON.parse(localStorage.getItem('favorites'));
if (action === 'add') {
favorites = Object.assign({ [item.id]: item }, favorites);
} else {
delete favorites[item.id];
}
await setupFavorites({ favorites });
return await JSON.parse(localStorage.getItem('favorites'));
};