This commit is contained in:
Daniel Hougaard
2023-11-28 00:54:55 +04:00
parent 3c2a66f722
commit a9776eaeb5
2 changed files with 36 additions and 39 deletions
@@ -8,21 +8,19 @@ import { z } from "zod";
import { Button, FormControl, Input, Modal, ModalContent, TextArea } from "@app/components/v2"; import { Button, FormControl, Input, Modal, ModalContent, TextArea } from "@app/components/v2";
interface ReminderFormProps {
isOpen: boolean;
onClose: (data?: {cron: string, note?: string}) => void;
}
const ReminderFormSchema = z.object({ const ReminderFormSchema = z.object({
note: z.string().optional(), note: z.string().optional(),
cron: z.string().refine(isValidCron, {message: "Invalid cron expression"}) cron: z.string().refine(isValidCron, { message: "Invalid cron expression" })
}); });
type TReminderFormSchema = z.infer<typeof ReminderFormSchema>; export type TReminderFormSchema = z.infer<typeof ReminderFormSchema>;
export const CreateReminderForm = ({isOpen, onClose}: ReminderFormProps) => { interface ReminderFormProps {
isOpen: boolean;
onOpenChange: (isOpen: boolean, data?: TReminderFormSchema) => void;
}
export const CreateReminderForm = ({ isOpen, onOpenChange }: ReminderFormProps) => {
const { const {
register, register,
watch, watch,
@@ -32,32 +30,27 @@ export const CreateReminderForm = ({isOpen, onClose}: ReminderFormProps) => {
const cronWatch = watch("cron"); const cronWatch = watch("cron");
const handleFormSubmit = async (data: TReminderFormSchema) => { const handleFormSubmit = async (data: TReminderFormSchema) => {
return onClose(data); onOpenChange(false, data);
} };
return ( return (
<Modal <Modal isOpen={isOpen} onOpenChange={onOpenChange}>
isOpen={isOpen}
onOpenChange={(state) => !state && onClose()}
>
<ModalContent <ModalContent
title="Create secret reminder" title="Create secret reminder"
// ? QUESTION: Should this specifically say its for secret rotation? // ? QUESTION: Should this specifically say its for secret rotation?
// ? Or should we be call it something more generic? // ? Or should we be call it something more generic?
subTitle={ subTitle={
<div> <div>
Set up a reminder for when this secret should be rotated. Set up a reminder for when this secret should be rotated. Everyone in the workspace will
<div> be notified when the reminder is triggered.
Format is in{" "} <div className="mt-1">
{/* eslint-disable-next-line react/jsx-no-target-blank */} {/* eslint-disable-next-line react/jsx-no-target-blank */}
<a target='_blank' href="https://crontab.guru/every-month"> <a target="_blank" href="https://crontab.guru/every-month">
<span className="text-primary-400 hover:text-primary-500 cursor-pointer"> <span className="cursor-pointer text-primary-400 hover:text-primary-500">
cron format. Learn more about cron expressions
</span> </span>
</a> </a>
</div> </div>
</div> </div>
} }
@@ -65,15 +58,17 @@ export const CreateReminderForm = ({isOpen, onClose}: ReminderFormProps) => {
<form onSubmit={handleSubmit(handleFormSubmit)}> <form onSubmit={handleSubmit(handleFormSubmit)}>
<div className="space-y-4"> <div className="space-y-4">
<div> <div>
<FormControl className="mb-0" label="How often" isError={Boolean(errors?.cron)} errorText={errors?.cron?.message}> <FormControl
<Input className="mb-0"
{...register("cron")} label="How often"
placeholder="0 0 1 * *" isError={Boolean(errors?.cron)}
/> errorText={errors?.cron?.message}
>
<Input {...register("cron")} placeholder="0 0 1 * *" />
</FormControl> </FormControl>
{!!cronWatch && isValidCron(cronWatch) && ( {!!cronWatch && isValidCron(cronWatch) && (
<div className="text-xs opacity-60 mt-2 ml-1">{cronstrue.toString(cronWatch)}</div> <div className="mt-2 ml-1 text-xs opacity-60">{cronstrue.toString(cronWatch)}</div>
)} )}
</div> </div>
<FormControl label="Note" className="mb-0"> <FormControl label="Note" className="mb-0">
@@ -81,7 +76,7 @@ export const CreateReminderForm = ({isOpen, onClose}: ReminderFormProps) => {
placeholder="Remember to rotate the AWS secret every month." placeholder="Remember to rotate the AWS secret every month."
className="border border-mineshaft-600 text-sm" className="border border-mineshaft-600 text-sm"
rows={8} rows={8}
reSize='none' reSize="none"
cols={30} cols={30}
{...register("note")} {...register("note")}
/> />
@@ -93,14 +88,14 @@ export const CreateReminderForm = ({isOpen, onClose}: ReminderFormProps) => {
isLoading={isSubmitting} isLoading={isSubmitting}
key="layout-create-project-submit" key="layout-create-project-submit"
className="mr-4" className="mr-4"
leftIcon={<FontAwesomeIcon icon={faClock}/>} leftIcon={<FontAwesomeIcon icon={faClock} />}
type="submit" type="submit"
> >
Create reminder Create reminder
</Button> </Button>
<Button <Button
key="layout-cancel-create-project" key="layout-cancel-create-project"
onClick={() => onClose()} onClick={() => onOpenChange(false)}
variant="plain" variant="plain"
colorSchema="secondary" colorSchema="secondary"
> >
@@ -111,4 +106,4 @@ export const CreateReminderForm = ({isOpen, onClose}: ReminderFormProps) => {
</ModalContent> </ModalContent>
</Modal> </Modal>
); );
} };
@@ -188,12 +188,14 @@ export const SecretItem = memo(
<> <>
<CreateReminderForm <CreateReminderForm
isOpen={createReminderFormOpen} isOpen={createReminderFormOpen}
onClose={(data) => { onOpenChange={(isOpen, data) => {
setCreateReminderFormOpen.toggle();
if (data) { if (data) {
console.log(data);
setValue("reminderCron", data.cron, { shouldDirty: true }); setValue("reminderCron", data.cron, { shouldDirty: true });
setValue("reminderNote", data.note, { shouldDirty: true }); setValue("reminderNote", data.note, { shouldDirty: true });
} }
setCreateReminderFormOpen.off();
}} }}
/> />