-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathgeneration.store.js
More file actions
34 lines (29 loc) · 1.17 KB
/
generation.store.js
File metadata and controls
34 lines (29 loc) · 1.17 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
import { writable } from "svelte/store";
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || "http://localhost:8504";
const API_ENDPOINT = API_BASE_URL + "/generate-ticket";
export const generationStates = {
IDLE: "idle",
SUCCESS: "success",
ERROR: "error",
LOADING: "loading",
};
function createGenerationStore() {
const { subscribe, update } = writable({ state: generationStates.IDLE, data: { title: "", text: "" } });
return {
subscribe,
generate: async (fromQuestion) => {
update(() => ({ state: generationStates.LOADING, data: { title: "", text: "" } }));
try {
const response = await fetch(`${API_ENDPOINT}?text=${encodeURI(fromQuestion)}`, {
method: "GET",
});
const generation = await response.json();
update(() => ({ state: generationStates.SUCCESS, data: generation.result }));
} catch (e) {
console.log("e: ", e);
update(() => ({ state: generationStates.ERROR, data: { title: "", text: "" } }));
}
},
};
}
export const generationStore = createGenerationStore();