F1Manager-Calc/libs/storage/cloudStorage.js

31 lines
955 B
JavaScript
Raw Normal View History

2023-04-18 09:23:55 +02:00
import axios from "axios";
2023-04-18 14:14:23 +02:00
import storage from 'redux-persist/lib/storage'
2023-04-18 09:23:55 +02:00
2023-04-18 10:13:38 +02:00
export default function createCloudStorage(session) {
2023-04-18 14:14:23 +02:00
const fallbackStorage = storage;
2023-04-18 09:23:55 +02:00
return {
getItem: (key) => {
return new Promise((resolve, reject) => {
2023-04-18 09:51:22 +02:00
axios.get(`/api/cloud/storage`).then(d => {
2023-04-18 14:14:23 +02:00
if (d.data && d.data[key]) {
resolve(d.data[key])
} else {
fallbackStorage.getItem(key).then(resolve).catch(reject)
}
}).catch(reject)
2023-04-18 09:23:55 +02:00
})
},
setItem: (key, item) => {
return new Promise((resolve, reject) => {
2023-04-18 14:14:23 +02:00
fallbackStorage.setItem(key, item)
axios.post(`/api/cloud/storage`, {[key]: item}).then(resolve).catch(reject)
2023-04-18 09:23:55 +02:00
})
},
removeItem: (key) => {
return new Promise((resolve, reject) => {
2023-04-18 14:14:23 +02:00
fallbackStorage.removeItem(key)
axios.post(`/api/cloud/storage`, {[key]: null}).then(resolve).catch(reject)
2023-04-18 09:23:55 +02:00
})
},
}
}