Skip to content

Commit

Permalink
fix: estimate unexpected update
Browse files Browse the repository at this point in the history
  • Loading branch information
asabotovich committed Oct 6, 2023
1 parent 14d8c89 commit 83f5206
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/components/Estimate/Estimate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const Estimate = React.forwardRef<HTMLDivElement, EstimateProps>(({ value
return year
? {
range: createDateRange(year),
type: DateType.Quarter,
type: DateType.Year,
}
: undefined;
}
Expand Down
15 changes: 7 additions & 8 deletions src/components/EstimateDate/EstimateDate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(currentDate);
const valueString = date ? createLocaleDate(date, { locale }) : undefined;

const [fullDate, setFullDate] = useState<string>(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<HTMLInputElement>) => {
Expand Down Expand Up @@ -69,8 +69,7 @@ export const EstimateDate: React.FC = () => {
year: true,
quarter: true,
});
setFullDate(currentDate);
}, [currentDate, setReadOnly]);
}, [setReadOnly]);

return (
<EstimateOption
Expand Down
9 changes: 7 additions & 2 deletions trpc/router/goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,9 @@ export const goal = router({
.use(goalAccessMiddleware)
.mutation(async ({ ctx, input }) => {
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 },
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 83f5206

Please sign in to comment.