import { AuthContext } from "@/authContext"; import { LoadingButton } from "@/components/frontend"; import DatePickerV2 from "@/components/frontend/DatePickerV2"; import { GlobalContext } from "@/globalContext"; import { callCustomAPI } from "@/utils/callCustomAPI"; import { NOTIFICATION_STATUS, NOTIFICATION_TYPE } from "@/utils/constants"; import MkdSDK from "@/utils/MkdSDK"; import { yupResolver } from "@hookform/resolvers/yup"; import moment from "moment/moment"; import React, { useContext, useEffect, useState, useRef } from "react"; import { FileUploader } from "react-drag-drop-files"; import { useForm } from "react-hook-form"; import { Navigate, useNavigate } from "react-router"; import { Link } from "react-router-dom"; import countries from "@/utils/countries.json"; import * as yup from "yup"; import CustomLocationAutoCompleteV2 from "@/components/CustomLocationAutoCompleteV2"; import CustomComboBox from "@/components/CustomComboBox"; const readImage = (file, previewEl) => { const reader = new FileReader(); reader.onload = (event) => { document.getElementById(previewEl).src = event.target.result; }; reader.readAsDataURL(file); }; async function getFileFromUrl(url) { if (!url) return null; try { let response = await fetch(url); let data = await response.blob(); let metadata = { type: "image/jpeg", }; return new File([data], url.split("/").pop(), metadata); } catch (err) { return null; } } export default function BecomeAHostPage() { const initialDate = useRef(new Date()); const { state: globalState, dispatch: globalDispatch } = useContext(GlobalContext); const { dispatch: authDispatch } = useContext(AuthContext); const [frontImage, setFrontImage] = useState(null); const [backImage, setBackImage] = useState(null); const [passport, setPassport] = useState(null); const [loading, setLoading] = useState(false); const [imageErr, setImageErr] = useState(""); const navigate = useNavigate(); const schema = yup.object({ // dob: yup // .string() // .required("This field is required") // .test("is-not-in-future", "Not a valid date", (val) => { // if (val == "") return true; // const date = new Date(val); // return date.setDate(date.getDate() + 1) < new Date(); // }), expiry_date: yup .string() .required("This field is required") .test("is-not-in-past", "Invalid expiry date", (val) => { const date = new Date(val); return date.setDate(date.getDate() - 1) > new Date(); }), city: yup.string().required("This field is required"), country: yup.string().required("This field is required"), selectedType: yup.string().required("This field is required"), about: yup.string().required("This field is required"), }); const { handleSubmit, register, setValue, control, watch, formState: { errors }, } = useForm({ defaultValues: { dob: globalState.user.dob ? moment(globalState.user.dob).format("yyyy-MM-DD") : "", expiry_date: globalState.user.verificationExpiry ? moment(globalState.user.verificationExpiry).format("yyyy-MM-DD") : "", city: globalState.user.city || "", country: globalState.user.country || "", selectedType: globalState.user.verificationType || "Driver's License", about: globalState.user.about || "", }, resolver: yupResolver(schema), }); const sdk = new MkdSDK(); const selectedType = watch("selectedType"); const handleImageUpload = async (file) => { const formData = new FormData(); formData.append("file", file); try { const upload = await sdk.uploadImage(formData); return upload.url; } catch (err) { console.log("err", err); return ""; } }; async function onSubmit(data) { // check if images are uploaded if (selectedType == "Driver's License" && (!frontImage || !backImage)) { setImageErr("Please upload required documents"); return; } if (selectedType == "Passport" && !passport) { setImageErr("Please upload required documents"); return; } console.log("submitting", data); setLoading(true); try { // edit user await callCustomAPI( "edit-self", "post", { user: { role: ["superadmin", "admin"].includes(globalState.user.role) ? undefined : "host" }, profile: { city: data.city, country: data.country, // dob: isSameDay(data.dob, initialDate.current) ? undefined : moment(data.dob).format("yyyy-MM-DD"), about: data.about, getting_started: 0, }, }, "", ); // submit id verification if (selectedType == "Driver's License") { data.image_front = await handleImageUpload(frontImage); data.image_back = await handleImageUpload(backImage); } else { data.image_front = await handleImageUpload(passport); } sdk.setTable("id_verification"); const result = await sdk.callRestAPI( { id: globalState.user.verificationId, type: selectedType, expiry_date: data.expiry_date, status: 0, image_front: data.image_front, image_back: data.image_back, user_id: Number(localStorage.getItem("user")), }, globalState.user.verificationId ? "PUT" : "POST", ); // create notification sdk.setTable("notification"); await sdk.callRestAPI( { user_id: Number(localStorage.getItem("user")), actor_id: null, action_id: result.message, notification_time: new Date().toISOString().split(".")[0], message: "New ID Verification submitted", type: NOTIFICATION_TYPE.NEW_ID_VERIFICATION, status: NOTIFICATION_STATUS.NOT_ADDRESSED, }, "POST", ); globalDispatch({ type: "SHOW_CONFIRMATION", payload: { heading: "Success", message: `Host account created, please re login to your account`, btn: "Ok got it", onClose: () => { sdk.logout(); authDispatch({ type: "LOGOUT" }); navigate("/login"); }, }, }); } catch (err) { globalDispatch({ type: "SHOW_ERROR", payload: { heading: "Operation failed", message: err.message, }, }); } setLoading(false); } useEffect(() => { (async () => { const front = await getFileFromUrl(globalState.user.verificationImageFront); const back = await getFileFromUrl(globalState.user.verificationImageBack); if (globalState.user.verificationType == "Passport") { setPassport(front); } else { setFrontImage(front); setBackImage(back); } })(); }, []); if (!globalState.user.id) return ; return (

Become A Host

Gain the ability to rent your spaces by giving us some additional information

Location

setValue("city", val)} name="city" className={`w-full rounded border py-2 px-3 leading-tight text-gray-700 ${errors.city?.message ? "border-red-500 focus:outline-red-500" : "focus-within:outline-primary"}`} placeholder="" hideIcons suggestionType={["(cities)"]} /> {/*
*/}

{errors.city?.message}

setValue("country", val)} items={countries} containerClassName="relative w-full" className={`w-full truncate border py-2 px-3 text-black ${errors.country?.message ? "border-red-500 focus:outline-red-500" : "focus-within:outline-primary"}`} placeholder="" /> {/*
*/}

{errors.country?.message}

Profile Information

setValue("dob", v)} />

{errors.about?.message}

Identity Verification

Explain what document(s) are allowed.

{imageErr}

{selectedType == "Driver's License" ? (
{ setFrontImage(file); }} types={["SVG", "JPEG", "PNG", "GIF", "JPG"]} >
{frontImage?.name ? ( ) : ( <>

Front

Click to upload or drag and drop SVG, PNG, JPG or GIF (max. 800x400px)

)}
{ setBackImage(file); }} types={["SVG", "JPEG", "PNG", "GIF", "JPG"]} >
{backImage?.name ? ( ) : ( <>

Back

Click to upload or drag and drop SVG, PNG, JPG or GIF (max. 800x400px)

)}
) : ( { setPassport(file); }} types={["SVG", "JPEG", "PNG", "GIF", "JPG"]} >
{passport?.name ? ( ) : ( <>

Passport page with photo

Click to upload or drag and drop SVG, PNG, JPG or GIF (max. 800x400px)

)}
)}
setValue("expiry_date", v)} />
Cancel Continue
); }