summaryrefslogtreecommitdiff
path: root/src/pages/my/transactions/index.js
blob: c31336f2eb98c7af49c1a5472d41f26a63b11eaa (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { useRouter } from "next/router";
import AppBar from "@/components/layouts/AppBar";
import BottomPopup from "@/components/elements/BottomPopup";
import Layout from "@/components/layouts/Layout";
import WithAuth from "@/components/auth/WithAuth";
import { useEffect, useRef, useState } from "react";
import { useAuth } from "@/core/utils/auth";
import apiOdoo from "@/core/utils/apiOdoo";
import currencyFormat from "@/core/utils/currencyFormat";
import { EllipsisVerticalIcon, MagnifyingGlassIcon } from "@heroicons/react/24/outline";
import Link from "@/components/elements/Link";
import Pagination from "@/components/elements/Pagination";
import Alert from "@/components/elements/Alert";

export default function Transactions() {
  const [ auth ] = useAuth();
  const router = useRouter();
  const { 
    q, 
    page = 1 
  } = router.query;

  const [ transactions, setTransactions ] = useState([]);
  const [ activePopupId, setActivePopupId ] = useState(null);

  const [ pageCount, setPageCount ] = useState(0);
  const [ isLoading, setIsLoading ] = useState(true);

  const searchQueryRef = useRef();

  useEffect(() => {
    const loadTransactions = async () => {
      if (auth) {
        const limit = 10;
        let offset = (page - 1) * 10;
        let queryParams = [`limit=${limit}`, `offset=${offset}`];
        if (q) queryParams.push(`name=${q}`);
        queryParams = queryParams.join('&');
        queryParams = queryParams ? '?' + queryParams : '';

        const dataTransactions = await apiOdoo('GET', `/api/v1/partner/${auth.partner_id}/sale_order${queryParams}`);
        setTransactions(dataTransactions);
        setPageCount(Math.ceil(dataTransactions.sale_order_total / limit));
        setIsLoading(false);
      };
    }
    loadTransactions();
  }, [ auth, q, page ]);

  const actionSearch = (e) => {
    e.preventDefault();
    let queryParams = [];
    if (searchQueryRef.current.value) queryParams.push(`q=${searchQueryRef.current.value}`);
    queryParams = queryParams.join('&');
    queryParams = queryParams ? `?${queryParams}` : '';
    router.push(`/my/transactions${queryParams}`);
  };

  return (
    <WithAuth>
      <Layout>
        <AppBar title="Transaksi" />

        <form onSubmit={actionSearch} className="p-4 pb-0 flex gap-x-4">
          <input 
            type="text" 
            className="form-input" 
            placeholder="Cari Transaksi" 
            ref={searchQueryRef}
            defaultValue={q}
          />
          <button type="submit" className="border border-gray_r-7 rounded px-3">
            <MagnifyingGlassIcon className="w-5"/>
          </button>
        </form>

        <div className="p-4 flex flex-col gap-y-5">
          { transactions?.sale_order_total === 0 && !isLoading && (
            <Alert type="info" className="text-center">
              Transaksi tidak ditemukan
            </Alert>
          ) }
          { transactions?.sale_orders?.map((transaction, index) => (
            <div className="p-4 border border-gray_r-7 rounded-md" key={index}>
              <div className="grid grid-cols-2">
                <Link href={`/my/transactions/${transaction.id}`}>
                  <span className="text-caption-2 text-gray_r-11">No. Transaksi</span>
                  <h2 className="text-red_r-11 mt-1">{ transaction.name }</h2>
                </Link>
                <div className="flex gap-x-1 justify-end">
                  <div className="badge-green h-fit">Pending</div>
                  <EllipsisVerticalIcon className="w-5 h-5" onClick={() => setActivePopupId(transaction.id)} />
                </div>
              </div>
              <Link href={`/my/transactions/${transaction.id}`} className="grid grid-cols-2 mt-3">
                <div>
                  <span className="text-caption-2 text-gray_r-11">Dilayani Oleh</span>
                  <p className="mt-1 font-medium text-gray_r-12">{ transaction.sales }</p>
                </div>
                <div className="text-right">
                  <span className="text-caption-2 text-gray_r-11">Total Harga</span>
                  <p className="mt-1 font-medium text-gray_r-12">{ currencyFormat(transaction.amount_total) }</p>
                </div>
              </Link>
            </div>
          )) }
        </div>

        <div className="pb-6">
          <Pagination currentPage={page} pageCount={pageCount} url={`/my/transactions${q ? `?q=${q}` : ''}`} />
        </div>
        
        { transactions?.sale_orders?.length > 0 && (
          <BottomPopup 
            title="Lainnya"
            active={activePopupId} 
            closePopup={() => setActivePopupId(null)}
          >
            <div className="flex flex-col gap-y-4">
              <p>Download Quotation</p>
              <p>Batalkan Transaksi</p>
            </div>
          </BottomPopup>
        ) }
      </Layout>
    </WithAuth>
  );
};