From f99e0aba70efad0deb907d8e27f09fc9f527c8a4 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 17 Feb 2023 17:07:50 +0700 Subject: Refactor --- src2/pages/my/address/index.js | 84 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src2/pages/my/address/index.js (limited to 'src2/pages/my/address/index.js') diff --git a/src2/pages/my/address/index.js b/src2/pages/my/address/index.js new file mode 100644 index 00000000..5cad4410 --- /dev/null +++ b/src2/pages/my/address/index.js @@ -0,0 +1,84 @@ +import { useEffect, useState } from "react"; +import { useRouter } from "next/router"; + +import AppBar from "@/components/layouts/AppBar"; +import Layout from "@/components/layouts/Layout"; +import Link from "@/components/elements/Link"; +import WithAuth from "@/components/auth/WithAuth"; + +import apiOdoo from "@/core/utils/apiOdoo"; +import { useAuth } from "@/core/utils/auth"; +import { createOrUpdateItemAddress, getItemAddress } from "@/core/utils/address"; +import { toast } from "react-hot-toast"; + +export default function Address() { + const router = useRouter(); + const { select } = router.query; + const [ auth ] = useAuth(); + const [ addresses, setAddresses ] = useState(null); + const [ selectedAdress, setSelectedAdress ] = useState(null); + + useEffect(() => { + const getAddress = async () => { + if (auth) { + const dataAddress = await apiOdoo('GET', `/api/v1/user/${auth.id}/address`); + setAddresses(dataAddress); + } + }; + getAddress(); + }, [auth]); + + useEffect(() => { + if (select) { + setSelectedAdress(getItemAddress(select)); + } + }, [select]); + + const changeSelectedAddress = (id) => { + if (select) { + createOrUpdateItemAddress(select, id); + router.back(); + } + }; + + return ( + + + + +
+ Tambah Alamat +
+ +
+ { auth && addresses && addresses.map((address, index) => { + let type = address.type.charAt(0).toUpperCase() + address.type.slice(1) + ' Address'; + return ( +
+
changeSelectedAddress(address.id)}> +
+
{ type }
+ { auth?.partner_id == address.id && ( +
Utama
+ ) } +
+

{ address.name }

+ { address.mobile && ( +

{ address.mobile }

+ ) } +

+ { address.street } +

+
+ Ubah Alamat +
+ ); + }) } +
+
+
+ ) +} \ No newline at end of file -- cgit v1.2.3