import React, { useContext } from "react"; import { useState } from "react"; import { Link } from "react-router-dom"; import NotVerifiedIcon from "@/components/frontend/icons/NotVerifiedIcon"; import PencilIcon from "@/components/frontend/icons/PencilIcon"; import { GlobalContext } from "@/globalContext"; import MkdSDK from "@/utils/MkdSDK"; import { callCustomAPI } from "@/utils/callCustomAPI"; import Skeleton from "react-loading-skeleton"; import { formatDate } from "@/utils/date-time-utils"; import { IMAGE_STATUS, NOTIFICATION_STATUS, NOTIFICATION_TYPE, } from "@/utils/constants"; import SwitchBulkMode from "@/components/SwitchBulkMode"; import TwoFaDialog from "@/components/Profile/TwoFaDialog"; import EditProfileModal from "@/components/Profile/EditProfileModal"; import EditLocationModal from "@/components/Profile/EditLocationModal"; import EditPasswordModal from "@/components/Profile/EditPasswordModal"; import EditAboutModal from "@/components/Profile/EditAboutModal"; import { parseJsonSafely } from "@/utils/utils"; import EnableEmailDialog from "@/components/Profile/EnableEmailDialog"; import DeleteAccountModal from "@/components/Profile/DeleteAccountModal"; import ProfileImageConfirmModal from "@/components/Profile/ProfileImageConfirmModal"; function getProfilePhotoMessage(image_status) { switch (image_status) { case IMAGE_STATUS.IN_REVIEW: return "We are currently reviewing your profile picture"; case IMAGE_STATUS.APPROVED: return "This will be displayed on your profile"; case IMAGE_STATUS.NOT_APPROVED: return "The image you uploaded was rejected after reviewing, please change it"; default: return "Please upload a profile picture"; } } export default function CustomerProfilePage() { const { dispatch: globalDispatch, state: globalState } = useContext(GlobalContext); const [loading, setLoading] = useState(false); const [twoFa, setTwoFa] = useState(false); const [twoFaDialog, setTwoFaDialog] = useState(false); const [enableEmailDialog, setEnableEmailDialog] = useState(false); const [updatePassword, setUpdatePassword] = useState(false); const [updateName, setUpdateName] = useState(false); const [updateAbout, setUpdateAbout] = useState(false); const [updateLocation, setUpdateLocation] = useState(false); const [deleteAccountModal, setDeleteAccountModal] = useState(false); const [showImagePreview, setShowImagePreview] = useState(false); const [selectedImage, setSelectedImage] = useState(null); let sdk = new MkdSDK(); const changeProfilePic = (e) => { const file = e.target.files && e.target.files[0]; if (file) { setSelectedImage(file); setShowImagePreview(true); } }; const handleConfirmUpload = async () => { setShowImagePreview(false); if (!selectedImage) return; globalDispatch({ type: "START_LOADING" }); const formData = new FormData(); formData.append("file", selectedImage); try { const upload = await sdk.uploadImage(formData); sdk.setTable("user"); await callCustomAPI( "edit-self", "post", { user: { photo: upload.url, is_photo_approved: IMAGE_STATUS.IN_REVIEW, }, }, "" ); globalDispatch({ type: "SET_USER_DATA", payload: { ...globalState.user, photo: upload.url, is_photo_approved: IMAGE_STATUS.IN_REVIEW, }, }); // create notification sdk.setTable("notification"); await sdk.callRestAPI( { user_id: globalState.user.id, actor_id: null, action_id: globalState.user.id, notification_time: new Date().toISOString().split(".")[0], message: "Profile Picture Edited", type: NOTIFICATION_TYPE.EDIT_USER_PICTURE, status: NOTIFICATION_STATUS.NOT_ADDRESSED, }, "POST" ); } catch (err) { globalDispatch({ type: "SHOW_ERROR", payload: { heading: "Operation failed", message: err.message, }, }); } globalDispatch({ type: "STOP_LOADING" }); setSelectedImage(null); }; const handleCancelUpload = () => { setShowImagePreview(false); setSelectedImage(null); }; const removeProfilePic = async () => { try { sdk.setTable("user"); await callCustomAPI( "edit-self", "post", { user: { photo: null, is_photo_approved: null, }, }, "" ); globalDispatch({ type: "SET_USER_DATA", payload: { ...globalState.user, photo: null, is_photo_approved: null }, }); } catch (err) { globalDispatch({ type: "SHOW_ERROR", payload: { heading: "Operation failed", message: err.message, }, }); } }; async function changeTwoFa() { setLoading(true); try { await callCustomAPI( "edit-self", "post", { user: { two_factor_authentication: twoFa != 1 ? 1 : 0, }, }, "" ); setTwoFaDialog(false); globalDispatch({ type: "SHOW_CONFIRMATION", payload: { heading: "Success", message: `Two factor Authentication ${ twoFa == 1 ? "disabled" : "enabled" }`, btn: "Ok got it", }, }); setTwoFa((prev) => (prev == 1 ? 0 : 1)); } catch (err) { setTwoFaDialog(false); globalDispatch({ type: "SHOW_ERROR", payload: { heading: "Operation failed", message: err.message, }, }); } setLoading(false); } return (
Profile status