Skip to content

Commit 5bb5db3

Browse files
Rui FanRui Fan
authored andcommitted
timetravel
1 parent 1d76a11 commit 5bb5db3

File tree

7 files changed

+45
-19
lines changed

7 files changed

+45
-19
lines changed

examples/example-2/client/src/client/linked/TimeTravel.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import React, { useEffect } from "react";
22
import { useQueryClient } from "@tanstack/react-query";
3+
import { QueryDisplay } from "../types";
34

45
const TimeTravel = () => {
56
const queryClient = useQueryClient();
67

78
useEffect(() => {
89
const listener = (event: CustomEvent) => {
9-
queryClient.setQueryData([event.detail.queryKey], event.detail.queryData);
10+
const currentQuery: QueryDisplay[] = event.detail.currentQuery;
11+
currentQuery.forEach((queryState) => {
12+
if (queryState.queryData !== "N/A") {
13+
queryClient.setQueryData(
14+
[queryState.queryKey.slice(2, -2)],
15+
queryState.queryData,
16+
);
17+
}
18+
});
1019
};
1120
window.addEventListener("UpdateUI", listener);
1221
return () => window.removeEventListener("UpdateUI", listener);

examples/example-2/client/src/client/types.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@ declare global {
1616
UpdateUI: CustomEvent;
1717
TimeTravel: CustomEvent;
1818
}
19-
}
19+
}
20+
21+
export type QueryDisplay = {
22+
queryKey: string;
23+
queryData: any;
24+
};
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
{
22
"posts": [
33
{
4-
"id": 1,
5-
"text": "Feed 1 - First Post",
4+
"text": "feed 2",
65
"liked": false,
7-
"comments": [
8-
"First comment on first post",
9-
"Second comment on first post",
10-
"Third comment on first post"
11-
],
6+
"comments": [],
127
"createComment": true,
13-
"timestamp": "12:00 PM"
8+
"timestamp": "7:02 PM"
149
}
1510
]
1611
}

examples/example-2/server/models/postsThree.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"id": 1,
55
"text": "Feed 3 - First Post",
6-
"liked": true,
6+
"liked": false,
77
"comments": [
88
"First comment on first post",
99
"Second comment on first post"
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
{
22
"posts": [
33
{
4-
"id": 1,
5-
"text": "Feed 2 - First Post",
4+
"text": "4",
65
"liked": true,
7-
"comments": [
8-
"First comment on first post",
9-
"Second comment on first post"
10-
],
6+
"comments": [],
117
"createComment": false,
12-
"timestamp": "12:00 PM"
8+
"timestamp": "9:57 PM"
9+
},
10+
{
11+
"text": "2",
12+
"liked": false,
13+
"comments": [],
14+
"createComment": false,
15+
"timestamp": "9:57 PM"
1316
}
1417
]
1518
}

src/TimeTravel.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import React, { useEffect } from "react";
22
import { useQueryClient } from "@tanstack/react-query";
3+
import { QueryDisplay } from "./types";
34

45
const TimeTravel = () => {
56
const queryClient = useQueryClient();
67

78
useEffect(() => {
89
const listener = (event: CustomEvent) => {
9-
queryClient.setQueryData([event.detail.queryKey], event.detail.queryData);
10+
const currentQuery: QueryDisplay[] = event.detail.currentQuery;
11+
currentQuery.forEach((queryState) => {
12+
if (queryState.queryData !== "N/A") {
13+
queryClient.setQueryData(
14+
[queryState.queryKey.slice(2, -2)],
15+
queryState.queryData,
16+
);
17+
}
18+
});
1019
};
1120
window.addEventListener("UpdateUI", listener);
1221
return () => window.removeEventListener("UpdateUI", listener);

src/types.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ declare global {
88
TimeTravel: CustomEvent;
99
}
1010
}
11+
12+
export type QueryDisplay = {
13+
queryKey: string;
14+
queryData: any;
15+
};

0 commit comments

Comments
 (0)