-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathflux.js
executable file
·53 lines (50 loc) · 1.22 KB
/
flux.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
48
49
50
51
52
53
const getState = ({ getStore, getActions, setStore }) => {
return {
store: {
message: null,
demo: [
{
title: "FIRST",
background: "white",
initial: "white"
},
{
title: "SECOND",
background: "white",
initial: "white"
}
],
},
actions: {
// Use getActions to call a function within a fuction
exampleFunction: () => {
getActions().changeColor(0, "green");
},
getMessage: async () => {
try {
// fetching data from the backend
const resp = await fetch(process.env.BACKEND_URL + "/api/hello")
const data = await resp.json()
setStore({ message: data.message })
// don't forget to return something, that is how the async resolves
return data;
} catch (error) {
console.log("Error loading message from backend", error)
}
},
changeColor: (index, color) => {
//get the store
const store = getStore();
//we have to loop the entire demo array to look for the respective index
//and change its color
const demo = store.demo.map((elm, i) => {
if (i === index) elm.background = color;
return elm;
});
//reset the global store
setStore({ demo: demo });
},
}
};
};
export default getState;