fixed issue with minbookingnotice not updating properly in prisma

This commit is contained in:
123om123 2022-11-02 16:15:38 -04:00
parent c5ce792a62
commit 16e9545dba
4 changed files with 32 additions and 12 deletions

View File

@ -1,7 +1,7 @@
import { useAutoAnimate } from "@formkit/auto-animate/react";
import * as RadioGroup from "@radix-ui/react-radio-group";
import { EventTypeSetupInfered, FormValues } from "pages/event-types/[type]";
import { useState } from "react";
import { useEffect, useState } from "react";
import { useFormContext, Controller, useWatch } from "react-hook-form";
import { classNames } from "@calcom/lib";
@ -14,12 +14,18 @@ import { Icon } from "@calcom/ui";
import { Select, Switch, Label, Input, Button, SettingsToggle, InputField } from "@calcom/ui/v2";
import DateRangePicker from "@calcom/ui/v2/core/form/date-range-picker/DateRangePicker";
export const durationArray = ["minutes"];
export const durationArray: string[] = ["minutes"];
export const EventLimitsTab = (props: Pick<EventTypeSetupInfered, "eventType">) => {
const { t } = useLocale();
const formMethods = useFormContext<FormValues>();
const { eventType } = props;
let minimumBookingNoticeType = findDurationType(eventType.minimumBookingNotice);
const displayValue = convertToNewDurationType(
"minutes",
minimumBookingNoticeType,
eventType.minimumBookingNotice
);
const PERIOD_TYPES = [
{
type: "ROLLING" as const,
@ -35,6 +41,11 @@ export const EventLimitsTab = (props: Pick<EventTypeSetupInfered, "eventType">)
},
];
useEffect(() => {
durationArray.unshift(minimumBookingNoticeType);
formMethods.setValue("minimumBookingNotice", displayValue);
}, []);
const periodType =
PERIOD_TYPES.find((s) => s.type === eventType.periodType) ||
PERIOD_TYPES.find((s) => s.type === "UNLIMITED");
@ -123,8 +134,6 @@ export const EventLimitsTab = (props: Pick<EventTypeSetupInfered, "eventType">)
name="minimumBookingNotice"
control={formMethods.control}
render={() => {
let displayValue = eventType.minimumBookingNotice;
let minimumBookingNoticeType = findDurationType(displayValue);
const minBookingValue = formMethods.watch("minimumBookingNotice");
const durationTypeOptions = [
{
@ -141,8 +150,6 @@ export const EventLimitsTab = (props: Pick<EventTypeSetupInfered, "eventType">)
},
];
displayValue = convertToNewDurationType("minutes", minimumBookingNoticeType, displayValue);
return (
<>
<div className="w-1/2 md:w-3/4">
@ -155,6 +162,11 @@ export const EventLimitsTab = (props: Pick<EventTypeSetupInfered, "eventType">)
defaultValue={displayValue}
{...formMethods.register("minimumBookingNotice", {
valueAsNumber: true,
onChange: (value) => {
value.target.value
? (eventType.minimumBookingNotice = JSON.parse(value.target.value))
: null;
},
})}
/>
</div>
@ -169,10 +181,10 @@ export const EventLimitsTab = (props: Pick<EventTypeSetupInfered, "eventType">)
durationArray.unshift(val.value);
const previousValue = durationArray[1];
minimumBookingNoticeType = val.value;
formMethods.setValue(
"minimumBookingNotice",
Math.ceil(convertToNewDurationType(previousValue, val.value, minBookingValue))
eventType.minimumBookingNotice = Math.ceil(
convertToNewDurationType(previousValue, val.value, minBookingValue)
);
formMethods.setValue("minimumBookingNotice", eventType.minimumBookingNotice);
}
}}
options={durationTypeOptions}

View File

@ -157,6 +157,8 @@ const EventTypePage = (props: inferSSRProps<typeof getServerSideProps>) => {
eventType.minimumBookingNotice
);
console.log(minimumBookingNoticeInMinutes, durationArray, eventType.minimumBookingNotice);
const formMethods = useForm<FormValues>({
defaultValues: {
title: eventType.title,

View File

@ -1,11 +1,11 @@
import { MINUTES_IN_DAY, MINUTES_IN_HOUR } from "./convertToNewDurationType";
export default function convertMinimumBookingNoticeToMinutes(type: string, minNotice: number) {
if (type === "minute") {
if (type === "minutes") {
return minNotice;
} else if (type === "hour") {
} else if (type === "hours") {
return minNotice * MINUTES_IN_HOUR;
} else if (type === "day") {
} else if (type === "days") {
return minNotice * MINUTES_IN_DAY;
}
return minNotice;

View File

@ -5,23 +5,29 @@ export const HOURS_IN_DAY = 24;
export default function convertToNewDurationType(prevType: string, newType: string, prevValue: number) {
if (newType === "minutes") {
if (prevType === "hours") {
console.log("hours to minutes", prevValue * MINUTES_IN_HOUR);
return prevValue * MINUTES_IN_HOUR;
}
if (prevType === "days") {
console.log("days to minutes", prevValue * MINUTES_IN_DAY);
return prevValue * MINUTES_IN_DAY;
}
} else if (newType === "hours") {
if (prevType === "minutes") {
console.log("minutes to hours", prevValue / MINUTES_IN_HOUR);
return prevValue / MINUTES_IN_HOUR;
}
if (prevType === "days") {
console.log("days to hours", prevValue * HOURS_IN_DAY);
return prevValue * HOURS_IN_DAY;
}
} else if (newType === "days") {
if (prevType === "minutes") {
console.log("minutes to days", prevValue / MINUTES_IN_DAY);
return prevValue / MINUTES_IN_DAY;
}
if (prevType === "hours") {
console.log("hours to days", prevValue / HOURS_IN_DAY);
return prevValue / HOURS_IN_DAY;
}
}