F1Manager-Calc/libs/storage/cloudStorage.js

37 lines
1.1 KiB
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-21 08:55:45 +02:00
export default function createCloudStorage() {
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-21 18:21:49 +02:00
if (typeof window === "undefined") {
resolve();
} else if (new Date() - window.lastUpload < 30 * 1000){
resolve();
} else {
window.lastUpload = new Date();
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:37:02 +02:00
// fallbackStorage.removeItem(key)
2023-04-18 14:14:23 +02:00
axios.post(`/api/cloud/storage`, {[key]: null}).then(resolve).catch(reject)
2023-04-18 09:23:55 +02:00
})
},
}
}