From 83f520618bbd0a673f5a19bd495407a6d23b4753 Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 6 Oct 2023 15:31:15 +0300 Subject: [PATCH] fix: estimate unexpected update --- src/components/Estimate/Estimate.tsx | 2 +- src/components/EstimateDate/EstimateDate.tsx | 15 +++++++-------- trpc/router/goal.ts | 9 +++++++-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/components/Estimate/Estimate.tsx b/src/components/Estimate/Estimate.tsx index 1245c6f5a..5a6939c15 100644 --- a/src/components/Estimate/Estimate.tsx +++ b/src/components/Estimate/Estimate.tsx @@ -75,7 +75,7 @@ export const Estimate = React.forwardRef(({ value return year ? { range: createDateRange(year), - type: DateType.Quarter, + type: DateType.Year, } : undefined; } diff --git a/src/components/EstimateDate/EstimateDate.tsx b/src/components/EstimateDate/EstimateDate.tsx index fcedc76dd..651a4cf8d 100644 --- a/src/components/EstimateDate/EstimateDate.tsx +++ b/src/components/EstimateDate/EstimateDate.tsx @@ -20,23 +20,23 @@ export const EstimateDate: React.FC = () => { const locale = useLocale(); const currentDate = currentLocaleDate({ locale }); - const { readOnly, setReadOnly, setDate } = useEstimateContext(); + const { readOnly, setReadOnly, setDate, date } = useEstimateContext(); - const [fullDate, setFullDate] = useState(currentDate); + const valueString = date ? createLocaleDate(date, { locale }) : undefined; + + const [fullDate, setFullDate] = useState(valueString ?? currentDate); useEffect(() => { if (!readOnly.date) { setFullDate((oldDate) => (isDateFullyFilled(oldDate) ? oldDate : currentDate)); - } else { - setFullDate(currentDate); } }, [readOnly.date, currentDate]); useEffect(() => { - if (isDateFullyFilled(fullDate)) { + if (isDateFullyFilled(fullDate) && valueString !== fullDate) { setDate(parseLocaleDate(fullDate, { locale })); } - }, [fullDate, setDate, locale]); + }, [fullDate, setDate, locale, valueString]); const onInputChange = useCallback( (e: React.ChangeEvent) => { @@ -69,8 +69,7 @@ export const EstimateDate: React.FC = () => { year: true, quarter: true, }); - setFullDate(currentDate); - }, [currentDate, setReadOnly]); + }, [setReadOnly]); return ( { const { activityId, role } = ctx.session.user; - const [estimate, estimateType] = input.estimate ? [new Date(input.estimate.date), input.estimate.type] : []; + const [estimate, estimateType = null] = input.estimate + ? [new Date(input.estimate.date), input.estimate.type] + : []; const actualGoal = await prisma.goal.findUnique({ where: { id: input.id }, @@ -411,7 +413,10 @@ export const goal = router({ }); } - if (Number(estimate) !== Number(actualGoal.estimate) || estimateType !== actualGoal.estimateType) { + const isDateChanged = (Number(estimate) || 0) !== (Number(actualGoal.estimate) || 0); + const isTypeChanged = estimateType !== actualGoal.estimateType; + + if (isDateChanged || isTypeChanged) { const prevHistoryEstimate = actualGoal.estimate ? encodeHistoryEstimate(actualGoal.estimate, actualGoal.estimateType ?? 'Strict') : null;