felicity-lims/webapp/stores/stream.ts

58 lines
2 KiB
TypeScript
Raw Normal View History

2023-11-10 14:05:15 +08:00
import { defineStore } from 'pinia';
2023-11-22 21:52:18 +08:00
import { urqlClient } from '../urql';
2023-11-10 14:05:15 +08:00
import { SUBSCRIBE_TO_ACTIVITY_STREAM } from '../graphql/operations/stream.subscriptions';
2023-11-22 21:52:18 +08:00
import { pipe, subscribe } from 'wonka';
2023-11-10 14:05:15 +08:00
import { useWorksheetStore } from './worksheet';
import { useSampleStore } from './sample';
import useAnalyticsComposable from '../composables/analytics';
export const useStreamStore = defineStore('stream', {
state: () =>
({
streams: [],
} as {
streams: any[];
}),
getters: {
getStreams: state => state.streams,
},
actions: {
addStream(payload) {
const { updateReport } = useAnalyticsComposable();
const wsStore = useWorksheetStore();
const sampleStore = useSampleStore();
this.streams?.unshift(payload);
if (payload.actionObjectType === 'sample') {
sampleStore.updateSampleStatus(payload.actionObject);
}
if (payload.actionObjectType === 'worksheet') {
wsStore.updateWorksheetStatus(payload.actionObject);
}
if (payload.actionObjectType === 'report') {
updateReport(payload.actionObject);
}
if (payload.actionObjectType === 'result') {
sampleStore.updateAnalysesResultsStatus([payload.actionObject]);
wsStore.updateWorksheetResultsStatus([payload.actionObject]);
// wsStore.updateAnalysesResults([payload.actionObject])
}
},
subscribeToActivityStream() {
2023-11-22 21:52:18 +08:00
pipe(
urqlClient.subscription(SUBSCRIBE_TO_ACTIVITY_STREAM, {}),
subscribe(result => {
if (result.data?.latestActivity) {
this.addStream(result.data?.latestActivity);
} else {
}
})
).unsubscribe;
2023-11-10 14:05:15 +08:00
},
},
});