Skip to content

Commit

Permalink
fix: added priority model instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisVorop committed Oct 12, 2023
1 parent 188c4da commit bdeeb35
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 69 deletions.
2 changes: 2 additions & 0 deletions src/components/FiltersPanel/FiltersPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export const FiltersPanel: FC<{
useQueryOptions,
);
const { data: states = [] } = trpc.state.all.useQuery();
const { data: priorities = [] } = trpc.priority.getAll.useQuery();

const setPartialQueryByKey = useCallback(<K extends keyof QueryState>(key: K) => {
return (value: QueryState[K]) => {
Expand Down Expand Up @@ -260,6 +261,7 @@ export const FiltersPanel: FC<{
<PriorityFilter
text={tr('Priority')}
value={filterQuery?.priority}
priorities={priorities}
onChange={setPartialQueryByKey('priority')}
/>

Expand Down
5 changes: 2 additions & 3 deletions src/components/GoalActivity.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { forwardRef } from 'react';
import { nullable } from '@taskany/bricks';

import { Priority } from '../types/priority';
import { GoalByIdReturnType } from '../../trpc/inferredTypes';
import { HistoryAction } from '../types/history';

Expand Down Expand Up @@ -85,8 +84,8 @@ export const GoalActivity = forwardRef<HTMLDivElement, GoalActivityProps>(
)}
{value.subject === 'priority' && (
<HistoryRecordPriority
from={value.previousValue as Priority}
to={value.nextValue as Priority}
from={excludeString(value.previousValue)}
to={excludeString(value.nextValue)}
/>
)}
{value.subject === 'state' && (
Expand Down
7 changes: 5 additions & 2 deletions src/components/GoalCreateForm/GoalCreateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ const GoalCreateForm: React.FC<GoalCreateFormProps> = ({ title, onGoalCreate })
const [createGoalType, setСreateGoalType] = useState<number>(goalCreateFormActionCache || 3);
const utils = trpc.useContext();
const { goalCreate } = useGoalResource({});
// FIXME https://github.com/taskany-inc/issues/issues/1834
const { data: priorities } = trpc.priority.getAll.useQuery();
const defaultPriority = priorities?.at(-2);

const createOptions = [
{
Expand Down Expand Up @@ -130,8 +133,8 @@ const GoalCreateForm: React.FC<GoalCreateFormProps> = ({ title, onGoalCreate })
validitySchema={goalCommonSchema}
owner={{ id: user?.activityId, user } as ActivityByIdReturnType}
parent={currentProjectCache || lastProjectCache || undefined}
priority="Medium"
onSumbit={createGoal}
priority={defaultPriority ?? undefined}
onSubmit={createGoal}
title={title}
actionButton={
<>
Expand Down
2 changes: 1 addition & 1 deletion src/components/GoalEditForm/GoalEditForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const GoalEditForm: React.FC<GoalEditFormProps> = ({ goal, onSubmit }) => {
priority={goal.priority ?? undefined}
tags={goal.tags}
estimate={estimateValue}
onSumbit={updateGoal}
onSubmit={updateGoal}
actionButton={
<>
<Button outline text={tr('Cancel')} onClick={dispatchModalEvent(ModalEvent.GoalEditModal)} />
Expand Down
15 changes: 9 additions & 6 deletions src/components/GoalForm/GoalForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { errorsProvider } from '../../utils/forms';
import { formateEstimate } from '../../utils/dateTime';
import { DateType } from '../../types/date';
import { useLocale } from '../../hooks/useLocale';
import { Priority } from '../../types/priority';
import { UserComboBox } from '../UserComboBox';
import { GoalParentComboBox } from '../GoalParentComboBox';
import { TagComboBox } from '../TagComboBox';
Expand Down Expand Up @@ -50,7 +49,11 @@ interface GoalFormProps extends React.HTMLAttributes<HTMLDivElement> {
parent?: { id: string; title: string; flowId: string; description?: string | null };
tags?: TagModel[];
state?: State;
priority?: Priority | string;
priority?: {
id: string;
title: string;
value: number;
};
estimate?: {
date: string;
type: DateType;
Expand All @@ -60,7 +63,7 @@ interface GoalFormProps extends React.HTMLAttributes<HTMLDivElement> {
id?: string;
tip?: React.ReactNode;

onSumbit: (fields: z.infer<GoalFormProps['validitySchema']>) => void;
onSubmit: (fields: z.infer<GoalFormProps['validitySchema']>) => void;
}

export const GoalForm: React.FC<GoalFormProps> = ({
Expand All @@ -77,7 +80,7 @@ export const GoalForm: React.FC<GoalFormProps> = ({
validitySchema,
actionButton,
tip,
onSumbit,
onSubmit,
...attrs
}) => {
const locale = useLocale();
Expand All @@ -94,7 +97,7 @@ export const GoalForm: React.FC<GoalFormProps> = ({
mode: 'onChange',
reValidateMode: 'onChange',
shouldFocusError: false,
defaultValues: {
values: {
title,
description,
owner,
Expand Down Expand Up @@ -125,7 +128,7 @@ export const GoalForm: React.FC<GoalFormProps> = ({

return (
<ModalContent {...attrs}>
<Form onSubmit={handleSubmit(onSumbit)}>
<Form onSubmit={handleSubmit(onSubmit)}>
<FormInput
{...register('title')}
error={errorsResolver('title')}
Expand Down
26 changes: 15 additions & 11 deletions src/components/HistoryRecord/HistoryRecord.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
GoalAchieveCriteria,
Goal,
Ghost,
Priority,
} from '@prisma/client';
import styled, { css } from 'styled-components';
import { UserPic, Text, Tag, nullable, Button } from '@taskany/bricks';
Expand All @@ -17,7 +18,6 @@ import { backgroundColor, gray7 } from '@taskany/colors';
import { ActivityFeedItem } from '../ActivityFeed';
import { IssueListItem } from '../IssueListItem';
import { RelativeTime } from '../RelativeTime/RelativeTime';
import { Priority } from '../../types/priority';
import { decodeHistoryEstimate, formateEstimate } from '../../utils/dateTime';
import { getPriorityText } from '../PriorityText/PriorityText';
import { StateDot } from '../StateDot';
Expand Down Expand Up @@ -395,16 +395,20 @@ export const HistoryRecordTextChange: React.FC<HistoryChangeProps<string>> = ({

export const HistoryRecordPriority: React.FC<HistoryChangeProps<Priority>> = ({ from, to }) => (
<HistorySimplifyRecord
from={nullable(from, (f) => (
<Text size="xs" weight="bold">
{getPriorityText(f)}
</Text>
))}
to={nullable(to, (t) => (
<Text size="xs" weight="bold">
{getPriorityText(t)}
</Text>
))}
from={
from ? (
<Text size="xs" weight="bold">
{getPriorityText(from.title)}
</Text>
) : null
}
to={
to ? (
<Text size="xs" weight="bold">
{getPriorityText(to.title)}
</Text>
) : null
}
/>
);

Expand Down
19 changes: 11 additions & 8 deletions src/components/PriorityDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
import React from 'react';
import { Button, Dropdown, MenuItem } from '@taskany/bricks';

import { Priority, priorityVariants } from '../types/priority';
import { comboboxItem, priorityCombobox } from '../utils/domObjects';
import { trpc } from '../utils/trpcClient';
import { PriorityReturnType } from '../../trpc/inferredTypes';

import { getPriorityText } from './PriorityText/PriorityText';
import { CommonDropdown } from './CommonDropdown';

interface PriorityDropdownProps {
text: React.ComponentProps<typeof Button>['text'];
value?: string | null;
value?: PriorityReturnType;
disabled?: React.ComponentProps<typeof Dropdown>['disabled'];
error?: React.ComponentProps<typeof Dropdown>['error'];

onChange?: (priority: Priority) => void;
onChange?: (priority: PriorityReturnType) => void;
}

export const PriorityDropdown = React.forwardRef<HTMLDivElement, PriorityDropdownProps>(
({ text, value, disabled, error, onChange }, ref) => {
const { data = [] } = trpc.priority.getAll.useQuery();

return (
<CommonDropdown
ref={ref}
error={error}
text={value || text}
value={value}
text={value?.title || text}
value={value?.title}
onChange={onChange}
items={Object.keys(priorityVariants)}
items={data}
disabled={disabled}
renderTrigger={(props) => (
<Button
Expand All @@ -39,12 +42,12 @@ export const PriorityDropdown = React.forwardRef<HTMLDivElement, PriorityDropdow
renderItem={(props) => (
<MenuItem
ghost
key={props.item}
key={props.item.id}
focused={props.cursor === props.index}
onClick={props.onClick}
{...comboboxItem.attr}
>
{getPriorityText(props.item)}
{getPriorityText(props.item.title)}
</MenuItem>
)}
/>
Expand Down
40 changes: 18 additions & 22 deletions src/components/PriorityFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
import { FC, useMemo } from 'react';
import { Tab } from '@taskany/bricks';

import { Priority as PriorityName, priorityVariants } from '../types/priority';

import { getPriorityText } from './PriorityText/PriorityText';
import { FilterCheckbox } from './FilterCheckbox';
import { FilterBase } from './FilterBase/FilterBase';
import { FilterTabLabel } from './FilterTabLabel';

type Variant = keyof typeof priorityVariants;

type Priority = {
id: Variant;
data: string;
};

const priorities: Priority[] = (Object.keys(priorityVariants) as Variant[]).map((p) => ({
id: p,
data: getPriorityText(p as PriorityName),
}));

function getKey(item: Priority) {
return item.id;
interface Priority {
id: string;
title: string;
value: number;
}

export const PriorityFilter: FC<{
interface PriorityFilterProps {
text: string;
value?: string[];
priorities: Priority[];
onChange: (value: string[]) => void;
}> = ({ value = [], onChange, text }) => {
}

const getKey = (priority: Priority) => priority.id;

export const PriorityFilter: FC<PriorityFilterProps> = ({ text, value = [], priorities, onChange }) => {
const values = useMemo(() => {
return priorities.filter((p) => value.includes(getKey(p)));
}, [value]);
}, [value, priorities]);

return (
<Tab name="priority" label={<FilterTabLabel text={text} selected={values.map(({ data }) => data)} />}>
<Tab
name="priority"
label={<FilterTabLabel text={text} selected={values.map(({ title }) => getPriorityText(title))} />}
>
<FilterBase
key="priority"
mode="multiple"
Expand All @@ -46,10 +42,10 @@ export const PriorityFilter: FC<{
renderItem={({ item, checked, onItemClick }) => (
<FilterCheckbox
name="priority"
value={item.id}
value={getKey(item)}
checked={checked}
onClick={onItemClick}
label={item.data}
label={getPriorityText(item.title)}
/>
)}
/>
Expand Down
12 changes: 10 additions & 2 deletions src/schema/goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ export const goalCommonSchema = z.object({
hue: z.number().optional(),
title: z.string().optional(),
}),
priority: z.string().nullable().optional(),
priority: z.object({
id: z.string(),
title: z.string(),
value: z.number(),
}),
estimate: z
.object({
date: z.string(),
Expand Down Expand Up @@ -142,7 +146,11 @@ export const goalUpdateSchema = z.object({
StateType.NotStarted,
]),
}),
priority: z.string().nullable(),
priority: z.object({
id: z.string(),
title: z.string(),
value: z.number(),
}),
estimate: z
.object({
date: z.string(),
Expand Down
16 changes: 15 additions & 1 deletion src/types/history.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { Goal, State, Project, Tag, Activity, GoalHistory, User, GoalAchieveCriteria, Ghost } from '@prisma/client';
import {
Goal,
State,
Project,
Tag,
Activity,
GoalHistory,
User,
GoalAchieveCriteria,
Ghost,
Priority,
} from '@prisma/client';

export const subjectToTableNameMap = {
dependencies: true,
Expand All @@ -9,6 +20,7 @@ export const subjectToTableNameMap = {
state: true,
criteria: true,
partnerProject: true,
priority: true,
};

export type HistoryRecordSubject = { [K in keyof typeof subjectToTableNameMap]: string };
Expand All @@ -22,6 +34,7 @@ export interface HistoryRecordMeta {
state: State;
criteria: GoalAchieveCriteria & { goalAsCriteria: Goal & { state: State | null } };
partnerProject: Project;
priority: Priority;
}

export type HistoryAction = 'add' | 'change' | 'remove' | 'delete' | 'edit' | 'complete' | 'uncomplete';
Expand All @@ -46,6 +59,7 @@ type HistoryRecord =
| HistoryValuesBySubject<'state'>
| HistoryValuesBySubject<'owner'>
| HistoryValuesBySubject<'criteria'>
| HistoryValuesBySubject<'priority'>
| {
subject: 'title' | 'description' | 'estimate';
previousValue?: string;
Expand Down
Loading

0 comments on commit bdeeb35

Please sign in to comment.