-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathXXX_storeQuery.html
58 lines (48 loc) · 1.5 KB
/
XXX_storeQuery.html
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
55
56
57
58
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Application Shell</title>
</head>
<body style="box-sizing: content-box; margin-inline: auto; max-inline-size: 60vw; margin-top: 10vh;">
<script src="./build/cami.cdn.js"></script>
<script type="module">
const { html, ReactiveElement, store } = cami;
// Singleton pattern to ensure the store is only initialized once
const appStore = store({
posts: [],
isLoading: false,
error: null
});
// Register a query to fetch posts
appStore.query('posts/fetchAll', {
queryKey: 'posts/fetchAll',
queryFn: () => fetch('https://api.camijs.com/posts').then(res => res.json()),
refetchOnWindowFocus: true,
});
appStore.register('posts/fetchAll/pending', (state, payload) => {
state.isLoading = true;
state.posts = [];
state.error = null;
});
appStore.register('posts/fetchAll/success', (state, payload) => {
state.posts = payload;
state.isLoading = false;
state.error = null;
});
appStore.register('posts/fetchAll/error', (state, payload) => {
state.error = payload;
state.isLoading = false;
state.posts = [];
});
// Fetch all posts
appStore.fetch('posts/fetchAll');
appStore.subscribe((state) => {
console.log('State:', state.posts);
});
cami.debug.enable();
console.log('App store initialized');
</script>
</body>
</html>