fix: use watch and controller for neverexpires/datepicker

pull/2277/head
Agusti Fernandez Pardo 2022-04-14 19:10:33 +02:00
parent 4142af7d92
commit d9c6be973c
2 changed files with 15 additions and 20 deletions

View File

@ -1,7 +1,7 @@
import { ClipboardCopyIcon } from "@heroicons/react/solid"; import { ClipboardCopyIcon } from "@heroicons/react/solid";
import dayjs from "dayjs"; import dayjs from "dayjs";
import { useState } from "react"; import { useState } from "react";
import { useForm } from "react-hook-form"; import { Controller, useForm } from "react-hook-form";
import { useLocale } from "@calcom/lib/hooks/useLocale"; import { useLocale } from "@calcom/lib/hooks/useLocale";
import showToast from "@calcom/lib/notification"; import showToast from "@calcom/lib/notification";
@ -19,16 +19,16 @@ import { TApiKeys } from "./ApiKeyListItem";
export default function ApiKeyDialogForm(props: { export default function ApiKeyDialogForm(props: {
title: string; title: string;
defaultValues?: Omit<TApiKeys, "userId" | "createdAt" | "lastUsedAt">; defaultValues?: Omit<TApiKeys, "userId" | "createdAt" | "lastUsedAt"> & { neverExpires: boolean };
handleClose: () => void; handleClose: () => void;
}) { }) {
const { t } = useLocale(); const { t } = useLocale();
const utils = trpc.useContext(); const utils = trpc.useContext();
const [neverExpires, setNeverExpires] = useState(false);
const { const {
defaultValues = { defaultValues = {
note: "", note: "",
nexerExpires: false,
expiresAt: dayjs().add(1, "month").toDate(), expiresAt: dayjs().add(1, "month").toDate(),
}, },
} = props; } = props;
@ -51,6 +51,8 @@ export default function ApiKeyDialogForm(props: {
const form = useForm({ const form = useForm({
defaultValues, defaultValues,
}); });
const watchNeverExpires = form.watch("neverExpires");
return ( return (
<> <>
{successfulNewApiKeyModal ? ( {successfulNewApiKeyModal ? (
@ -95,7 +97,7 @@ export default function ApiKeyDialogForm(props: {
</DialogFooter> </DialogFooter>
</> </>
) : ( ) : (
<Form<Omit<TApiKeys, "userId" | "createdAt" | "lastUsedAt">> <Form<Omit<TApiKeys, "userId" | "createdAt" | "lastUsedAt"> & { neverExpires: boolean }>
form={form} form={form}
handleSubmit={async (event) => { handleSubmit={async (event) => {
const newApiKey = await utils.client.mutation("viewer.apiKeys.create", event); const newApiKey = await utils.client.mutation("viewer.apiKeys.create", event);
@ -114,30 +116,23 @@ export default function ApiKeyDialogForm(props: {
placeholder={t("personal_note_placeholder")} placeholder={t("personal_note_placeholder")}
{...form.register("note")} {...form.register("note")}
type="text" type="text"
// onChange={handleNoteChange}
/> />
<div className="flex flex-col"> <div className="flex flex-col">
<div className="flex justify-between py-2"> <div className="flex justify-between py-2">
<span className="block text-sm font-medium text-gray-700">{t("expire_date")}</span> <span className="block text-sm font-medium text-gray-700">{t("expire_date")}</span>
<Switch <Controller
label={t("never_expire_key")} name="neverExpires"
defaultChecked={neverExpires} render={({ field: { onChange, value } }) => (
onCheckedChange={(isChecked) => { <Switch label={t("never_expire_key")} onCheckedChange={onChange} checked={value} />
if (isChecked) { )}
form.setValue("expiresAt", null);
setNeverExpires(true);
} else {
setNeverExpires(false);
form.setValue("expiresAt", form.getValues().expiresAt || new Date());
}
}}
/> />
</div> </div>
<DatePicker <DatePicker
disabled={form.getValues().expiresAt === null} disabled={watchNeverExpires}
minDate={new Date()} minDate={new Date()}
date={selectedDate as Date} date={selectedDate}
onDatesChange={handleDateChange} onDatesChange={handleDateChange}
/> />
</div> </div>

View File

@ -35,7 +35,7 @@ export default function ApiKeyListItem(props: { apiKey: TApiKeys; onEditApiKey:
<div className="flex max-w-full flex-col truncate"> <div className="flex max-w-full flex-col truncate">
<div className="flex space-x-2"> <div className="flex space-x-2">
<span className="text-gray-900"> <span className="text-gray-900">
{props?.apiKey?.note ? props?.apiKey?.note : t("api_key_no_note")} {props?.apiKey?.note ? props.apiKey.note : t("api_key_no_note")}
</span> </span>
{!neverExpires && isExpired && ( {!neverExpires && isExpired && (
<Badge className="-p-2" variant="default"> <Badge className="-p-2" variant="default">