import { Download, Downloading } from '@mui/icons-material'; import { ButtonBase } from '@mui/material'; import { Resume } from '@reactive-resume/schema'; import clsx from 'clsx'; import download from 'downloadjs'; import get from 'lodash/get'; import isEmpty from 'lodash/isEmpty'; import { GetServerSideProps, NextPage } from 'next'; import Link from 'next/link'; import { serverSideTranslations } from 'next-i18next/serverSideTranslations'; import { useEffect } from 'react'; import toast from 'react-hot-toast'; import { useMutation, useQuery } from 'react-query'; import Page from '@/components/build/Center/Page'; import { ServerError } from '@/services/axios'; import { printResumeAsPdf, PrintResumeAsPdfParams } from '@/services/printer'; import { fetchResumeByShortId } from '@/services/resume'; import { useAppDispatch } from '@/store/hooks'; import { setResume } from '@/store/resume/resumeSlice'; import styles from '@/styles/pages/Preview.module.scss'; type QueryParams = { shortId: string; }; type Props = { shortId: string; }; export const getServerSideProps: GetServerSideProps = async ({ query, locale = 'en' }) => { const { shortId } = query as QueryParams; return { props: { shortId, ...(await serverSideTranslations(locale, ['common'])) } }; }; const Preview: NextPage = ({ shortId }) => { const dispatch = useAppDispatch(); const { data: resume } = useQuery(`resume/${shortId}`, () => fetchResumeByShortId({ shortId }), { refetchOnMount: false, refetchOnReconnect: false, refetchOnWindowFocus: false, onSuccess: (data) => { dispatch(setResume(data)); }, }); const { mutateAsync, isLoading } = useMutation(printResumeAsPdf); useEffect(() => { if (resume) dispatch(setResume(resume)); }, [resume, dispatch]); if (!resume || isEmpty(resume)) return null; const layout: string[][][] = get(resume, 'metadata.layout', []); const handleDownload = async () => { try { const url = await mutateAsync({ username: resume.user.username, slug: resume.slug }); download(`/api${url}`); } catch { toast.error('Something went wrong, please try again later.'); } }; return (
{layout.map((_, pageIndex) => ( ))}
{isLoading ? ( <>

Please wait

) : ( <>

Download PDF

)}

Made with Reactive Resume

); }; export default Preview;