2023-09-01 11:12:30 +00:00
|
|
|
import { makeObservable, observable, action, runInAction } from "mobx";
|
2023-09-01 15:08:53 +00:00
|
|
|
import { v4 as uuidv4 } from "uuid";
|
2023-09-01 11:12:30 +00:00
|
|
|
// store
|
|
|
|
import { RootStore } from "./root";
|
|
|
|
// services
|
|
|
|
import IssueService from "services/issue.service";
|
2023-09-01 15:08:53 +00:00
|
|
|
import { IIssue, IVote } from "types/issue";
|
2023-09-01 11:12:30 +00:00
|
|
|
|
|
|
|
export type IPeekMode = "side" | "modal" | "full";
|
|
|
|
|
|
|
|
export interface IIssueDetailStore {
|
|
|
|
loader: boolean;
|
|
|
|
error: any;
|
|
|
|
// peek info
|
|
|
|
peekId: string | null;
|
|
|
|
peekMode: IPeekMode;
|
|
|
|
details: {
|
|
|
|
[key: string]: IIssue;
|
|
|
|
};
|
|
|
|
// peek actions
|
|
|
|
setPeekId: (issueId: string | null) => void;
|
|
|
|
setPeekMode: (mode: IPeekMode) => void;
|
|
|
|
// issue details
|
|
|
|
fetchIssueDetails: (workspaceId: string, projectId: string, issueId: string) => void;
|
|
|
|
// issue comments
|
|
|
|
addIssueComment: (workspaceId: string, projectId: string, issueId: string, data: any) => Promise<void>;
|
|
|
|
updateIssueComment: (
|
|
|
|
workspaceId: string,
|
|
|
|
projectId: string,
|
|
|
|
issueId: string,
|
|
|
|
comment_id: string,
|
|
|
|
data: any
|
|
|
|
) => Promise<any>;
|
|
|
|
deleteIssueComment: (workspaceId: string, projectId: string, issueId: string, comment_id: string) => void;
|
2023-09-06 06:29:57 +00:00
|
|
|
addCommentReaction: (
|
|
|
|
workspaceId: string,
|
|
|
|
projectId: string,
|
|
|
|
issueId: string,
|
|
|
|
commentId: string,
|
|
|
|
reactionHex: string
|
|
|
|
) => void;
|
|
|
|
removeCommentReaction: (
|
|
|
|
workspaceId: string,
|
|
|
|
projectId: string,
|
|
|
|
issueId: string,
|
|
|
|
commentId: string,
|
|
|
|
reactionHex: string
|
|
|
|
) => void;
|
2023-09-01 11:12:30 +00:00
|
|
|
// issue reactions
|
2023-09-01 15:08:53 +00:00
|
|
|
addIssueReaction: (workspaceId: string, projectId: string, issueId: string, reactionHex: string) => void;
|
|
|
|
removeIssueReaction: (workspaceId: string, projectId: string, issueId: string, reactionHex: string) => void;
|
2023-09-01 11:12:30 +00:00
|
|
|
// issue votes
|
|
|
|
addIssueVote: (workspaceId: string, projectId: string, issueId: string, data: { vote: 1 | -1 }) => Promise<void>;
|
|
|
|
removeIssueVote: (workspaceId: string, projectId: string, issueId: string) => Promise<void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
class IssueDetailStore implements IssueDetailStore {
|
|
|
|
loader: boolean = false;
|
|
|
|
error: any = null;
|
|
|
|
peekId: string | null = null;
|
|
|
|
peekMode: IPeekMode = "side";
|
|
|
|
details: {
|
|
|
|
[key: string]: IIssue;
|
|
|
|
} = {};
|
|
|
|
issueService;
|
|
|
|
rootStore: RootStore;
|
|
|
|
|
|
|
|
constructor(_rootStore: RootStore) {
|
|
|
|
makeObservable(this, {
|
|
|
|
loader: observable.ref,
|
|
|
|
error: observable.ref,
|
|
|
|
// peek
|
|
|
|
peekId: observable.ref,
|
|
|
|
peekMode: observable.ref,
|
|
|
|
details: observable.ref,
|
|
|
|
// actions
|
|
|
|
setPeekId: action,
|
|
|
|
setPeekMode: action,
|
2023-09-06 06:29:57 +00:00
|
|
|
fetchIssueDetails: action,
|
|
|
|
addIssueComment: action,
|
|
|
|
updateIssueComment: action,
|
|
|
|
deleteIssueComment: action,
|
|
|
|
addCommentReaction: action,
|
|
|
|
removeCommentReaction: action,
|
|
|
|
addIssueReaction: action,
|
|
|
|
removeIssueReaction: action,
|
|
|
|
addIssueVote: action,
|
|
|
|
removeIssueVote: action,
|
2023-09-01 11:12:30 +00:00
|
|
|
});
|
|
|
|
this.issueService = new IssueService();
|
|
|
|
this.rootStore = _rootStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
setPeekId = (issueId: string | null) => {
|
|
|
|
this.peekId = issueId;
|
|
|
|
};
|
|
|
|
|
|
|
|
setPeekMode = (mode: IPeekMode) => {
|
|
|
|
this.peekMode = mode;
|
|
|
|
};
|
|
|
|
|
|
|
|
fetchIssueDetails = async (workspaceSlug: string, projectId: string, issueId: string) => {
|
|
|
|
try {
|
|
|
|
this.loader = true;
|
|
|
|
this.error = null;
|
|
|
|
|
|
|
|
const issueDetails = this.rootStore.issue.issues?.find((i) => i.id === issueId);
|
|
|
|
const commentsResponse = await this.issueService.getIssueComments(workspaceSlug, projectId, issueId);
|
|
|
|
|
|
|
|
if (issueDetails) {
|
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
2023-09-01 15:22:12 +00:00
|
|
|
...(this.details[issueId] ?? issueDetails),
|
2023-09-01 11:12:30 +00:00
|
|
|
comments: commentsResponse,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.loader = false;
|
|
|
|
this.error = error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
addIssueComment = async (workspaceSlug: string, projectId: string, issueId: string, data: any) => {
|
|
|
|
try {
|
|
|
|
const issueDetails = this.rootStore.issue.issues?.find((i) => i.id === issueId);
|
|
|
|
const issueCommentResponse = await this.issueService.createIssueComment(workspaceSlug, projectId, issueId, data);
|
|
|
|
if (issueDetails) {
|
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
|
|
|
...issueDetails,
|
|
|
|
comments: [...this.details[issueId].comments, issueCommentResponse],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return issueCommentResponse;
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Failed to add issue comment");
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
updateIssueComment = async (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
issueId: string,
|
|
|
|
commentId: string,
|
|
|
|
data: any
|
|
|
|
) => {
|
|
|
|
try {
|
2023-09-06 13:32:59 +00:00
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
|
|
|
...this.details[issueId],
|
|
|
|
comments: this.details[issueId].comments.map((c) => ({
|
|
|
|
...c,
|
|
|
|
...(c.id === commentId ? data : {}),
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2023-09-01 11:12:30 +00:00
|
|
|
|
2023-09-06 13:32:59 +00:00
|
|
|
await this.issueService.updateIssueComment(workspaceSlug, projectId, issueId, commentId, data);
|
2023-09-01 11:12:30 +00:00
|
|
|
} catch (error) {
|
2023-09-06 13:32:59 +00:00
|
|
|
const issueComments = await this.issueService.getIssueComments(workspaceSlug, projectId, issueId);
|
|
|
|
|
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
|
|
|
...this.details[issueId],
|
|
|
|
comments: issueComments,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2023-09-01 11:12:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
deleteIssueComment = async (workspaceSlug: string, projectId: string, issueId: string, comment_id: string) => {
|
|
|
|
try {
|
|
|
|
await this.issueService.deleteIssueComment(workspaceSlug, projectId, issueId, comment_id);
|
|
|
|
const remainingComments = this.details[issueId].comments.filter((c) => c.id != comment_id);
|
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
|
|
|
...this.details[issueId],
|
|
|
|
comments: remainingComments,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Failed to add issue vote");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-06 06:29:57 +00:00
|
|
|
addCommentReaction = async (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
issueId: string,
|
|
|
|
commentId: string,
|
|
|
|
reactionHex: string
|
|
|
|
) => {
|
|
|
|
const newReaction = {
|
|
|
|
id: uuidv4(),
|
|
|
|
comment: commentId,
|
|
|
|
reaction: reactionHex,
|
|
|
|
actor_detail: this.rootStore.user.currentActor,
|
|
|
|
};
|
|
|
|
const newComments = this.details[issueId].comments.map((comment) => ({
|
|
|
|
...comment,
|
|
|
|
comment_reactions:
|
|
|
|
comment.id === commentId ? [...comment.comment_reactions, newReaction] : comment.comment_reactions,
|
|
|
|
}));
|
|
|
|
|
|
|
|
try {
|
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
|
|
|
...this.details[issueId],
|
|
|
|
comments: [...newComments],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
await this.issueService.createCommentReaction(workspaceSlug, projectId, commentId, {
|
|
|
|
reaction: reactionHex,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
const issueComments = await this.issueService.getIssueComments(workspaceSlug, projectId, issueId);
|
|
|
|
|
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
|
|
|
...this.details[issueId],
|
|
|
|
comments: issueComments,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
removeCommentReaction = async (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
issueId: string,
|
|
|
|
commentId: string,
|
|
|
|
reactionHex: string
|
|
|
|
) => {
|
|
|
|
try {
|
|
|
|
const comment = this.details[issueId].comments.find((c) => c.id === commentId);
|
|
|
|
const newCommentReactions = comment?.comment_reactions.filter((r) => r.reaction !== reactionHex) ?? [];
|
|
|
|
|
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
|
|
|
...this.details[issueId],
|
|
|
|
comments: this.details[issueId].comments.map((c) => ({
|
|
|
|
...c,
|
|
|
|
comment_reactions: c.id === commentId ? newCommentReactions : c.comment_reactions,
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
await this.issueService.deleteCommentReaction(workspaceSlug, projectId, commentId, reactionHex);
|
|
|
|
} catch (error) {
|
|
|
|
const issueComments = await this.issueService.getIssueComments(workspaceSlug, projectId, issueId);
|
|
|
|
|
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
|
|
|
...this.details[issueId],
|
|
|
|
comments: issueComments,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-01 15:08:53 +00:00
|
|
|
addIssueReaction = async (workspaceSlug: string, projectId: string, issueId: string, reactionHex: string) => {
|
2023-09-01 11:12:30 +00:00
|
|
|
try {
|
2023-09-01 15:08:53 +00:00
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
|
|
|
...this.details[issueId],
|
|
|
|
reactions: [
|
|
|
|
...this.details[issueId].reactions,
|
|
|
|
{
|
|
|
|
id: uuidv4(),
|
|
|
|
issue: issueId,
|
|
|
|
reaction: reactionHex,
|
|
|
|
actor_detail: this.rootStore.user.currentActor,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2023-09-01 11:12:30 +00:00
|
|
|
|
2023-09-01 15:08:53 +00:00
|
|
|
await this.issueService.createIssueReaction(workspaceSlug, projectId, issueId, {
|
|
|
|
reaction: reactionHex,
|
|
|
|
});
|
2023-09-01 11:12:30 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log("Failed to add issue vote");
|
2023-09-01 15:08:53 +00:00
|
|
|
const issueReactions = await this.issueService.getIssueReactions(workspaceSlug, projectId, issueId);
|
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
|
|
|
...this.details[issueId],
|
|
|
|
reactions: issueReactions,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2023-09-01 11:12:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-01 15:08:53 +00:00
|
|
|
removeIssueReaction = async (workspaceSlug: string, projectId: string, issueId: string, reactionHex: string) => {
|
2023-09-01 11:12:30 +00:00
|
|
|
try {
|
2023-09-01 15:08:53 +00:00
|
|
|
const newReactions = this.details[issueId].reactions.filter(
|
|
|
|
(_r) => !(_r.reaction === reactionHex && _r.actor_detail.id === this.rootStore.user.currentUser?.id)
|
|
|
|
);
|
2023-09-01 11:12:30 +00:00
|
|
|
|
2023-09-01 15:08:53 +00:00
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
|
|
|
...this.details[issueId],
|
|
|
|
reactions: newReactions,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
await this.issueService.deleteIssueReaction(workspaceSlug, projectId, issueId, reactionHex);
|
2023-09-01 11:12:30 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log("Failed to remove issue reaction");
|
2023-09-01 15:08:53 +00:00
|
|
|
const reactions = await this.issueService.getIssueReactions(workspaceSlug, projectId, issueId);
|
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
|
|
|
...this.details[issueId],
|
|
|
|
reactions: reactions,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2023-09-01 11:12:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
addIssueVote = async (workspaceSlug: string, projectId: string, issueId: string, data: { vote: 1 | -1 }) => {
|
2023-09-01 15:08:53 +00:00
|
|
|
const newVote: IVote = {
|
|
|
|
actor: this.rootStore.user.currentUser?.id ?? "",
|
|
|
|
actor_detail: this.rootStore.user.currentActor,
|
|
|
|
issue: issueId,
|
|
|
|
project: projectId,
|
|
|
|
workspace: workspaceSlug,
|
|
|
|
vote: data.vote,
|
|
|
|
};
|
|
|
|
|
|
|
|
const filteredVotes = this.details[issueId].votes.filter((v) => v.actor !== this.rootStore.user.currentUser?.id);
|
|
|
|
|
2023-09-01 11:12:30 +00:00
|
|
|
try {
|
2023-09-01 15:08:53 +00:00
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
|
|
|
...this.details[issueId],
|
|
|
|
votes: [...filteredVotes, newVote],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2023-09-01 11:12:30 +00:00
|
|
|
|
2023-09-01 15:08:53 +00:00
|
|
|
await this.issueService.createIssueVote(workspaceSlug, projectId, issueId, data);
|
2023-09-01 11:12:30 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log("Failed to add issue vote");
|
2023-09-01 15:08:53 +00:00
|
|
|
const issueVotes = await this.issueService.getIssueVotes(workspaceSlug, projectId, issueId);
|
|
|
|
|
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
|
|
|
...this.details[issueId],
|
|
|
|
votes: issueVotes,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2023-09-01 11:12:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
removeIssueVote = async (workspaceSlug: string, projectId: string, issueId: string) => {
|
2023-09-01 15:08:53 +00:00
|
|
|
const newVotes = this.details[issueId].votes.filter((v) => v.actor !== this.rootStore.user.currentUser?.id);
|
|
|
|
|
2023-09-01 11:12:30 +00:00
|
|
|
try {
|
2023-09-01 15:08:53 +00:00
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
|
|
|
...this.details[issueId],
|
|
|
|
votes: newVotes,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2023-09-01 11:12:30 +00:00
|
|
|
|
2023-09-01 15:08:53 +00:00
|
|
|
await this.issueService.deleteIssueVote(workspaceSlug, projectId, issueId);
|
2023-09-01 11:12:30 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log("Failed to remove issue vote");
|
2023-09-01 15:08:53 +00:00
|
|
|
const issueVotes = await this.issueService.getIssueVotes(workspaceSlug, projectId, issueId);
|
|
|
|
|
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
|
|
|
...this.details[issueId],
|
|
|
|
votes: issueVotes,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2023-09-01 11:12:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default IssueDetailStore;
|