|
1 | 1 | "use client"; |
2 | 2 |
|
3 | | -import { useAction, usePaginatedQuery, useQuery } from "convex/react"; |
4 | | -import { AnimatePresence, motion } from "framer-motion"; |
5 | | -import { RefreshCcwIcon } from "lucide-react"; |
6 | | -import moment from "moment"; |
7 | | -import { useState } from "react"; |
8 | | -import InfiniteScroll from "react-infinite-scroller"; |
9 | | -import { toast } from "sonner"; |
10 | | -import { useLocalStorage } from "usehooks-ts"; |
| 3 | +import { useAuth } from "@clerk/nextjs"; |
11 | 4 |
|
12 | | -import { Loader } from "@/components/Loader"; |
13 | | -import { Button } from "@/components/ui/button"; |
14 | | -import { |
15 | | - Select, |
16 | | - SelectContent, |
17 | | - SelectGroup, |
18 | | - SelectItem, |
19 | | - SelectTrigger, |
20 | | - SelectValue, |
21 | | -} from "@/components/ui/select"; |
22 | | -import { api } from "@/convex/_generated/api"; |
23 | | -import type { Id } from "@/convex/_generated/dataModel"; |
24 | | -import { cn } from "@/lib/utils"; |
25 | | -import type { VouchersFilters } from "@/types/vouchers-types"; |
| 5 | +import { VouchersList } from "./VouchersList"; |
26 | 6 |
|
27 | | -import { VoucherCardItem } from "./VoucherCard"; |
28 | | -import { PageContainer } from "@/components/PageContainer"; |
29 | | - |
30 | | -/** |
31 | | - * Represents a page component for managing vouchers. |
32 | | - * |
33 | | - * @returns The rendered JSX element. |
34 | | - */ |
35 | 7 | export default function Page() { |
36 | | - const [isUpdating, setIsUpdating] = useState<boolean>(false); |
37 | | - const [filter, setFilter] = useLocalStorage<VouchersFilters>( |
38 | | - "vouchers-list-filter", |
39 | | - "all", |
40 | | - ); |
41 | | - |
42 | | - const { |
43 | | - results: vouchers, |
44 | | - status, |
45 | | - loadMore, |
46 | | - } = usePaginatedQuery( |
47 | | - api.cibus.cibusQueries.allVouchers, |
48 | | - { filter: filter }, |
49 | | - { initialNumItems: 20 }, |
50 | | - ); |
51 | | - |
52 | | - const summary = useQuery(api.cibus.cibusQueries.allVouchersAggragated); |
53 | | - |
54 | | - const [collapsed, setCollapsed] = useState<Id<"cibusVouchers"> | null>(null); |
55 | | - const updateCibusVouchers = useAction( |
56 | | - api.cibus.cibusActions.updateCibusVouchers, |
57 | | - ); |
58 | | - |
59 | | - // Get the last date of the vouchers or 1 month ago if no vouchers exist |
60 | | - const lastDate = |
61 | | - vouchers?.[0]?.date || moment().subtract(1, "month").toDate(); |
62 | | - const lastDateFormatted = moment(lastDate).format("YYYY-MM-DD"); |
63 | | - |
64 | | - const refresh = async () => { |
65 | | - try { |
66 | | - setIsUpdating(true); |
67 | | - |
68 | | - await updateCibusVouchers({ |
69 | | - fromDate: lastDateFormatted, |
70 | | - }); |
71 | | - toast.success("השוברים עודכנו בהצלחה"); |
72 | | - } catch (error) { |
73 | | - console.error(error); |
74 | | - toast.error("אירעה שגיאה בעדכון השוברים", { |
75 | | - action: { |
76 | | - label: "נסה שוב", |
77 | | - onClick: refresh, |
78 | | - }, |
79 | | - description: "אם הבעיה חוזרת נא לנסות להתחבר מחדש", |
80 | | - }); |
81 | | - } finally { |
82 | | - setIsUpdating(false); |
83 | | - } |
84 | | - }; |
85 | | - |
86 | | - const handleChangeFilter = (filter: VouchersFilters) => { |
87 | | - setFilter(filter); |
88 | | - }; |
| 8 | + const { userId, isLoaded } = useAuth(); |
89 | 9 |
|
90 | | - if (status === "LoadingFirstPage") { |
| 10 | + if (!isLoaded) { |
91 | 11 | return ( |
92 | | - <div className="w-full h-full absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 backdrop-blur-[2px] rounded-xl p-4"> |
93 | | - <Loader /> |
| 12 | + <div> |
| 13 | + <p className="text-center text-3xl text-pretty">טוען...</p> |
94 | 14 | </div> |
95 | 15 | ); |
96 | 16 | } |
97 | 17 |
|
98 | | - return ( |
99 | | - <PageContainer className="pb-16 px-4"> |
100 | | - <header className="mt-2 flex w-full items-center justify-between gap-3"> |
101 | | - <Button |
102 | | - disabled={isUpdating || !vouchers} |
103 | | - type="button" |
104 | | - className="flex w-full gap-5 rounded-3xl bg-gradient-to-b from-pink-800 to-pink-600" |
105 | | - onClick={() => refresh()} |
106 | | - > |
107 | | - {isUpdating ? "סורק..." : "משוך שוברים"} |
108 | | - <RefreshCcwIcon |
109 | | - className={cn("size-5", isUpdating && "animate-spin")} |
110 | | - /> |
111 | | - </Button> |
112 | | - |
113 | | - <Select value={filter} onValueChange={handleChangeFilter}> |
114 | | - <SelectTrigger |
115 | | - className="border-pink-700 rounded-3xl text-pink-700 focus:ring-0" |
116 | | - dir="rtl" |
117 | | - > |
118 | | - <SelectValue /> |
119 | | - </SelectTrigger> |
120 | | - <SelectContent dir="rtl"> |
121 | | - <SelectGroup> |
122 | | - <SelectItem value="all">כל השוברים</SelectItem> |
123 | | - <SelectItem value="unused">שוברים שלא נוצלו</SelectItem> |
124 | | - <SelectItem value="used">שוברים שנוצלו</SelectItem> |
125 | | - <SelectItem value="bugged">שוברים תקולים</SelectItem> |
126 | | - </SelectGroup> |
127 | | - </SelectContent> |
128 | | - </Select> |
129 | | - </header> |
130 | | - |
131 | | - {vouchers?.length === 0 && ( |
132 | | - <p className="flex h-full flex-col justify-center text-3xl"> |
133 | | - אין שוברים להצגה |
| 18 | + if (!userId) { |
| 19 | + return ( |
| 20 | + <div> |
| 21 | + <p className="text-center text-3xl text-pretty"> |
| 22 | + אנא התחבר כדי לראות את השוברים שלך. |
134 | 23 | </p> |
135 | | - )} |
136 | | - |
137 | | - {/* Vouchers cards */} |
138 | | - <InfiniteScroll |
139 | | - pageStart={0} |
140 | | - loadMore={loadMore} |
141 | | - hasMore={status === "CanLoadMore"} |
142 | | - className="flex flex-1 flex-col gap-4 mt-4" |
143 | | - loader={ |
144 | | - <div className="w-full flex justify-center" key={0}> |
145 | | - <Loader /> |
146 | | - </div> |
147 | | - } |
148 | | - > |
149 | | - <AnimatePresence initial={false}> |
150 | | - {vouchers.map((voucher) => ( |
151 | | - <motion.div |
152 | | - key={voucher._id} |
153 | | - initial={{ height: 0, scale: 0 }} |
154 | | - animate={{ height: "auto", scale: 1 }} |
155 | | - exit={{ height: 0, scale: 0 }} |
156 | | - style={{ overflow: "hidden" }} |
157 | | - > |
158 | | - <VoucherCardItem |
159 | | - key={voucher._id} |
160 | | - voucher={voucher} |
161 | | - isCollapsed={collapsed === voucher._id} |
162 | | - setCollapsed={setCollapsed} |
163 | | - /> |
164 | | - </motion.div> |
165 | | - ))} |
166 | | - </AnimatePresence> |
167 | | - </InfiniteScroll> |
| 24 | + </div> |
| 25 | + ); |
| 26 | + } |
168 | 27 |
|
169 | | - {/* Total unused amount */} |
170 | | - <footer |
171 | | - className="brightness-110 opacity-80 rounded-xl fixed bottom-[70px] flex h-14 w-11/12 items-center justify-center bg-pink-500" |
172 | | - style={{ |
173 | | - backgroundImage: |
174 | | - "url(data:image/gif;base64,R0lGODdhWAICAMIFAM0Mg9ojd+g3bPNKYP9cWv///////////ywAAAAAWAICAAADVwi63P4wykmrvSHrzbv/YCiOZGkKaKqubOu+cCzPdD3ceK7vfO//wKBwSCQYj8ikcslsOp/QqHR6qVqv2CzGxO16v+BSbUwum882onrNbruH07h8Tq9PEwA7)", |
175 | | - }} |
176 | | - > |
177 | | - <p className="text-xl text-white"> |
178 | | - יש לך {Math.floor(summary?.totalUnusedAmount || 0)} ₪ ב-{" "} |
179 | | - {summary?.totalUnusedCount} שוברים שלא נוצלו |
180 | | - </p> |
181 | | - </footer> |
182 | | - </PageContainer> |
183 | | - ); |
| 28 | + return <VouchersList />; |
184 | 29 | } |
0 commit comments