Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing additional feedback from Workflows PR #3456

Merged
merged 7 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 21 additions & 20 deletions apps/web/ee/components/workflows/WorkflowDetailsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WorkflowActions, WorkflowTemplates } from "@prisma/client";
import { useRouter } from "next/router";
import { useState, useEffect, Dispatch, SetStateAction } from "react";
import { useState, useEffect, Dispatch, SetStateAction, useMemo } from "react";
CarinaWolli marked this conversation as resolved.
Show resolved Hide resolved
import { Controller, UseFormReturn } from "react-hook-form";

import { useLocale } from "@calcom/lib/hooks/useLocale";
Expand All @@ -10,14 +10,14 @@ import { Button } from "@calcom/ui";
import { Form } from "@calcom/ui/form/fields";
import { AddActionDialog } from "@ee/components/workflows/AddActionDialog";
import WorkflowStepContainer from "@ee/components/workflows/WorkflowStepContainer";
import { Option, FormValues } from "@ee/pages/workflows/[workflow]";
import { FormValues } from "@ee/pages/workflows/[workflow]";

import { trpc } from "@lib/trpc";

import MultiSelectCheckboxes from "@components/ui/form/MultiSelectCheckboxes";
import MultiSelectCheckboxes, { Option } from "@components/ui/form/MultiSelectCheckboxes";

