summaryrefslogtreecommitdiff
path: root/components/form-data/temp-download-btn.tsx
blob: a5f963e4146e3323cdd9c90f58ddf02de865cef9 (plain)
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
"use client";

import React from "react";
import Image from "next/image";
import { useToast } from "@/hooks/use-toast";
import { toast as toastMessage } from "sonner";
import { saveAs } from "file-saver";
import { Button } from "@/components/ui/button";
import { getReportTempFileData } from "@/lib/forms/services";

export const TempDownloadBtn = () => {
  const { toast } = useToast();

  const downloadTempFile = async () => {
    try {
      const { fileName, fileType, base64 } = await getReportTempFileData();

      saveAs(`data:${fileType};base64,${base64}`, fileName);

      toastMessage.success("Report Sample File 다운로드 완료!");
    } catch (err) {
      console.log(err);
      toast({
        title: "Error",
        description: "Sample File을 찾을 수가 없습니다.",
        variant: "destructive",
      });
    }
  };
  return (
    <Button
      variant="ghost"
      className="relative p-2"
      aria-label="Template Sample Download"
      onClick={downloadTempFile}
    >
      <Image
        src="/icons/temp_sample_icon.svg"
        alt="Template Sample Download Icon"
        width={20}
        height={20}
      />
    </Button>
  );
};