-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmercure.html
More file actions
98 lines (90 loc) · 3.29 KB
/
Copy pathmercure.html
File metadata and controls
98 lines (90 loc) · 3.29 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Mercure</title>
<script type="importmap">
{
"imports": {
"@jsxImportSource": "https://esm.sh/react@18.2.0",
"react-dom/": "https://esm.sh/react-dom@18.2.0/",
"react": "https://esm.sh/react@18.2.0",
"@tanstack/react-query": "https://esm.sh/@tanstack/react-query?deps=react@18.2.0",
"eventsource": "https://esm.sh/eventsource",
"@api-platform/mercure": "./mercure.js"
}
}
</script>
<script type="module" src="https://esm.sh/run"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
import mercure, { close } from "@api-platform/mercure";
import {useState} from "react"
import {createRoot} from "react-dom/client"
import {
QueryClient,
useQuery,
QueryClientProvider,
} from '@tanstack/react-query'
const queryClient = new QueryClient();
queryClient.getQueryCache().subscribe((event) => {
if (event.type === 'removed') {
event.query.queryKey.forEach((queryKey) => {
close(queryKey)
})
}
})
function Author() {
const [id, setId] = useState('/authors/1')
// this sends and update to mercure, API Platform does this under the hood
function handleSubmit(e) {
e.preventDefault();
const form = e.target;
const formData = new FormData(form);
const body = new URLSearchParams(formData.entries());
// You can pass formData as a fetch body directly:
fetch(form.action, { method: form.method, body: body.toString(), headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InN1YnNjcmliZSI6WyIqIl0sInB1Ymxpc2giOlsiKiJdfX0.NXhzhXJ8VTxiRRW3pAB4EgP7s_guZeibwzAGw3wZ_KY'
}
});
}
const { isPending, error, data: author } = useQuery({
gcTime: 500,
queryKey: [id],
queryFn: ({queryKey}) => {
return mercure(queryKey, {onUpdate: (d) => queryClient.setQueryData([d['@id']], d)})
.then(res => res.json())
}
})
if (isPending) return 'Loading...'
if (error) return 'An error has occurred: ' + error.message
return (
<div>
<button data-testid="author-1" onClick={() => setId(`/authors/1`)}>Author 1</button>
<button data-testid="author-2" onClick={() => setId(`/authors/2`)}>Author 2</button>
<button data-testid="author-3" onClick={() => setId(`/authors/3`)}>Author 3</button>
<p data-testid="result">
viewing {id}: {author.name}
</p>
<form method="POST" action="/.well-known/mercure" onSubmit={handleSubmit}>
<input type="hidden" name="topic" defaultValue="/authors/1" />
<textarea cols="60" rows="2" name="data" defaultValue='{"@id":"/authors/1","name":"Soyuka"}'></textarea><br />
<button type="submit" data-testid="mercure">Send mercure update</button>
</form>
</div>
);
}
function App() {
return (
<QueryClientProvider client={queryClient}>
<Author />
</QueryClientProvider>
)
}
createRoot(root).render(<App />)
</script>
</body>
</html>