generated from graasp/graasp-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add useChildrenUpdates and useSharedItemsUpdates hooks
- Loading branch information
1 parent
468999d
commit 167e313
Showing
4 changed files
with
143 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* Graasp websocket client | ||
* React effect hooks to subscribe to real-time updates and mutate query client | ||
* | ||
* @author Alexandre CHAU | ||
*/ | ||
|
||
import { ServerMessage } from "graasp-websockets/src/interfaces/message"; | ||
import { List } from "immutable"; | ||
import { useEffect } from "react"; | ||
import { QueryClient } from "react-query"; | ||
import { buildItemChildrenKey, buildItemKey, SHARED_ITEMS_KEY } from "../config/keys"; | ||
import { Item, UUID } from "../types"; | ||
import { Channel, GraaspWebsocketClient } from "./ws-client"; | ||
|
||
const ITEM_ENTITY_TYPE = "item" | ||
const MEMBER_ENTITY_TYPE = "member" | ||
|
||
export default (websocketClient: GraaspWebsocketClient, queryClient: QueryClient) => ({ | ||
/** | ||
* React hook to subscribe to the children updates of the give parent item ID | ||
* | ||
* @param parentId The ID of the parent on which to observe children updates | ||
*/ | ||
useChildrenUpdates: (parentId: UUID) => { | ||
useEffect(() => { | ||
if (!parentId) { | ||
return; | ||
} | ||
|
||
const channel: Channel = { name: parentId, entity: ITEM_ENTITY_TYPE }; | ||
const parentChildrenKey = buildItemChildrenKey(parentId); | ||
|
||
const handler = (data: ServerMessage) => { | ||
if (data.type === "update" && data.body.kind === "childItem" && data.body.entity === ITEM_ENTITY_TYPE) { | ||
const current: List<Item> | undefined = queryClient.getQueryData(parentChildrenKey); | ||
const value = data.body.value; | ||
let mutation; | ||
switch (data.body.op) { | ||
case "create": { | ||
if (current && !current.find(i => i.id === value.id)) { | ||
mutation = current.push(value); | ||
queryClient.setQueryData(parentChildrenKey, mutation); | ||
queryClient.setQueryData(buildItemKey(value.id), value); | ||
} | ||
break; | ||
} | ||
case "delete": { | ||
mutation = current?.filter(i => i.id !== value.id); | ||
queryClient.setQueryData(parentChildrenKey, mutation); | ||
break; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
websocketClient.subscribe(channel, handler); | ||
|
||
return function cleanup() { | ||
websocketClient.unsubscribe(channel, handler); | ||
}; | ||
}, [parentId]); | ||
}, | ||
|
||
useSharedItemsUpdates: (userId: UUID) => { | ||
useEffect(() => { | ||
if (!userId) { | ||
return; | ||
} | ||
|
||
const channel: Channel = { name: userId, entity: MEMBER_ENTITY_TYPE }; | ||
|
||
const handler = (data: ServerMessage) => { | ||
if (data.type === "update" && data.body.kind === "sharedWith" && data.body.entity === MEMBER_ENTITY_TYPE) { | ||
const current: List<Item> | undefined = queryClient.getQueryData(SHARED_ITEMS_KEY); | ||
const value = data.body.value; | ||
let mutation; | ||
switch (data.body.op) { | ||
case "create": { | ||
if (current && !current.find(i => i.id === value.id)) { | ||
mutation = current.push(value); | ||
queryClient.setQueryData(SHARED_ITEMS_KEY, mutation); | ||
queryClient.setQueryData(buildItemKey(value.id), value); | ||
} | ||
break; | ||
} | ||
case "delete": { | ||
mutation = current?.filter(i => i.id !== value.id); | ||
queryClient.setQueryData(SHARED_ITEMS_KEY, mutation); | ||
break; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
websocketClient.subscribe(channel, handler); | ||
|
||
return function cleanup() { | ||
websocketClient.unsubscribe(channel, handler); | ||
} | ||
}, [userId]); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,23 @@ | ||
/** | ||
* Graasp websocket client top-level file | ||
* Entry point to use the Graasp WebSocket client in front-end applications | ||
* | ||
* @author Alexandre CHAU | ||
*/ | ||
|
||
import { QueryClient } from "react-query"; | ||
import { QueryClientConfig } from "../types"; | ||
import configureWebsocketHooks from "./hooks"; | ||
import { configureWebsocketClient } from "./ws-client"; | ||
|
||
const configureWebsockets = ( | ||
// to be called by the main query client configurator | ||
export default ( | ||
queryClient: QueryClient, | ||
queryConfig: QueryClientConfig, | ||
) => ({ | ||
|
||
}); | ||
) => { | ||
const websocketClient = configureWebsocketClient(queryConfig); | ||
|
||
export default configureWebsockets; | ||
return { | ||
hooks: configureWebsocketHooks(websocketClient, queryClient), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters