import React, { useEffect, useState } from "react"; import "./Manage_Address.css"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faPencil, faPencilSquare, faPenToSquare, faTrash, } from "@fortawesome/free-solid-svg-icons"; import { Form } from "react-bootstrap"; import PhoneInput from "react-phone-input-2"; import "react-phone-input-2/lib/style.css"; import { useForm, Controller } from "react-hook-form"; import Green_round_btn from "../../../common-components/common_btn/green_round_btn/Green_round_btn"; import Green_btn from "../../../common-components/common_btn/green_btn/Green_btn"; import Select from "react-select"; import { useDispatch, useSelector } from "react-redux"; import { AllCity, AllPincode, AllState, getAllCountries, } from "../../../../features/master/masterSlice"; import { createOrderAddress, getAllOrderAddress, } from "../../../../features/order/orderSlice"; import { createUserAddress, deleteUserAddress, getUserAddresses } from "../../../../features/user/userSlice"; const Manage_Address = () => { // const options = [ // { value: "chocolate", label: "Chocolate" }, // { value: "strawberry", label: "Strawberry" }, // { value: "vanilla", label: "Vanilla" }, // ]; const { control, handleSubmit, register, getValues, reset, setValue, formState: { errors }, } = useForm({ defaultValues: { // name: details.name || "", // pincode: details.pincode || "", // addressLine1: details.addressLine1 || "", // addressLine2: details.addressLine2 || "", // landmark: details.landmark || "", // city: details.city || "", // country: details.country || null, // state: details.state || null, // defaultAddress: details.defaultAddress || false, // instructions: details.instructions || "", // contact: details.contact || "", }, }); const dispatch = useDispatch(); useEffect(() => { dispatch(getAllOrderAddress()); }, [dispatch]); const countries = useSelector((state) => state.master.all_country); const states = useSelector((state) => state.master.all_state); const cities = useSelector((state) => state.master.all_city); const pincodes = useSelector((state) => state.master.all_pincode); useEffect(() => { dispatch(getAllCountries()); }, [dispatch]); const handleCountryChange = (selectedOption) => { setValue("country_id", selectedOption?.value); setValue("state_id", null); setValue("city_id", null); setValue("pincode_id", null); dispatch(AllState({ countryId: selectedOption.value })); }; const handleStateChange = (selectedOption) => { setValue("state_id", selectedOption.value); setValue("city_id", null); setValue("pincode_id", null); dispatch(AllCity({ stateId: selectedOption.value })); }; const handleCityChange = (selectedOption) => { setValue("city_id", selectedOption?.value); setValue("pincode_id", null); dispatch(AllPincode({ cityId: selectedOption.value })); }; const handlePincodeChange = (selectedOption) => { setValue("pincode_id", selectedOption?.value); }; const onSubmit = async (d) => { const res = await dispatch(createUserAddress(d)); if (res.meta?.requestStatus === "fulfilled") { } else if (res.meta?.requestStatus === "rejected") { } reset() }; const [isEdit, setIsEdit] = useState(false); const [isFormVisible, setIsFormVisible] = useState(false); const handleAdd = () => { setIsEdit(true); }; const handleEdit = (data) => { setIsFormVisible(true); setValue("name", data?.name); setValue("country_id", data?.country_id); setValue("state_id", data?.state_id); setValue("city_id", data?.city_id); setValue("pincode_id", data?.pincode_id); setValue("street", data?.street); setValue("contact_no", data?.contact_no); setValue("landmark", data?.landmark); }; const handleCancel = () => { setIsEdit(false); }; const handleDelete = (id) => { // dispatch(deleteUserAddress(id)); } const allAddress = useSelector((state) => state.order.allOrdersAddress); useEffect(() => { dispatch(getAllOrderAddress()); }, [dispatch]); const [selectedAddressId, setSelectedAddressId] = useState(); // useEffect(() => { // if (selectedAddressId) { // setSelectedAddressId(selectedAddressId); // } else // if (allAddress?.data?.length && !selectedAddressId) { // setSelectedAddressId(allAddress.data[0].id); // } // setIsFormVisible(false) // }, [allAddress?.data]); return ( <> {!isEdit && (