Skip to content

Instantly share code, notes, and snippets.

@MonteLogic
Last active September 22, 2024 14:17
Show Gist options
  • Select an option

  • Save MonteLogic/c07c5ad350f97152b5c9c2f7bb9c37f5 to your computer and use it in GitHub Desktop.

Select an option

Save MonteLogic/c07c5ad350f97152b5c9c2f7bb9c37f5 to your computer and use it in GitHub Desktop.
import React, { useState, useRef, useEffect } from 'react';
import { ChevronDownIcon } from '@heroicons/react/solid';
import ExampleData3 from '#/dummy-data/input-object-hcp-3.json';
import ExampleData4 from '#/dummy-data/input-object-hcp-4.json';
import { FormData } from './types';
interface ChevronDropdownMenuProps {
onFillData: (data: FormData) => void;
currentFormData: FormData;
}
const ChevronDropdownMenu: React.FC<ChevronDropdownMenuProps> = ({
onFillData,
currentFormData,
}) => {
const [isOpen, setIsOpen] = useState(false);
const dropdownRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (
dropdownRef.current &&
!dropdownRef.current.contains(event.target as Node)
) {
setIsOpen(false);
}
};
document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, []);
const copyToClipboard = () => {
const formDataString = JSON.stringify(currentFormData, null, 2);
navigator.clipboard.writeText(formDataString).then(
() => {
alert('Form data copied to clipboard!');
setIsOpen(false);
},
(err) => {
console.error('Failed to copy text: ', err);
},
);
};
return (
<div className="relative" ref={dropdownRef}>
<div className="absolute right-0 top-0 z-50 mb-4">
<button
onClick={() => setIsOpen(!isOpen)}
className="flex h-8 w-8 items-center justify-center rounded-full bg-blue-500 text-white hover:bg-blue-600 focus:outline-none focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-opacity-75"
>
<ChevronDownIcon
className={`${isOpen ? 'rotate-180 transform' : ''} h-5 w-5`}
/>
</button>
{isOpen && (
<div className="absolute right-0 z-50 mt-2 w-48 origin-top-right rounded-md bg-white shadow-lg ring-1 ring-black ring-opacity-5">
<div className="py-1">
<button
onClick={() => {
onFillData(ExampleData3 as FormData);
setIsOpen(false);
}}
className="block w-full px-4 py-2 text-left text-sm text-gray-700 hover:bg-gray-100"
>
Fill with Example Data set 1
</button>
<button
onClick={() => {
onFillData(ExampleData4 as FormData);
setIsOpen(false);
}}
className="block w-full px-4 py-2 text-left text-sm text-gray-700 hover:bg-gray-100"
>
Fill with Example Data set 2
</button>
<div className="my-1 border-t border-gray-200" />
<button
onClick={copyToClipboard}
className="block w-full px-4 py-2 text-left text-sm text-gray-700 hover:bg-gray-100"
>
Copy Form Data to Clipboard
</button>
</div>
</div>
)}
</div>
</div>
);
};
export default ChevronDropdownMenu;
'use client';
import React, { useState } from 'react';
import Button from '../button';
import { FormSections } from './FormSections';
import {
FormData,
WorkExperience,
ReferenceCheck,
Education,
License,
CertificationType,
MachinesUsed,
} from './types';
import { generateHealthcarePDF } from '#/healthcare';
import ChevronDropdownMenu from './ChevronDropdownMenu';
export const HealthcareProfileForm: React.FC = () => {
const [formData, setFormData] = useState<FormData>({
// Personal Information
name: '',
email: '',
profession: '',
address: '',
phoneHome: '',
phoneMobile: '',
// Education
education: [],
// Licenses/Certifications
licenses: [],
// Specialty Certifications
certificationTypes: [],
// Work Experience (multiple entries)
workExperiences: [],
//
machinesUsed: [],
// Reference Check
referenceChecks: [],
});
const handleInputChange = (
e: React.ChangeEvent<HTMLInputElement>,
section?: string,
index?: number,
) => {
const { name, value } = e.target;
setFormData((prev) => {
if (section && typeof index === 'number') {
return {
...prev,
// @ts-ignore
[section]: prev[section].map((item: any, i: number) =>
i === index ? { ...item, [name]: value } : item,
),
};
} else {
return { ...prev, [name]: value };
}
});
};
const addWorkExperience = () => {
setFormData((prev) => ({
...prev,
workExperiences: [...prev.workExperiences, {} as WorkExperience],
}));
};
const addReferenceCheck = () => {
setFormData((prev) => ({
...prev,
referenceChecks: [...prev.referenceChecks, {} as ReferenceCheck],
}));
};
const addEducation = () => {
setFormData((prev) => ({
...prev,
education: [...prev.education, {} as Education],
}));
};
const addLicense = () => {
setFormData((prev) => ({
...prev,
licenses: [...prev.licenses, {} as License],
}));
};
const addCertification = () => {
setFormData((prev) => ({
...prev,
certificationTypes: [...prev.certificationTypes, {} as CertificationType],
}));
};
const addMachinesUsed = () => {
setFormData((prev) => ({
...prev,
machinesUsed: [...prev.machinesUsed, {} as MachinesUsed],
}));
};
const handleGeneratePDF = () => {
console.log(103, formData);
generateHealthcarePDF(formData);
setHasUnsavedChanges(false);
};
// type issues:
const fillWithExampleData = (exampleData: any) => {
setFormData(exampleData);
setHasUnsavedChanges(true);
};
const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);
return (
<>
<ChevronDropdownMenu
onFillData={(data) => setFormData(data)}
currentFormData={formData}
/>
<div className="space-y-8">
<FormSections
formData={formData}
setFormData={setFormData}
handleInputChange={handleInputChange}
addWorkExperience={addWorkExperience}
addMachinesUsed={addMachinesUsed}
addReferenceCheck={addReferenceCheck}
addEducation={addEducation}
addLicense={addLicense}
addCertification={addCertification}
setHasUnsavedChanges={setHasUnsavedChanges}
/>
<Button
onClick={handleGeneratePDF}
className="w-full justify-center rounded-md bg-green-500 px-4 py-2 text-sm font-bold text-white hover:bg-green-600 focus:outline-none focus-visible:ring-2 focus-visible:ring-white/75"
>
Generate Healthcare Profile PDF
</Button>
</div>
</>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment