@cshah18/sdk 3.0.4 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -307,11 +307,44 @@ var CoBuySDK = (function (exports) {
307
307
  this.currentSessionId = null;
308
308
  this.onGroupJoined = null;
309
309
  this.onViewProgress = null;
310
+ this.socketListenerRegistered = false;
310
311
  this.escapeHandler = (event) => {
311
312
  if (event.key === "Escape") {
312
313
  this.close();
313
314
  }
314
315
  };
316
+ this.handleGroupMemberJoinedEvent = (event) => {
317
+ this.onGroupMemberJoined(event);
318
+ };
319
+ /** Handle group member joined socket event and update groups list */
320
+ this.onGroupMemberJoined = (event) => {
321
+ const detail = event.detail || {};
322
+ const productId = detail.product_id;
323
+ const groupData = detail.group;
324
+ // Only process if this is for the current product
325
+ if (!productId || !this.currentProductId || productId !== this.currentProductId) {
326
+ return;
327
+ }
328
+ if (!groupData) {
329
+ return;
330
+ }
331
+ const groupId = groupData.id;
332
+ const participantsCount = groupData.participants_count;
333
+ if (!groupId) {
334
+ return;
335
+ }
336
+ // Find and update the group in the list
337
+ const groupIndex = this.groups.findIndex((g) => g.groupId === groupId);
338
+ if (groupIndex !== -1) {
339
+ // Update the joined count
340
+ if (typeof participantsCount === "number") {
341
+ this.groups[groupIndex].joined = participantsCount;
342
+ }
343
+ this.logger.info(`[GroupListModal] Updated group ${groupId} - participants: ${participantsCount}`);
344
+ // Re-render the specific group card
345
+ this.updateGroupCard(groupId);
346
+ }
347
+ };
315
348
  this.logger = new Logger(debug);
316
349
  this.groups = groups;
317
350
  this.liveCount = liveCount;
@@ -338,6 +371,65 @@ var CoBuySDK = (function (exports) {
338
371
  hasGroup(groupId) {
339
372
  return this.groups.some((group) => group.groupId === groupId);
340
373
  }
374
+ /** Subscribe to socket events for real-time group updates */
375
+ subscribeToSocketEvents() {
376
+ if (typeof window === "undefined" || this.socketListenerRegistered) {
377
+ return;
378
+ }
379
+ window.addEventListener("group:member:joined", this.handleGroupMemberJoinedEvent);
380
+ this.socketListenerRegistered = true;
381
+ this.logger.debug("[GroupListModal] Socket event listeners registered");
382
+ }
383
+ /** Unsubscribe from socket events */
384
+ unsubscribeFromSocketEvents() {
385
+ if (typeof window === "undefined" || !this.socketListenerRegistered) {
386
+ return;
387
+ }
388
+ window.removeEventListener("group:member:joined", this.handleGroupMemberJoinedEvent);
389
+ this.socketListenerRegistered = false;
390
+ this.logger.debug("[GroupListModal] Socket event listeners unregistered");
391
+ }
392
+ /** Update the rendered group card with new data */
393
+ updateGroupCard(groupId) {
394
+ if (!this.overlayEl) {
395
+ return;
396
+ }
397
+ const groupCard = this.overlayEl.querySelector(`[data-group-id="${groupId}"]`);
398
+ if (!groupCard) {
399
+ return;
400
+ }
401
+ const group = this.groups.find((g) => g.groupId === groupId);
402
+ if (!group) {
403
+ return;
404
+ }
405
+ // Update the progress info
406
+ const remaining = Math.max(group.total - group.joined, 0);
407
+ const joinedCountEl = groupCard.querySelector(".cobuy-joined-count");
408
+ const spotsLeftEl = groupCard.querySelector(".cobuy-spots-left");
409
+ if (joinedCountEl) {
410
+ joinedCountEl.textContent = `${group.joined} Joined`;
411
+ }
412
+ if (spotsLeftEl) {
413
+ spotsLeftEl.textContent = `${remaining} ${remaining === 1 ? "spot" : "spots"} left`;
414
+ }
415
+ // Update the progress bar
416
+ const progressFill = groupCard.querySelector(".cobuy-gl-progress-fill");
417
+ if (progressFill) {
418
+ const pct = Math.max(0, Math.min(100, Math.round((group.joined / group.total) * 100)));
419
+ progressFill.style.width = `${pct}%`;
420
+ }
421
+ // Update member avatars
422
+ const membersContainer = groupCard.querySelector(".cobuy-group-members");
423
+ if (membersContainer) {
424
+ // Clear existing avatars
425
+ membersContainer.innerHTML = "";
426
+ // Recreate avatars with updated joined count
427
+ for (let i = 0; i < group.total; i++) {
428
+ const avatar = this.createMemberAvatar(i >= group.joined);
429
+ membersContainer.appendChild(avatar);
430
+ }
431
+ }
432
+ }
341
433
  async open(productId, sessionId, joinedGroupId) {
342
434
  if (this.overlayEl) {
343
435
  this.logger.debug("Group list modal already open");
@@ -383,6 +475,8 @@ var CoBuySDK = (function (exports) {
383
475
  this.overlayEl = overlay;
384
476
  document.body.appendChild(overlay);
385
477
  document.addEventListener("keydown", this.escapeHandler);
478
+ // Subscribe to socket events for real-time group updates
479
+ this.subscribeToSocketEvents();
386
480
  requestAnimationFrame(() => {
387
481
  overlay.classList.add("cobuy-gl-open");
388
482
  });
@@ -480,6 +574,8 @@ var CoBuySDK = (function (exports) {
480
574
  if (!this.overlayEl) {
481
575
  return;
482
576
  }
577
+ // Unsubscribe from socket events
578
+ this.unsubscribeFromSocketEvents();
483
579
  // Clear any running countdown intervals to avoid leaks
484
580
  this.countdownIntervals.forEach((id) => window.clearInterval(id));
485
581
  this.countdownIntervals = [];
@@ -723,6 +819,7 @@ var CoBuySDK = (function (exports) {
723
819
  createGroupCard(group) {
724
820
  const card = document.createElement("div");
725
821
  card.className = "cobuy-group-card";
822
+ card.setAttribute("data-group-id", group.groupId);
726
823
  const header = document.createElement("div");
727
824
  header.className = "cobuy-group-card-header";
728
825
  const groupId = document.createElement("div");
@@ -2636,7 +2733,6 @@ var CoBuySDK = (function (exports) {
2636
2733
  }
2637
2734
  /** Fetch latest group data and re-render containers */
2638
2735
  async refreshGroupDataFromRealtime() {
2639
- console.log("request group data realtime callledddd");
2640
2736
  // Debounce rapid refreshes to prevent loops and reduce API load
2641
2737
  const now = Date.now();
2642
2738
  if (now - this.lastGroupDataRefreshTime < this.GROUP_REFRESH_DEBOUNCE) {
@@ -2661,6 +2757,9 @@ var CoBuySDK = (function (exports) {
2661
2757
  this.groupFulfilled = true;
2662
2758
  }
2663
2759
  }
2760
+ else {
2761
+ this.groupFulfilled = false;
2762
+ }
2664
2763
  this.refreshRenderedContainers();
2665
2764
  }
2666
2765
  catch (error) {
@@ -3051,7 +3150,6 @@ var CoBuySDK = (function (exports) {
3051
3150
  let groupData = null;
3052
3151
  if (this.apiClient) {
3053
3152
  rewardData = await this.fetchRewardWithRetry(options.productId);
3054
- console.log("from renderrrr");
3055
3153
  groupData = await this.fetchPrimaryGroup(options.productId);
3056
3154
  this.currentGroupData = groupData;
3057
3155
  this.currentGroupId = (groupData === null || groupData === void 0 ? void 0 : groupData.id) || null;
@@ -3308,7 +3406,6 @@ var CoBuySDK = (function (exports) {
3308
3406
  */
3309
3407
  async fetchPrimaryGroup(productId) {
3310
3408
  var _a;
3311
- console.log("fetch primary group called");
3312
3409
  if (!this.apiClient) {
3313
3410
  return null;
3314
3411
  }
@@ -3425,6 +3522,7 @@ var CoBuySDK = (function (exports) {
3425
3522
  createWidget(rewardData, container, options) {
3426
3523
  const isFulfilled = this.groupFulfilled;
3427
3524
  const activeReward = this.frozenReward || (rewardData === null || rewardData === void 0 ? void 0 : rewardData.reward) || null;
3525
+ this.logger.info(`activeReward: ${JSON.stringify(activeReward)}`);
3428
3526
  const wrapper = document.createElement("div");
3429
3527
  wrapper.className = "cobuy-widget";
3430
3528
  wrapper.style.display = "grid";
@@ -3464,28 +3562,31 @@ var CoBuySDK = (function (exports) {
3464
3562
  rewardLine.style.opacity = "0";
3465
3563
  rewardLine.style.animation =
3466
3564
  "cobuy-fadeIn var(--cobuy-animation-duration, 300ms) var(--cobuy-animation-easing, ease-out) forwards";
3467
- if (isFulfilled) {
3468
- const rewardText = this.formatRewardText(activeReward);
3469
- rewardLine.textContent = rewardText
3470
- ? `Reward available: ${rewardText}`
3471
- : "Reward available for this group";
3472
- rewardLine.setAttribute("aria-label", rewardText ? `Reward locked in: ${rewardText}` : "Reward available for this group");
3473
- rewardLine.title = rewardLine.textContent;
3474
- }
3475
- else if (activeReward) {
3476
- const rewardText = this.formatRewardText(activeReward);
3477
- rewardLine.textContent = rewardText
3478
- ? `Save up to ${rewardText} with CoBuy`
3479
- : "CoBuy reward available";
3480
- rewardLine.setAttribute("aria-label", `Eligible for CoBuy reward: ${rewardLine.textContent}`);
3481
- rewardLine.title = rewardLine.textContent;
3482
- }
3483
- else {
3484
- rewardLine.textContent = "CoBuy offer loading or unavailable";
3485
- rewardLine.setAttribute("aria-label", "CoBuy offer loading or unavailable");
3486
- rewardLine.title = "CoBuy offer loading or unavailable";
3487
- rewardLine.style.color = "#6b7280";
3488
- }
3565
+ // if (isFulfilled) {
3566
+ // const rewardText = this.formatRewardText(activeReward);
3567
+ // rewardLine.textContent = rewardText
3568
+ // ? `Reward available: ${rewardText}`
3569
+ // : "Reward available for this group";
3570
+ // rewardLine.setAttribute(
3571
+ // "aria-label",
3572
+ // rewardText ? `Reward locked in: ${rewardText}` : "Reward available for this group",
3573
+ // );
3574
+ // rewardLine.title = rewardLine.textContent;
3575
+ // }
3576
+ // else if (activeReward) {
3577
+ // const rewardText = this.formatRewardText(activeReward);
3578
+ // rewardLine.textContent = rewardText
3579
+ // ? `Save up to ${rewardText} with CoBuy`
3580
+ // : "CoBuy reward available";
3581
+ // rewardLine.setAttribute("aria-label", `Eligible for CoBuy reward: ${rewardLine.textContent}`);
3582
+ // rewardLine.title = rewardLine.textContent;
3583
+ // }
3584
+ // else {
3585
+ // rewardLine.textContent = "CoBuy offer loading or unavailable";
3586
+ // rewardLine.setAttribute("aria-label", "CoBuy offer loading or unavailable");
3587
+ // rewardLine.title = "CoBuy offer loading or unavailable";
3588
+ // rewardLine.style.color = "#6b7280";
3589
+ // }
3489
3590
  sections.reward = rewardLine;
3490
3591
  // Button - semantic button element with accessibility
3491
3592
  const button = document.createElement("button");
@@ -8954,9 +9055,7 @@ var CoBuySDK = (function (exports) {
8954
9055
  const ref = window.localStorage.getItem(key);
8955
9056
  if (ref) {
8956
9057
  this.logger.debug(`[SDK] Retrieved checkout reference via prefix: ${key}`);
8957
- const parsedGroupId = key.startsWith(`${basePrefix}_`)
8958
- ? key.substring(basePrefix.length + 1)
8959
- : null;
9058
+ const parsedGroupId = key.startsWith(`${basePrefix}_`) ? key.substring(basePrefix.length + 1) : null;
8960
9059
  return { key, checkoutRef: ref, groupId: parsedGroupId };
8961
9060
  }
8962
9061
  }
@@ -9377,7 +9476,6 @@ var CoBuySDK = (function (exports) {
9377
9476
  const pid = typeof w.getProductId === "function" ? w.getProductId() : null;
9378
9477
  if (!productId || pid === productId) {
9379
9478
  if (typeof w.requestRefresh === "function") {
9380
- console.log("calling refresshh now");
9381
9479
  refreshPromises.push(w.requestRefresh());
9382
9480
  }
9383
9481
  }