-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
47 lines (42 loc) · 1.31 KB
/
api.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 { AsyncStorage } from 'react-native';
const STORAGE_KEY = '@Flashcards:store';
export const getAllDecks = () =>
AsyncStorage.getItem(STORAGE_KEY)
.then(d => JSON.parse(d))
.catch(err => {
console.log(err);
return [];
});
export const saveDeckTitle = title =>
AsyncStorage.getItem(STORAGE_KEY)
.then(data => {
data = JSON.parse(data);
data = data ? data : {};
data[title] = { title, questions: [] };
return AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(data));
})
.catch(err => {
console.log(err);
return null;
});
export const getDeck = id =>
AsyncStorage.getItem(STORAGE_KEY)
.then(data => {
data = JSON.parse(data);
return data[id];
})
.catch(err => {
console.log(err);
return null;
});
export const addCardToDeck = (title, card) =>
AsyncStorage.getItem(STORAGE_KEY)
.then(data => {
data = JSON.parse(data);
data[title].questions = data[title].questions.concat(card);
return AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(data));
})
.catch(err => {
console.log(err);
return null;
});