Small changes

This commit is contained in:
bskjon 2024-06-19 19:56:51 +02:00
parent 9244a65235
commit 77f1787403
2 changed files with 65 additions and 0 deletions

View File

@ -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<EventsObjectListResponse>("/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 (
<></>
)
}

View File

@ -0,0 +1,26 @@
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
import { EventsObjectList, EventsObjectListResponse } from "../../types";
interface PersistentEventsState {
items: Array<EventsObjectList>
lastPull: string|null
}
const initialState: PersistentEventsState = {
items: [],
lastPull: null
}
const persistentEventSlice = createSlice({
name: "PersistentEvents",
initialState,
reducers: {
set(state, action: PayloadAction<EventsObjectListResponse>) {
state.items = action.payload.items;
state.lastPull = action.payload.lastPull;
}
}
});
export const { set } = persistentEventSlice.actions;
export default persistentEventSlice.reducer;