diff --git a/apps/ui/web/src/app/page/PersistentEventsPage.tsx b/apps/ui/web/src/app/page/PersistentEventsPage.tsx new file mode 100644 index 00000000..f396d8de --- /dev/null +++ b/apps/ui/web/src/app/page/PersistentEventsPage.tsx @@ -0,0 +1,39 @@ +import { useEffect } from "react"; +import { useDispatch, useSelector } from "react-redux"; +import { useStompClient } from 'react-stomp-hooks'; +import { RootState } from "../store"; +import { useWsSubscription } from "../ws/subscriptions"; +import { set } from "../store/persistent-events-slice"; +import { EventsObjectListResponse } from "../../types"; + +export default function PersistentEventsPage() { + const dispatch = useDispatch(); + const client = useStompClient(); + const cursor = useSelector((state: RootState) => state.persistentEvents) + + useWsSubscription("/topic/persistent/events", (response) => { + dispatch(set(response)) + }); + + useEffect(() => { + + + // Kjør din funksjon her når komponenten lastes inn for første gang + // Sjekk om cursor er null + if (cursor.items === null && client !== null) { + console.log(cursor) + // Kjør din funksjon her når cursor er null og client ikke er null + client?.publish({ + destination: "/app/persistent/events" + }); + + // Alternativt, du kan dispatche en Redux handling her + // dispatch(fetchDataAction()); // Eksempel på å dispatche en handling + } + }, [cursor, client, dispatch]); + + + return ( + <> + ) +} \ No newline at end of file diff --git a/apps/ui/web/src/app/store/persistent-events-slice.ts b/apps/ui/web/src/app/store/persistent-events-slice.ts new file mode 100644 index 00000000..107bb3e3 --- /dev/null +++ b/apps/ui/web/src/app/store/persistent-events-slice.ts @@ -0,0 +1,26 @@ +import { PayloadAction, createSlice } from "@reduxjs/toolkit" +import { EventsObjectList, EventsObjectListResponse } from "../../types"; + +interface PersistentEventsState { + items: Array + lastPull: string|null +} + +const initialState: PersistentEventsState = { + items: [], + lastPull: null +} + +const persistentEventSlice = createSlice({ + name: "PersistentEvents", + initialState, + reducers: { + set(state, action: PayloadAction) { + state.items = action.payload.items; + state.lastPull = action.payload.lastPull; + } + } +}); + +export const { set } = persistentEventSlice.actions; +export default persistentEventSlice.reducer; \ No newline at end of file