Skip to content

Commit

Permalink
fix(unlock-app): Improve event creation process to be semi-async (#15386
Browse files Browse the repository at this point in the history
)

update event creation logic
  • Loading branch information
0xTxbi authored and julien51 committed Jan 22, 2025
1 parent 0167333 commit d75fe9a
Showing 1 changed file with 37 additions and 15 deletions.
52 changes: 37 additions & 15 deletions unlock-app/src/components/content/event/NewEvent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const defaultEventCheckoutConfigForLockOnNetwork = (
title: 'Registration',
locks: {
[lockAddress]: {
network: network,
network,
metadataInputs: [
{
name: 'email',
Expand Down Expand Up @@ -56,21 +56,26 @@ export const NewEvent = () => {

const onSubmit = async (formData: NewEventForm) => {
try {
// Create the event with pending status
const { data: event } = await locksmith.saveEventData({
const walletService = await getWalletService(formData.network)

// Create initial event with pending status
const pendingEventData = {
data: {
...formDataToMetadata({
name: formData.lock.name,
...formData.metadata,
}),
...formData.metadata,
status: EventStatus.PENDING,
},
})
status: EventStatus.PENDING,
}

// Deploy the lock
const walletService = await getWalletService(formData.network)
walletService.createLock(
// Create pending event first
const { data: pendingEvent } =
await locksmith.saveEventData(pendingEventData)

// Deploy the lock and wait for the address
const lockAddress = await walletService.createLock(
{
...formData.lock,
name: formData.lock.name,
Expand All @@ -80,24 +85,41 @@ export const NewEvent = () => {
{},
async (createLockError, transactionHash) => {
if (createLockError) {
console.error('Error creating lock:', createLockError)
throw createLockError
}
if (transactionHash) {
// update the event with the transaction hash
await locksmith.updateEventData(event.slug, {
transactionHash,
})
setTransactionDetails({
hash: transactionHash,
network: formData.network,
slug: event.slug,
slug: pendingEvent.slug,
})
}
}
)

// If lock is created, update metadata and update event status to deployed
if (lockAddress) {
// Update lock metadata
await locksmith.updateLockMetadata(formData.network, lockAddress, {
metadata: {
name: `Ticket for ${formData.lock.name}`,
image: formData.metadata.image,
},
})

// Update existing event with checkout config and deployed status
await locksmith.updateEventData(pendingEvent.slug, {
status: EventStatus.DEPLOYED,
checkoutConfig: {
name: `Checkout config for ${formData.lock.name}`,
config: defaultEventCheckoutConfigForLockOnNetwork(
lockAddress,
formData.network
),
},
})
}
} catch (error) {
console.error('Error in event creation process:', error)
ToastHelper.error(
'There was an error creating your event. Please try again.'
)
Expand Down

0 comments on commit d75fe9a

Please sign in to comment.