summaryrefslogtreecommitdiff
path: root/src/pages/my/address/index.js
blob: 787cfcfa903924cb87a95866d6ec9bba8ecd18c6 (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
68
69
70
71
72
73
74
75
76
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">
        { auth && addresses && addresses.map((address, index) => {
          let type = address.type.charAt(0).toUpperCase() + address.type.slice(1) + ' Address';
          return (
            <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)}
            >
              <div className="flex gap-x-2">
                <div className="badge-red">{ type }</div>
                { auth?.partner_id == address.id && (
                  <div className="badge-green">Utama</div>
                ) }
              </div>
              <p className="font-medium mt-1">{ address.name }</p>
              <p className="mt-2 text-gray_r-11">{ address.mobile }</p>
              <p className="mt-1 text-gray_r-11 leading-6">{ address.street } { address.street2 }</p>
              <Link href={`/my/address/${address.id}/edit`} className="btn-light mt-3 w-full text-gray_r-11">Ubah Alamat</Link>
            </div>
          );
        }) }
        </div>
      </Layout>
    </WithAuth>
  )
}