-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathremote-save-state.ts
More file actions
54 lines (46 loc) · 1.33 KB
/
remote-save-state.ts
File metadata and controls
54 lines (46 loc) · 1.33 KB
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
import { serialize } from '@/src/io/state-file/serialize';
import { useMessageStore } from '@/src/store/messages';
import { $fetch } from '@/src/utils/fetch';
import { defineStore } from 'pinia';
import { ref } from 'vue';
const useRemoteSaveStateStore = defineStore('remoteSaveState', () => {
const saveUrl = ref('');
const isSaving = ref(false);
const messageStore = useMessageStore();
const setSaveUrl = (url: string) => {
saveUrl.value = url;
};
const saveState = async () => {
if (!saveUrl.value || isSaving.value) return;
try {
isSaving.value = true;
const blob = await serialize();
const saveResult = await $fetch(saveUrl.value, {
method: 'POST',
headers: {
'Content-Type': 'application/zip',
'Content-Length': blob.size.toString(),
},
body: blob,
});
if (saveResult.ok) messageStore.addSuccess('Save Successful');
else
messageStore.addError('Save Failed', {
details: 'Network response not OK',
});
} catch (error) {
messageStore.addError('Save Failed with error', {
details: `Failed from: ${error}`,
});
} finally {
isSaving.value = false;
}
};
return {
saveUrl,
setSaveUrl,
isSaving,
saveState,
};
});
export default useRemoteSaveStateStore;