summaryrefslogtreecommitdiff
path: root/src/pages/my/address/index.js
blob: b97e21e7d5950cc75ff7f0b06f36bad413994312 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { useEffect, useState } from "react";
import AppBar from "../../../components/AppBar";
import Layout from "../../../components/Layout";
import Link from "../../../components/Link";
import WithAuth from "../../../components/WithAuth";
import apiOdoo from "../../../helpers/apiOdoo";
import { useAuth } from "../../../helpers/auth";
import { useRouter } from "next/router";
import { createOrUpdateItemAddress, getItemAddress } from "../../../helpers/address";

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 (
    <WithAuth>
      <Layout>
        <AppBar title="Daftar Alamat" />
        
        <div className="text-right mt-4 px-4">
          <Link href="/my/address/create">Tambah Alamat</Link>
        </div>

        <div className="grid gap-y-4 p-4">
        { addresses && addresses.map((address, index) => (
          <div 
            key={index} 
            className={"p-4 rounded-md border " + (selectedAdress && selectedAdress == address.id ? "border-yellow_r-7 bg-yellow_r-2" : "border-gray_r-7") } 
            onClick={() => changeSelectedAddress(address.id)}
          >
            <p className="font-medium">{ address.name }</p>
            <p className="mt-3 text-gray_r-11">{ address.mobile }</p>
            <p className="mt-1 text-gray_r-11 leading-6">{ address.street } { address.street2 }</p>
            <button className="btn-light mt-3 w-full">Ubah Alamat</button>
          </div>
        )) }
        </div>
      </Layout>
    </WithAuth>
  )
}