interface Props {
form: UseFormReturn<FormValues, any>;
form: UseFormReturn<FormValues>;
workflowId: number;
selectedEventTypes: Option[];
setSelectedEventTypes: Dispatch<SetStateAction<Option[]>>;
Expand All @@ -29,30 +29,31 @@ export default function WorkflowDetailsPage(props: Props) {
const router = useRouter();
const utils = trpc.useContext();

const [evenTypeOptions, setEventTypeOptions] = useState<Option[]>([]);
const [isAddActionDialogOpen, setIsAddActionDialogOpen] = useState(false);
const [reload, setReload] = useState(false);
const [editCounter, setEditCounter] = useState(0);

const { data, isLoading } = trpc.useQuery(["viewer.eventTypes"]);

useEffect(() => {
if (data) {
let options: Option[] = [];
data.eventTypeGroups.forEach((group) => {
const eventTypeOptions = group.eventTypes.map((eventType) => {
return { value: String(eventType.id), label: eventType.title };
});
options = [...options, ...eventTypeOptions];
});
setEventTypeOptions(options);
}
}, [isLoading]);
const eventTypeOptions = useMemo(
() =>
data?.eventTypeGroups.reduce(
(options, group) => [
...options,
...group.eventTypes.map((eventType) => ({
value: String(eventType.id),
label: eventType.title,
})),
],
[] as Option[]
) || [],
[data]
);

const updateMutation = trpc.useMutation("viewer.workflows.update", {
onSuccess: async ({ workflow }) => {
if (workflow) {
await utils.setQueryData(["viewer.workflows.get", { id: +workflow.id }], workflow);
utils.setQueryData(["viewer.workflows.get", { id: +workflow.id }], workflow);

showToast(
t("workflow_updated_successfully", {
Expand All @@ -74,7 +75,7 @@ export default function WorkflowDetailsPage(props: Props) {
const addAction = (action: WorkflowActions, sendTo?: string) => {
const steps = form.getValues("steps");
const id =
steps && steps.length > 0
steps?.length > 0
? steps.sort((a, b) => {
return a.id - b.id;
})[0].id - 1
Expand Down Expand Up @@ -130,7 +131,7 @@ export default function WorkflowDetailsPage(props: Props) {
render={() => {
return (
<MultiSelectCheckboxes
options={evenTypeOptions}
options={eventTypeOptions}
isLoading={isLoading}
setSelected={setSelectedEventTypes}
selected={selectedEventTypes}
Expand Down
5 changes: 2 additions & 3 deletions apps/web/ee/components/workflows/WorkflowStepContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { Dispatch, SetStateAction, useState } from "react";
import { Controller, UseFormReturn } from "react-hook-form";
import PhoneInput from "react-phone-number-input";

import classNames from "@calcom/lib/classNames";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { Button } from "@calcom/ui";
import Dropdown, { DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@calcom/ui/Dropdown";
import Select from "@calcom/ui/form/Select";
Expand All @@ -23,9 +25,6 @@ import {
} from "@ee/lib/workflows/getOptions";
import { FormValues } from "@ee/pages/workflows/[workflow]";

import classNames from "@lib/classNames";
import { useLocale } from "@lib/hooks/useLocale";

type WorkflowStepProps = {
step?: WorkflowStep;
form: UseFormReturn<FormValues, any>;
CarinaWolli marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
5 changes: 4 additions & 1 deletion apps/web/ee/lib/workflows/reminders/emailReminderManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export const scheduleEmailReminder = async (
const scheduledDate =
timeBefore.time && timeUnit ? dayjs(startTime).subtract(timeBefore.time, timeUnit) : null;

if (!process.env.SENDGRID_API_KEY || !process.env.SENDGRID_EMAIL) return;
if (!process.env.SENDGRID_API_KEY || !process.env.SENDGRID_EMAIL) {
console.error("Sendgrid credentials are missing from the .env file");
return;
}

const batchIdResponse = await client.request({
url: "/v3/mail/batch",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/ee/lib/workflows/reminders/smsReminderManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
WorkflowTemplates,
WorkflowActions,
WorkflowMethods,
} from "@prisma/client/";
} from "@prisma/client";

import dayjs from "@calcom/dayjs";
import prisma from "@calcom/prisma";
Expand Down
61 changes: 27 additions & 34 deletions apps/web/ee/pages/workflows/[workflow].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ import useMeQuery from "@lib/hooks/useMeQuery";
import { trpc } from "@lib/trpc";

import Shell from "@components/Shell";

export type Option = {
value: string;
label: string;
};
import { Option } from "@components/ui/form/MultiSelectCheckboxes";

export type FormValues = {
name: string;
Expand All @@ -39,6 +35,30 @@ export type FormValues = {
timeUnit?: TimeUnit;
};

const formSchema = z.object({
name: z.string(),
activeOn: z.object({ value: z.string(), label: z.string() }).array(),
trigger: z.enum(WORKFLOW_TRIGGER_EVENTS),
time: z.number().gte(0).optional(),
timeUnit: z.enum(TIME_UNIT).optional(),
steps: z
.object({
id: z.number(),
stepNumber: z.number(),
action: z.enum(WORKFLOW_ACTIONS),
workflowId: z.number(),
reminderBody: z.string().optional().nullable(),
emailSubject: z.string().optional().nullable(),
template: z.enum(WORKFLOW_TEMPLATES),
sendTo: z
.string()
.refine((val) => isValidPhoneNumber(val))
.optional()
.nullable(),
})
.array(),
});

function WorkflowPage() {
const { t } = useLocale();
const session = useSession();
Expand All @@ -50,30 +70,6 @@ function WorkflowPage() {
const [selectedEventTypes, setSelectedEventTypes] = useState<Option[]>([]);
const [isAllDataLoaded, setIsAllDataLoaded] = useState(false);

const formSchema = z.object({
name: z.string(),
activeOn: z.object({ value: z.string(), label: z.string() }).array(),
trigger: z.enum(WORKFLOW_TRIGGER_EVENTS),
time: z.number().gte(0).optional(),
timeUnit: z.enum(TIME_UNIT).optional(),
steps: z
.object({
id: z.number(),
stepNumber: z.number(),
action: z.enum(WORKFLOW_ACTIONS),
workflowId: z.number(),
reminderBody: z.string().optional().nullable(),
emailSubject: z.string().optional().nullable(),
template: z.enum(WORKFLOW_TEMPLATES),
sendTo: z
.string()
.refine((val) => isValidPhoneNumber(val))
.optional()
.nullable(),
})
.array(),
});

const form = useForm<FormValues>({
resolver: zodResolver(formSchema),
});
Expand Down Expand Up @@ -113,15 +109,12 @@ function WorkflowPage() {
}
}, [dataUpdatedAt]);

if (isLoading) {
return <Loader />;
}

return (
<Shell
title="Title"
heading={
session.data?.hasValidLicense && (
session.data?.hasValidLicense &&
isAllDataLoaded && (
<div className="group relative cursor-pointer" onClick={() => setEditIcon(false)}>
{editIcon ? (
<>
Expand Down
2 changes: 1 addition & 1 deletion apps/web/ee/pages/workflows/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function WorkflowsPage() {
<Shell
heading={t("workflows")}
subtitle={t("workflows_to_automate_notifications")}
CTA={session.data?.hasValidLicense ? <NewWorkflowButton /> : <></>}>
CTA={session.data?.hasValidLicense && !isFreeUser ? <NewWorkflowButton /> : <></>}>
<LicenseRequired>
{isLoading ? (
<Loader />
Expand Down
12 changes: 0 additions & 12 deletions packages/emails/templates/workflow-reminder-email.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import dayjs from "@calcom/dayjs";
import localizedFormat from "dayjs/plugin/localizedFormat";
import timezone from "dayjs/plugin/timezone";
import toArray from "dayjs/plugin/toArray";
import utc from "dayjs/plugin/utc";

import BaseEmail from "./_base-email";
import { BookingInfo } from "@calcom/web/ee/lib/workflows/reminders/smsReminderManager";

dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(localizedFormat);
dayjs.extend(toArray);

export default class WorkflowReminderEmail extends BaseEmail {
sendTo: string;
body: string;
Expand Down