Skip to content

Commit

Permalink
phase editing
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Huang committed Oct 9, 2024
1 parent 35f4629 commit 70a4b47
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { GeneralInformationSchema } from "@lib/schemas/opportunity";
import { Save } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@components/ui/button";
import { useRouter } from "next/navigation";
import { type Session } from "next-auth";
import Breadcrumbs from "@components/ui/breadcrumbs";
import { Form } from "@components/ui/form";
Expand All @@ -16,13 +15,12 @@ import { GeneralInformation } from "../../../_components/general/general";
import { DeleteButton } from "../deleteButton";

interface Props {
session: Session;
defaultValues: z.infer<typeof GeneralInformationSchema>;
update: (
input: z.infer<typeof GeneralInformationSchema>,
) => Promise<Opportunity>;
}
const EditGeneralForm = ({ session, defaultValues, update }: Props) => {
const EditGeneralForm = ({ defaultValues, update }: Props) => {
const form = useForm<z.infer<typeof GeneralInformationSchema>>({
resolver: zodResolver(GeneralInformationSchema),
defaultValues,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,5 @@ export async function EditGeneral({
});
}

return (
<EditGeneralForm
session={session}
defaultValues={defaultValues}
update={update}
/>
);
return <EditGeneralForm defaultValues={defaultValues} update={update} />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import Phase from "./phases/phase";
import { type z } from "zod";
import { AddPhasePopover } from "./phases/addPhasePopover";
import { PhasePopover } from "./phases/phasePopover";
import { type PhasesSchema } from "@lib/schemas/opportunity";
import { Button } from "@components/ui/button";
import { Save } from "lucide-react";
import { Plus, Save } from "lucide-react";
import { toast } from "sonner";
import { TallyForm } from "./tally/tallyForm";
import { createPhasesStore, PhasesContext } from "./usePhasesStore";
Expand Down Expand Up @@ -72,7 +72,7 @@ export function EditPhasesForm({ defaultValues, update }: Props) {
/>
))}

<AddPhasePopover
<PhasePopover
onSave={(data) => appendPhase(data)}
className="flex w-max opacity-0 transition-opacity group-hover/phases:opacity-100"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export async function EditPhases({ opportunityId }: { opportunityId: string }) {
const defaultPhases = opportunity.phases.map((phase) => ({
id: phase.id,
name: phase.name,
isInterview: phase.isInterview,
questionnaires: phase.questionnaires.map((questionnaire) => ({
id: questionnaire.id,
name: questionnaire.name,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Button } from "@components/ui/button";
import { Separator } from "@components/ui/separator";
import { Ellipsis, FolderPen, Move, Plus, Trash } from "lucide-react";
import {
Ellipsis,
FolderPen,
Handshake,
Move,
Plus,
Trash,
} from "lucide-react";
import { QuestionnaireDialog } from "../questionnaire/questionnaireDialog";
import { Card } from "@components/ui/card";
import {
Expand All @@ -17,6 +24,7 @@ import {
} from "@components/ui/tooltip";
import { usePhasesContext } from "../usePhasesStore";
import { cn } from "@lib/utils";
import { PhasePopover } from "./phasePopover";

interface Props {
index: number;
Expand All @@ -28,6 +36,7 @@ export default function Phase({ index: phaseIndex, className }: Props) {
if (!phase) throw new Error(`Phase does not exist at index ${phaseIndex}`);

const onRemove = usePhasesContext((s) => s.removePhase);
const onSave = usePhasesContext((s) => s.updatePhase)(phaseIndex);
const appendQuestionnaire = usePhasesContext((s) => s.appendQuestionnaire)(
phaseIndex,
);
Expand All @@ -41,10 +50,11 @@ export default function Phase({ index: phaseIndex, className }: Props) {
return (
<Card className={cn("group w-80", className)}>
<div className="flex flex-row items-center justify-between p-2">
<h4 className="scroll-m-20 text-xl font-semibold tracking-tight">
<h4 className="flex scroll-m-20 items-center text-xl font-semibold tracking-tight">
{phase.isInterview && <Handshake className="mr-2 h-6 w-6" />}
{phase.name}
</h4>
<div className="flex flex-row gap-1">
<div className="flex flex-row gap-2">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
Expand All @@ -57,10 +67,6 @@ export default function Phase({ index: phaseIndex, className }: Props) {
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem>
<FolderPen className="mr-2 h-4 w-4" />
<span>Rename</span>
</DropdownMenuItem>
<DropdownMenuItem>
<Move className="mr-2 h-4 w-4" />
<span>Move</span>
Expand All @@ -72,6 +78,17 @@ export default function Phase({ index: phaseIndex, className }: Props) {
</DropdownMenuContent>
</DropdownMenu>

<PhasePopover onSave={onSave} defaultValues={phase}>
<Button
type="button"
size="icon-sm"
variant="ghost"
className="text-muted-foreground"
>
<FolderPen />
</Button>
</PhasePopover>

<QuestionnaireDialog onSave={appendQuestionnaire}>
<Button
type="button"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { Button } from "@components/ui/button";
import { Input } from "@components/ui/input";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@components/ui/popover";
import {
type SubmitHandler,
useForm,
type UseFormProps,
} from "react-hook-form";
import { PhaseSchema } from "@lib/schemas/opportunity";
import { type z } from "zod";
import { useState } from "react";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@components/ui/form";
import { zodResolver } from "@hookform/resolvers/zod";
import { Plus, Save } from "lucide-react";
import { type Phase as PhaseType } from "@lib/types/opportunity";
import { v4 as uuidv4 } from "uuid";
import { cn } from "@lib/utils";
import { Checkbox } from "@components/ui/checkbox";

interface Props {
onSave: (phase: PhaseType) => void;
children?: React.ReactNode;
defaultValues?: UseFormProps<z.infer<typeof PhaseSchema>>["defaultValues"];
className?: string;
}

export const PhasePopover = ({
onSave,
children,
defaultValues: initialDefaultValues,
className,
}: Props) => {
const [popoverOpen, setPopoverOpen] = useState(false);

const defaultValues = {
id: uuidv4(),
name: "",
questionnaires: [],
isInterview: false,
};

const form = useForm<z.infer<typeof PhaseSchema>>({
resolver: zodResolver(PhaseSchema),
defaultValues: initialDefaultValues ?? defaultValues,
});

const onSubmit: SubmitHandler<z.infer<typeof PhaseSchema>> = (data) => {
onSave(data);

if (!initialDefaultValues) {
form.reset(defaultValues);
}

setPopoverOpen(false);
};

return (
<Popover open={popoverOpen} onOpenChange={setPopoverOpen}>
<PopoverTrigger asChild>
{!!children ? (
children
) : (
<Button variant="outline" size="sm" className={cn(className)}>
<Plus className="mr-2" />
{initialDefaultValues ? "Edit" : "Add"} Phase
</Button>
)}
</PopoverTrigger>
<PopoverContent>
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSave, console.error)}
className="space-y-4"
>
<h4 className="scroll-m-20 text-xl font-semibold tracking-tight">
{initialDefaultValues ? "Edit" : "Add"} Phase
</h4>

<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Name*</FormLabel>
<FormControl>
<Input placeholder="Screening" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>

<FormField
control={form.control}
name="isInterview"
render={({ field }) => (
<FormItem className="flex flex-row items-start space-x-3 space-y-0">
<FormControl>
<Checkbox
checked={field.value}
onCheckedChange={field.onChange}
/>
</FormControl>
<div className="space-y-1 leading-none">
<FormLabel>Is Interview</FormLabel>
<FormDescription>
Applications for interviews can be manually picked
</FormDescription>
</div>
</FormItem>
)}
/>

<Button
variant="secondary"
className="w-full"
// eslint-disable-next-line @typescript-eslint/no-misused-promises
onClick={form.handleSubmit(onSubmit, (err) => console.error(err))}
>
<Save className="mr-2" />
Save
</Button>
</form>
</Form>
</PopoverContent>
</Popover>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { Card } from "@components/ui/card";
import { QuestionForm, type QuestionFormProps } from "./question";
import type { Question } from "@lib/types/question";
import { UsersStack } from "../../usersStack";
import { useSession } from "next-auth/react";

const QuestionEdit = ({
question,
Expand Down
Loading

0 comments on commit 70a4b47

Please sign in to comment.