forked from github/plane
03e5f4a5bd
* dev: add is_subscriber to issue details endpoint * dev: remove is_subscribed annotation from detail serializers * dev: update issue details endpoint * dev: inbox issue create * dev: issue detail serializer * dev: optimize and add extra fields for issue details * dev: remove data from issue updates * dev: add fields for issue link and attachment * remove expecting a issue response while updating and deleting an issue * change link, attachment and reaction types and modify store to recieve their data from within the issue detail API call * make changes for subscription store to recieve data from issue detail API call * dev: add issue reaction id * add query prarms for archived issue --------- Co-authored-by: rahulramesha <rahulramesham@gmail.com>
111 lines
3.8 KiB
TypeScript
111 lines
3.8 KiB
TypeScript
import { action, makeObservable, observable, runInAction } from "mobx";
|
|
import set from "lodash/set";
|
|
// services
|
|
import { NotificationService } from "services/notification.service";
|
|
// types
|
|
import { IIssueDetail } from "./root.store";
|
|
|
|
export interface IIssueSubscriptionStoreActions {
|
|
addSubscription: (issueId: string, isSubscribed: boolean | undefined | null) => void;
|
|
fetchSubscriptions: (workspaceSlug: string, projectId: string, issueId: string) => Promise<boolean>;
|
|
createSubscription: (workspaceSlug: string, projectId: string, issueId: string) => Promise<void>;
|
|
removeSubscription: (workspaceSlug: string, projectId: string, issueId: string) => Promise<void>;
|
|
}
|
|
|
|
export interface IIssueSubscriptionStore extends IIssueSubscriptionStoreActions {
|
|
// observables
|
|
subscriptionMap: Record<string, Record<string, boolean>>; // Record defines subscriptionId as key and link as value
|
|
// helper methods
|
|
getSubscriptionByIssueId: (issueId: string) => boolean | undefined;
|
|
}
|
|
|
|
export class IssueSubscriptionStore implements IIssueSubscriptionStore {
|
|
// observables
|
|
subscriptionMap: Record<string, Record<string, boolean>> = {};
|
|
// root store
|
|
rootIssueDetail: IIssueDetail;
|
|
// services
|
|
notificationService;
|
|
|
|
constructor(rootStore: IIssueDetail) {
|
|
makeObservable(this, {
|
|
// observables
|
|
subscriptionMap: observable,
|
|
// actions
|
|
addSubscription: action.bound,
|
|
fetchSubscriptions: action,
|
|
createSubscription: action,
|
|
removeSubscription: action,
|
|
});
|
|
// root store
|
|
this.rootIssueDetail = rootStore;
|
|
// services
|
|
this.notificationService = new NotificationService();
|
|
}
|
|
|
|
// helper methods
|
|
getSubscriptionByIssueId = (issueId: string) => {
|
|
if (!issueId) return undefined;
|
|
const currentUserId = this.rootIssueDetail.rootIssueStore.currentUserId;
|
|
if (!currentUserId) return undefined;
|
|
return this.subscriptionMap[issueId]?.[currentUserId] ?? undefined;
|
|
};
|
|
|
|
addSubscription = (issueId: string, isSubscribed: boolean | undefined | null) => {
|
|
const currentUserId = this.rootIssueDetail.rootIssueStore.currentUserId;
|
|
if (!currentUserId) throw new Error("user id not available");
|
|
|
|
runInAction(() => {
|
|
set(this.subscriptionMap, [issueId, currentUserId], isSubscribed ?? false);
|
|
});
|
|
};
|
|
|
|
fetchSubscriptions = async (workspaceSlug: string, projectId: string, issueId: string) => {
|
|
try {
|
|
const subscription = await this.notificationService.getIssueNotificationSubscriptionStatus(
|
|
workspaceSlug,
|
|
projectId,
|
|
issueId
|
|
);
|
|
|
|
this.addSubscription(issueId, subscription?.subscribed);
|
|
|
|
return subscription?.subscribed;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
createSubscription = async (workspaceSlug: string, projectId: string, issueId: string) => {
|
|
try {
|
|
const currentUserId = this.rootIssueDetail.rootIssueStore.currentUserId;
|
|
if (!currentUserId) throw new Error("user id not available");
|
|
|
|
runInAction(() => {
|
|
set(this.subscriptionMap, [issueId, currentUserId], { subscribed: true });
|
|
});
|
|
|
|
await this.notificationService.subscribeToIssueNotifications(workspaceSlug, projectId, issueId);
|
|
} catch (error) {
|
|
this.fetchSubscriptions(workspaceSlug, projectId, issueId);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
removeSubscription = async (workspaceSlug: string, projectId: string, issueId: string) => {
|
|
try {
|
|
const currentUserId = this.rootIssueDetail.rootIssueStore.currentUserId;
|
|
if (!currentUserId) throw new Error("user id not available");
|
|
|
|
runInAction(() => {
|
|
set(this.subscriptionMap, [issueId, currentUserId], { subscribed: false });
|
|
});
|
|
|
|
await this.notificationService.unsubscribeFromIssueNotifications(workspaceSlug, projectId, issueId);
|
|
} catch (error) {
|
|
this.fetchSubscriptions(workspaceSlug, projectId, issueId);
|
|
throw error;
|
|
}
|
|
};
|
|
}
|