File size: 5,407 Bytes
d97b8f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import React from "react";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { format } from "date-fns";
import { Button } from "@/components/ui/button";
import { Textarea } from "@/components/ui/textarea";
import { 
  Dialog, 
  DialogContent, 
  DialogDescription, 
  DialogFooter, 
  DialogHeader, 
  DialogTitle 
} from "@/components/ui/dialog";
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/components/ui/form";
import { Leave, LeaveStatus, LeaveStatusUpdateRequest } from "@/types";
import { toast } from "sonner";

const statusUpdateSchema = z.object({
  comments: z.string().min(3, {
    message: "Comments must be at least 3 characters",
  }).optional(),
});

type StatusUpdateFormValues = z.infer<typeof statusUpdateSchema>;

interface LeaveStatusUpdateProps {
  leave: Leave | null;
  action: "approve" | "reject";
  isOpen: boolean;
  onOpenChange: (open: boolean) => void;
  onSubmit: (data: LeaveStatusUpdateRequest) => Promise<void>;
  approverId: number;
  isSubmitting: boolean;
}

const LeaveStatusUpdate: React.FC<LeaveStatusUpdateProps> = ({

  leave,

  action,

  isOpen,

  onOpenChange,

  onSubmit,

  approverId,

  isSubmitting,

}) => {
  const form = useForm<StatusUpdateFormValues>({
    resolver: zodResolver(statusUpdateSchema),
    defaultValues: {
      comments: "",
    },
  });

  if (!leave) return null;

  const handleSubmit = async (values: StatusUpdateFormValues) => {
    try {
      const updateData: LeaveStatusUpdateRequest = {
        id: leave.id,
        status: action === "approve" ? "Approved" : "Rejected",
        approvedById: approverId,
        actionDate: format(new Date(), "yyyy-MM-dd'T'HH:mm:ss"),
        comments: values.comments,
      };

      await onSubmit(updateData);
      form.reset();
      toast.success(`Leave request ${action === "approve" ? "approved" : "rejected"} successfully`);
      onOpenChange(false);
    } catch (error) {
      console.error(`Error ${action} leave request:`, error);
      toast.error(`Failed to ${action} leave request`);
    }
  };

  return (
    <Dialog open={isOpen} onOpenChange={onOpenChange}>

      <DialogContent className="sm:max-w-[425px]">

        <DialogHeader>

          <DialogTitle>

            {action === "approve" ? "Approve" : "Reject"} Leave Request

          </DialogTitle>

          <DialogDescription>

            {action === "approve"

              ? "Approve the leave request and provide any optional comments."

              : "Reject the leave request and provide a reason for rejection."}

          </DialogDescription>

        </DialogHeader>

        <Form {...form}>

          <form onSubmit={form.handleSubmit(handleSubmit)} className="space-y-4">

            <div className="grid gap-4 py-4">

              <div className="grid grid-cols-4 items-center gap-4">

                <div className="font-medium">Employee:</div>

                <div className="col-span-3">ID: {leave.employeeId}</div>

              </div>

              <div className="grid grid-cols-4 items-center gap-4">

                <div className="font-medium">Date:</div>

                <div className="col-span-3">

                  {format(new Date(leave.startDate), "PPP")} to{" "}

                  {format(new Date(leave.endDate), "PPP")}

                </div>

              </div>

              <div className="grid grid-cols-4 items-center gap-4">

                <div className="font-medium">Type:</div>

                <div className="col-span-3">{leave.type}</div>

              </div>

              <div className="grid grid-cols-4 items-center gap-4">

                <div className="font-medium">Reason:</div>

                <div className="col-span-3">{leave.reason}</div>

              </div>



              <FormField

                control={form.control}

                name="comments"

                render={({ field }) => (

                  <FormItem>

                    <FormLabel>Comments</FormLabel>

                    <FormControl>

                      <Textarea

                        placeholder={

                          action === "approve"

                            ? "Optional comments for approval"

                            : "Please provide a reason for rejection"

                        }

                        className="resize-none"

                        {...field}

                      />

                    </FormControl>

                    <FormMessage />

                  </FormItem>

                )}

              />

            </div>

            <DialogFooter>

              <Button type="button" variant="outline" onClick={() => onOpenChange(false)}>

                Cancel

              </Button>

              <Button type="submit" disabled={isSubmitting} variant={action === "reject" ? "destructive" : "default"}>

                {isSubmitting

                  ? "Processing..."

                  : action === "approve"

                  ? "Approve"

                  : "Reject"}

              </Button>

            </DialogFooter>

          </form>

        </Form>

      </DialogContent>

    </Dialog>
  );
};

export default LeaveStatusUpdate;