debian-mirror-gitlab/app/assets/javascripts/sidebar/components/assignees/assignees_realtime.vue

88 lines
2.1 KiB
Vue
Raw Normal View History

2020-05-24 23:13:21 +05:30
<script>
2021-06-08 01:23:25 +05:30
import produce from 'immer';
import { convertToGraphQLId, getIdFromGraphQLId } from '~/graphql_shared/utils';
import { IssuableType } from '~/issue_show/constants';
2021-04-29 21:17:54 +05:30
import { assigneesQueries } from '~/sidebar/constants';
2020-05-24 23:13:21 +05:30
export default {
subscription: null,
name: 'AssigneesRealtime',
props: {
mediator: {
type: Object,
2021-04-29 21:17:54 +05:30
required: false,
default: null,
2020-05-24 23:13:21 +05:30
},
2021-06-08 01:23:25 +05:30
issuableType: {
2020-05-24 23:13:21 +05:30
type: String,
required: true,
},
2021-06-08 01:23:25 +05:30
issuableId: {
type: Number,
2020-05-24 23:13:21 +05:30
required: true,
},
2021-06-08 01:23:25 +05:30
queryVariables: {
type: Object,
2021-04-29 21:17:54 +05:30
required: true,
},
2020-05-24 23:13:21 +05:30
},
2021-06-08 01:23:25 +05:30
computed: {
issuableClass() {
return Object.keys(IssuableType).find((key) => IssuableType[key] === this.issuableType);
},
},
2020-05-24 23:13:21 +05:30
apollo: {
2021-06-08 01:23:25 +05:30
issuable: {
2021-04-29 21:17:54 +05:30
query() {
return assigneesQueries[this.issuableType].query;
},
2020-05-24 23:13:21 +05:30
variables() {
2021-06-08 01:23:25 +05:30
return this.queryVariables;
},
update(data) {
return data.workspace?.issuable;
2020-05-24 23:13:21 +05:30
},
2021-06-08 01:23:25 +05:30
subscribeToMore: {
document() {
return assigneesQueries[this.issuableType].subscription;
},
variables() {
return {
issuableId: convertToGraphQLId(this.issuableClass, this.issuableId),
};
},
updateQuery(prev, { subscriptionData }) {
if (prev && subscriptionData?.data?.issuableAssigneesUpdated) {
const data = produce(prev, (draftData) => {
draftData.workspace.issuable.assignees.nodes =
subscriptionData.data.issuableAssigneesUpdated.assignees.nodes;
});
if (this.mediator) {
this.handleFetchResult(data);
}
return data;
}
return prev;
},
2020-05-24 23:13:21 +05:30
},
},
},
methods: {
2021-06-08 01:23:25 +05:30
handleFetchResult(data) {
2021-04-29 21:17:54 +05:30
const { nodes } = data.workspace.issuable.assignees;
2020-05-24 23:13:21 +05:30
2021-03-08 18:12:59 +05:30
const assignees = nodes.map((n) => ({
2020-05-24 23:13:21 +05:30
...n,
avatar_url: n.avatarUrl,
id: getIdFromGraphQLId(n.id),
}));
this.mediator.store.setAssigneesFromRealtime(assignees);
},
},
render() {
2021-04-29 21:17:54 +05:30
return null;
2020-05-24 23:13:21 +05:30
},
};
</script>