Files
hats-app/app/(dashboard)/presence/components/FormComponent.tsx
T
2025-12-19 13:57:55 +07:00

189 lines
8.6 KiB
TypeScript

"use client"
import { FC, useEffect, useState } from 'react';
import Link from 'next/link';
import { Camera as CameraIcon } from 'lucide-react';
import { DatePicker, Form, Input, Radio, Select } from 'antd';
import { Button } from '@/components/ui/button';
import { FormDescription, FormLabel } from '@/components/ui/form';
import { Textarea } from '@/components/ui/textarea';
import Camera from './Camera';
type Props = {
isCheckout?: boolean
};
const FormComponent: FC<Props> = ({ isCheckout }) => {
const OFFICE_PEJATEN_LATTITUDE = -6.27831622506824
const OFFICE_PEJATEN_LONGITUDE = 106.82955901443484
const [showCamera, setShowCamera] = useState(false)
const [photo, setPhoto] = useState('')
const [locationName, setLocationName] = useState('')
const [workingStatus, setWorkingStatus] = useState('WFO')
const options: PositionOptions = {
enableHighAccuracy: false,
timeout: 1000,
maximumAge: 0
};
useEffect(() => {
if (!navigator.geolocation) {
return alert("Geolocation not available in your apps")
}
navigator.geolocation.getCurrentPosition(successCallback, errorCallback, options);
}, [])
const successCallback = (position: GeolocationPosition) => {
console.log(position)
const x = position.coords.latitude;
const y = position.coords.longitude;
displayLocation(x, y);
};
const errorCallback = (error: GeolocationPositionError) => {
let errorMessage = 'Unknown error';
switch (error.code) {
case 1:
errorMessage = 'Permission denied';
break;
case 2:
errorMessage = 'Position unavailable';
break;
case 3:
errorMessage = 'Timeout';
break;
}
alert(errorMessage)
};
const displayLocation = async (latitude: number, longitude: number) => {
// const res = await fetch('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitude + ',' + longitude + '&sensor=true&key=' + '')
// const res = await fetch('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitude + ',' + longitude + '&sensor=false&key=' + '')
const res = await fetch(`https://nominatim.openstreetmap.org/reverse?lat=${latitude}&lon=${longitude}&format=jsonv2&zoom=13`)
// const res = await fetch(`https://nominatim.openstreetmap.org/reverse?lat=${latitude}&lon=${longitude}&format=geojson&zoom=13`)
if (res.ok) {
const data = await res.json()
console.log(data?.display_name)
setLocationName(data?.display_name)
} else {
alert(`${res.status} ${res.statusText}`)
}
};
const handleReceivePhoto = (photo: string) => {
setPhoto(photo)
setShowCamera(false)
}
return (
<>
<Form className='space-y-8'>
{/* <div className='mt-6'>
<FormLabel>N I K</FormLabel>
<Form.Item name='username' >
<Input placeholder="930915" className='input' />
</Form.Item>
<FormDescription>
Nomor induk karyawan yang sudah terdaftar di sistem. Silakan lihat pada <Link href="/niktg-list" className='text-blue-500 font-semibold'>link berikut</Link> untuk melihat daftar nik karyawan.
</FormDescription>
</div>
<div>
<FormLabel>Email</FormLabel>
<Form.Item>
<Select placeholder='Select a verified email to display'>
<Select.Option value="m@example.com">m@example.com</Select.Option>
<Select.Option value="m@google.com">m@google.com</Select.Option>
<Select.Option value="m@support.com">m@support.com</Select.Option>
</Select>
</Form.Item>
<FormDescription>
You can manage verified email addresses in your{" "}
<Link href="/examples/forms">email settings</Link>.
</FormDescription>
</div>
<div>
<FormLabel>Datetime</FormLabel>
<Form.Item>
<DatePicker className='w-full h-10' showTime />
</Form.Item>
<FormDescription>
Tanggal dan waktu absensi
</FormDescription>
</div> */}
<div className='space-y-1'>
<FormLabel>Working status</FormLabel>
<FormDescription>Status kerja pada saat absensi</FormDescription>
<div className='flex items-center space-x-5'>
<div className='flex flex-col items-center space-y-1'>
<button className={`rounded-xl w-20 h-20 ${workingStatus === 'WFO' ? 'bg-zinc-200 dark:bg-zinc-700 ' : 'btn-outline'} `} onClick={() => setWorkingStatus('WFO')}>
<i className="ti ti-building-skyscraper icon text-2xl"></i>
</button>
<p>WFO</p>
</div>
<div className='flex flex-col items-center space-y-1'>
<button className={`rounded-xl w-20 h-20 ${workingStatus === 'WFH' ? 'bg-zinc-200 dark:bg-zinc-700 ' : 'btn-outline'}`} onClick={() => setWorkingStatus('WFH')}>
<i className="ti ti-home icon text-2xl"></i>
</button>
<p>WFH</p>
</div>
<div className='flex flex-col items-center space-y-1'>
<button className={`rounded-xl w-20 h-20 ${workingStatus === 'WFS' ? 'bg-zinc-200 dark:bg-zinc-700 ' : 'btn-outline'}`} onClick={() => setWorkingStatus('WFS')}>
<i className="ti ti-building-community icon text-2xl"></i>
</button>
<p>WFS</p>
</div>
</div>
{/* <Form.Item>
<Radio.Group className='flex flex-col space-y-1'>
<Radio value={1}>
<FormLabel className='font-normal ml-2'>WFO</FormLabel>
</Radio>
<Radio value={2}>
<FormLabel className='font-normal ml-2'>WFH</FormLabel>
</Radio>
<Radio value={3}>
<FormLabel className='font-normal ml-2'>WFS</FormLabel>
</Radio>
</Radio.Group>
</Form.Item> */}
</div>
<div className='flex flex-col space-y-2'>
<FormLabel>Take photo</FormLabel>
{photo ?
<div className='flex flex-col space-y-2'>
<img src={photo} alt='' className='w-48 h-64 object-cover rounded-2xl shadow' style={{ transform: 'scaleX(-1)' }} />
<Button variant='outline' onClick={() => setShowCamera(true)}>
<CameraIcon className='mr-2 w-4 h-4' />Retake photo
</Button>
</div>
:
<button className='btn-outline w-36 h-36 rounded-xl flex items-center justify-center'
onClick={() => setShowCamera(true)}
>
<CameraIcon size={64} />
</button>
}
</div>
<div>
<FormLabel>Note</FormLabel>
<Form.Item>
<Textarea
placeholder="Tell us a little bit about yourself"
className="resize-none"
/>
</Form.Item>
<FormDescription>
Tambahkan catatan kehadiran jika diperlukan
</FormDescription>
</div>
<Button type="submit">Submit</Button>
</Form>
{showCamera && <Camera onClose={() => setShowCamera(false)} onSubmit={handleReceivePhoto} />}
</>
);
}
export default FormComponent;