felicity-lims/webapp/stores/stream.ts

59 lines
2 KiB
TypeScript
Raw Normal View History

2023-04-10 09:29:10 +08:00
import { defineStore } from 'pinia';
2022-04-04 02:54:31 +08:00
import { urqlClient } from '../urql';
2023-04-10 09:29:10 +08:00
import { SUBSCRIBE_TO_ACTIVITY_STREAM } from '../graphql/stream.subscriptions';
2022-04-04 02:54:31 +08:00
import { pipe, subscribe } from 'wonka';
2023-04-10 09:29:10 +08:00
import { useWorksheetStore } from './worksheet';
import { useSampleStore } from './sample';
import useAnalyticsComposable from '../composables/analytics';
2022-04-04 02:54:31 +08:00
2023-04-10 09:29:10 +08:00
export const useStreamStore = defineStore('stream', {
state: () =>
({
streams: [],
} as {
streams: any[];
}),
2022-04-04 02:54:31 +08:00
getters: {
2023-04-10 09:29:10 +08:00
getStreams: state => state.streams,
2022-04-04 02:54:31 +08:00
},
actions: {
2023-04-10 09:29:10 +08:00
addStream(payload) {
const { updateReport } = useAnalyticsComposable();
const wsStore = useWorksheetStore();
const sampleStore = useSampleStore();
this.streams?.unshift(payload);
2023-04-10 09:29:10 +08:00
if (payload.actionObjectType === 'sample') {
sampleStore.updateSampleStatus(payload.actionObject);
}
2023-04-10 09:29:10 +08:00
if (payload.actionObjectType === 'worksheet') {
wsStore.updateWorksheetStatus(payload.actionObject);
}
2023-04-10 09:29:10 +08:00
if (payload.actionObjectType === 'report') {
updateReport(payload.actionObject);
}
2023-04-10 09:29:10 +08:00
if (payload.actionObjectType === 'result') {
sampleStore.updateAnalysesResultsStatus([payload.actionObject]);
wsStore.updateWorksheetResultsStatus([payload.actionObject]);
2022-12-22 22:42:05 +08:00
// wsStore.updateAnalysesResults([payload.actionObject])
}
2022-04-04 02:54:31 +08:00
},
2023-04-10 09:29:10 +08:00
subscribeToActivityStream() {
2022-04-04 02:54:31 +08:00
pipe(
2023-04-10 09:29:10 +08:00
urqlClient.subscription(SUBSCRIBE_TO_ACTIVITY_STREAM, {}),
subscribe(result => {
2023-04-10 09:29:10 +08:00
if (result.data?.latestActivity) {
this.addStream(result.data?.latestActivity);
} else {
2023-04-10 09:29:10 +08:00
console.log('empty stream received');
}
})
2023-04-10 09:29:10 +08:00
).unsubscribe;
},
},
});