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;
|
|
|
|
// 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,
|
|
|
|
fetchIssueDetails: action,
|
|
|
|
setPeekMode: action,
|
|
|
|
});
|
|
|
|
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]: {
|
|
|
|
...issueDetails,
|
|
|
|
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 {
|
|
|
|
const issueCommentUpdateResponse = await this.issueService.updateIssueComment(
|
|
|
|
workspaceSlug,
|
|
|
|
projectId,
|
|
|
|
issueId,
|
|
|
|
commentId,
|
|
|
|
data
|
|
|
|
);
|
|
|
|
|
|
|
|
if (issueCommentUpdateResponse) {
|
|
|
|
const remainingComments = this.details[issueId].comments.filter((com) => com.id != commentId);
|
|
|
|
runInAction(() => {
|
|
|
|
this.details = {
|
|
|
|
...this.details,
|
|
|
|
[issueId]: {
|
|
|
|
...this.details[issueId],
|
|
|
|
comments: [...remainingComments, issueCommentUpdateResponse],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return issueCommentUpdateResponse;
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Failed to add issue comment");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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-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;
|