-
Notifications
You must be signed in to change notification settings - Fork 10
/
tickets.js
226 lines (207 loc) · 9.85 KB
/
tickets.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import Cookies from "js-cookie";
import {useEffect, useState} from "react";
import {createClient, OAuthStrategy} from "@wix/sdk";
import {orders as checkout, wixEventsV2 as wixEvents} from "@wix/events";
import {redirects} from "@wix/redirects";
import testIds from "@/src/utils/test-ids";
import {CLIENT_ID} from "@/constants/constants";
import Link from "next/link";
import styles from "@/styles/app.module.css";
import Head from "next/head";
import {useAsyncHandler} from "@/src/hooks/async-handler";
import {useClient} from "@/internal/providers/client-provider";
import {useModal} from "@/internal/providers/modal-provider";
// We're creating a Wix client using the createClient function from the Wix SDK.
const myWixClient = createClient({
// We specify the modules we want to use with the client.
// In this case, we're using the wixEvents, checkout, and redirects modules.
modules: {wixEvents, checkout, redirects},
// We're using the OAuthStrategy for authentication.
// This strategy requires a client ID and a set of tokens.
auth: OAuthStrategy({
// The client ID is a unique identifier for the application.
// It's used to authenticate the application with the Wix platform.
clientId: CLIENT_ID,
// The tokens are used to authenticate the user.
// In this case, we're getting the tokens from a cookie named "session".
// If the cookie doesn't exist, we default to null.
tokens: JSON.parse(Cookies.get("session") || null),
}),
});
export default function Tickets() {
// State variables for events list and tickets availability
const [eventsList, setEventsList] = useState([]);
const [ticketsAvailability, setTicketsAvailability] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const [selectedEvent, setSelectedEvent] = useState(null);
const [selectedTicket, setSelectedTicket] = useState(null);
const handleAsync = useAsyncHandler();
const {msid} = useClient();
const {openModal} = useModal();
// This is function fetches a list of events.
async function fetchEvents() {
setIsLoading(true);
try {
await handleAsync(async () => {
// We call the queryEvents method from the wixEvents module of the Wix client.
// This method retrieves a list of events.
// In this case, we're limiting the number of events to 10.
const eventsList = await myWixClient.wixEvents
.queryEvents()
.limit(10)
.find();
// Then, we update the state of the events list in the React component.
setEventsList(eventsList.items);
});
} catch (error) {
console.error("Error fetching events", error);
} finally {
setIsLoading(false);
}
}
// This function fetches the availability of tickets for a specific event.
async function fetchTicketsAvailability(event) {
await handleAsync(async () => {
// We call the queryAvailableTickets method from the checkout module of the Wix client.
// This method retrieves the available tickets for a specific event.
// We filter the tickets by the event ID and limit the number of tickets to 10.
const tickets = await myWixClient.checkout.queryAvailableTickets({
filter: {eventId: event._id},
limit: 10,
});
// Then, we update the state of the tickets availability in the React component.
setTicketsAvailability(tickets.definitions);
// We also update the selected event in the state.
setSelectedEvent(event);
});
}
// This function creates a redirect to the checkout page for a specific ticket.
async function createRedirect(ticket) {
await handleAsync(async () => {
try {// We find the event associated with the ticket from the events list.
// We're interested in the slug of the event, which is a URL-friendly version of the event name.
const eventSlug = eventsList.find(
(event) => event._id === ticket.eventId,
).slug;
// We call the createReservation method from the checkout module of the Wix client.
// This method creates a reservation for the ticket.
// We pass the event ID and an object that specifies the quantity of the ticket.
const reservation = await myWixClient.checkout.createReservation(
ticket.eventId,
{
ticketQuantities: [
{
ticketDefinitionId: ticket._id,
quantity: 1,
},
],
},
);
// We call the createRedirectSession method from the redirects module of the Wix client.
// This method creates a redirect session to the checkout page.
// We pass an object that specifies the event slug and the reservation ID for the eventsCheckout.
// We also specify the postFlowUrl to be the current page URL. This is where the user will be redirected after the checkout flow.
const redirect = await myWixClient.redirects.createRedirectSession({
eventsCheckout: {eventSlug, reservationId: reservation._id},
callbacks: {postFlowUrl: window.location.href},
});
// We update the selected ticket in the state.
setSelectedTicket(ticket);
// Finally, we redirect the user to the URL generated by the redirect session.
window.location = redirect.redirectSession.fullUrl;
} catch (e) {
openModal("premium", {
primaryAction: () => {
window.open(
`https://manage.wix.com/premium-purchase-plan/dynamo?siteGuid=${msid || ""}`,
"_blank"
);
},
});
}
});
}
// Fetch events on component mount
useEffect(() => {
fetchEvents();
}, []);
return (
<>
<Head>
<title>Events Page</title>
</Head>
<main data-testid={testIds.EVENTS_PAGE.CONTAINER}>
<div>
<h2>Choose an Event:</h2>
{isLoading ? (
<p>Loading events...</p>
) : eventsList.length > 0 ? (
eventsList.map((event) => {
return (
<section
key={event._id}
data-testid={testIds.EVENTS_PAGE.EVENT}
className={`${styles.selectable} ${event === selectedEvent ? styles.active : ""}`}
// When the section is clicked, we fetch the availability of tickets for the event.
onClick={() => fetchTicketsAvailability(event)}
>
{/* We display the title of the event. */}
{event.title}
</section>
);
})
) : (
<div>
<p>No events available</p>
<Link
href={`https://manage.wix.com/dashboard/${msid}/events`}
rel="noopener noreferrer"
target="_blank"
style={{textDecoration: "underline", color: "#0070f3"}}
>
Create an event
</Link>
</div>
)}
</div>
<div>
<h2>Choose a Ticket:</h2>
{ticketsAvailability.length === 0 && selectedEvent && (
<div>
<p>No tickets available</p>
<Link
href={`https://manage.wix.com/dashboard/${msid}/events/event/${selectedEvent?._id}/tickets/new`}
target={"_blank"}
style={{color: "#0070f3"}}
>
Create a ticket for this event
</Link>
</div>
)}
{/* We map over the tickets availability list and create a section for each ticket. */}
{ticketsAvailability.map((ticket) => {
return (
<section
key={ticket._id}
data-testid={testIds.EVENTS_PAGE.TICKET_OPTION}
// When the section is clicked, we create a redirect to the checkout page for the ticket.
onClick={() => createRedirect(ticket)}
className={`${styles.selectable} ${ticket === selectedTicket ? styles.active : ""}`}
>
{/* We display the name of the ticket. */}
{ticket.name}
</section>
);
})}
</div>
</main>
</>
);
}
export async function getStaticProps() {
return {
props: {
title: "Events & Tickets",
},
};
}