Skip to content

Commit

Permalink
fix: allow to change state of goal if goal is editable only
Browse files Browse the repository at this point in the history
  • Loading branch information
awinogradov committed Jun 27, 2023
1 parent dc71266 commit 39a3098
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
12 changes: 8 additions & 4 deletions src/components/GoalPage/GoalPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { IssueMeta } from '../IssueMeta';
import { UserBadge } from '../UserBadge';
import { InlineTrigger } from '../InlineTrigger';
import { UserComboBox } from '../UserComboBox';
import { State } from '../State';

import { tr } from './GoalPage.i18n';

Expand Down Expand Up @@ -240,10 +241,13 @@ export const GoalPage = ({ user, ssrTime, params: { id } }: ExternalPageProps<{

<IssueTitle title={goal.title} />

{/* FIXME: must be interactive if goal is editable https://github.com/taskany-inc/issues/issues/1165 */}
{nullable(goal.state, (s) => (
<StateSwitch state={s} flowId={project?.flowId} onClick={onGoalStateChange} />
))}
{nullable(goal.state, (s) =>
goal._isEditable ? (
<StateSwitch state={s} flowId={project?.flowId} onClick={onGoalStateChange} />
) : (
<State title={s.title} hue={s.hue} />
),
)}

<IssueStats
estimate={goal._lastEstimate}
Expand Down
11 changes: 8 additions & 3 deletions src/components/GoalPreview/GoalPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { GoalStateChangeSchema } from '../../schema/goal';
import { GoalActivity } from '../GoalActivity';
import { GoalCriteria } from '../GoalCriteria/GoalCriteria';
import { CriteriaForm } from '../CriteriaForm/CriteriaForm';
import { State } from '../State';

import { tr } from './GoalPreview.i18n';

Expand Down Expand Up @@ -217,9 +218,13 @@ const GoalPreview: React.FC<GoalPreviewProps> = ({ preview, onClose, onDelete })

<StyledImportantActions>
<StyledPublicActions>
{nullable(goal?.state, (s) => (
<StateSwitch state={s} flowId={goal?.project?.flowId} onClick={onGoalStateChange} />
))}
{nullable(goal?.state, (s) =>
goal?._isEditable ? (
<StateSwitch state={s} flowId={goal?.project?.flowId} onClick={onGoalStateChange} />
) : (
<State title={s.title} hue={s.hue} />
),
)}

<IssueStats
mode="compact"
Expand Down
30 changes: 22 additions & 8 deletions trpc/router/goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@ export const goal = router({

if (!actualGoal) return null;

const { _isEditable } = addCalclulatedGoalsFields(actualGoal, ctx.session.user.activityId);

if (!_isEditable) {
return null;
}

const tagsToDisconnect = calculateDiffBetweenArrays(actualGoal.tags, input.tags);
const tagsToConnect = calculateDiffBetweenArrays(input.tags, actualGoal.tags);

Expand Down Expand Up @@ -620,6 +626,12 @@ export const goal = router({
return null;
}

const { _isEditable } = addCalclulatedGoalsFields(actualGoal, ctx.session.user.activityId);

if (!_isEditable) {
return null;
}

try {
return await prisma.goal.update({
where: {
Expand All @@ -644,7 +656,7 @@ export const goal = router({
}
}),
createComment: protectedProcedure.input(goalCreateCommentSchema).mutation(async ({ ctx, input }) => {
const [commentAuthor, goal] = await Promise.all([
const [commentAuthor, actualGoal] = await Promise.all([
prisma.activity.findUnique({
where: { id: ctx.session.user.activityId },
include: { user: true, ghost: true },
Expand All @@ -659,7 +671,9 @@ export const goal = router({
]);

if (!commentAuthor) return null;
if (!goal) return null;
if (!actualGoal) return null;

const { _isEditable } = addCalclulatedGoalsFields(actualGoal, ctx.session.user.activityId);

try {
// We want to see state changes record and comment next in activity feed.
Expand All @@ -669,14 +683,14 @@ export const goal = router({
where: { id: input.id },
data: {
id: input.id,
stateId: input.stateId,
stateId: _isEditable ? input.stateId : actualGoal.stateId,
history:
input.stateId && input.stateId !== goal.stateId
_isEditable && input.stateId && input.stateId !== actualGoal.stateId
? {
create: {
subject: 'state',
action: 'change',
previousValue: goal.stateId,
previousValue: actualGoal.stateId,
nextValue: input.stateId,
activityId: ctx.session.user.activityId,
},
Expand All @@ -690,14 +704,14 @@ export const goal = router({
description: input.description,
activityId: commentAuthor.id,
goalId: input.id,
stateId: input.stateId,
stateId: _isEditable ? input.stateId : undefined,
},
}),
]);

let toEmails = goal.participants;
let toEmails = actualGoal.participants;

if (commentAuthor.user?.email === goal.activity?.user?.email) {
if (commentAuthor.user?.email === actualGoal.activity?.user?.email) {
toEmails = toEmails.filter((p) => p.user?.email !== commentAuthor?.user?.email);
}

Expand Down

0 comments on commit 39a3098

Please sign in to comment.