Skip to content

Frontend state issues in editor#586

Merged
uldisrudzitis merged 5 commits intomasterfrom
fix-states
Oct 13, 2025
Merged

Frontend state issues in editor#586
uldisrudzitis merged 5 commits intomasterfrom
fix-states

Conversation

@uldisrudzitis
Copy link
Copy Markdown
Collaborator

@uldisrudzitis uldisrudzitis commented Oct 13, 2025

  • Preview component uses some of shop states for site rendering, make sure we are logged in
  • Fix setSelectedFile undefined error

Summary by CodeRabbit

  • Bug Fixes

    • Consistent, more reliable base URL construction across previews, icons, carts, and redirects.
    • Prevented malformed preview URLs by only appending a port when present.
    • Avoided requests when gallery files are missing and added guards against missing site settings.
  • Refactor

    • Initialization now reacts to both site and authentication state for more reliable setup.
  • Style

    • Minor formatting and template simplifications; flattened UI markup and safer file-selection checks.

@uldisrudzitis uldisrudzitis self-assigned this Oct 13, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Oct 13, 2025

Walkthrough

Replaces manual multi-part URL assembly with location.origin across preview, app, icon, rerender, and cart render code; shop NGXS states now wait for both current site and authenticated user using combineLatest; adds null-guards to shop settings selectors; flattens entry gallery template, simplifies file-selection logic, and adds a missing-file guard in the image editor.

Changes

Cohort / File(s) Summary of changes
Origin-based URL construction
editor/src/app/preview/preview.component.ts, editor/src/app/app.component.ts, editor/src/app/inputs/icon-readonly.component.ts, editor/src/app/rerender/template-rerender.service.ts, editor/src/app/shop/shop-cart-render.service.ts
Replaces manual location.protocol + '//' + location.hostname(+ ':'+port) construction with location.origin when composing base URLs for iframe, navigation, icon sources, and cart return URLs; formatting/indentation tweaks only elsewhere.
Auth-gated shop state initialization
editor/src/app/shop/regional-costs/shop-regional-costs.state.ts, editor/src/app/shop/settings/shop-settings.state.ts
Adds combineLatest([AppState.getSite, UserState.isLoggedIn]), filters for non-null site and isLoggedIn === true, maps to site, then continues existing initialization flows; imports updated to include combineLatest and UserState.
Selector null guards
editor/src/app/shop/settings/shop-settings.state.ts
getCurrentWeightUnit and getCurrentCurrency now early-return when currentSiteSettings is falsy to avoid undefined access.
Entry gallery template & selection logic
editor/src/app/sites/media/entry-gallery-editor.component.ts, editor/src/app/sites/media/entry-gallery-image-editor.component.ts
Flattens/simplifies template markup and dropdown SVG; uses optional chaining for media file access and adjusts selected-file assignment; adds an ngOnInit guard in the image editor to exit early if galleryFile is missing; minor formatting/animation metadata reflow.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Preview as PreviewComponent
  participant Window as window/location
  participant App as AppState.getSite
  participant User as UserState.isLoggedIn
  participant ShopR as ShopRegionalCostsState
  participant ShopS as ShopSettingsState

  rect rgba(220,235,255,0.18)
    note left of Preview: build base URL (new)
    Preview->>Window: read location.origin
    Window-->>Preview: origin (scheme + host[:port])
    Preview->>Preview: compose final URL (origin + path)
  end

  rect rgba(200,230,200,0.12)
    note over ShopR,ShopS: auth-gated initialization (new)
    App-->>ShopR: site$
    User-->>ShopR: isLoggedIn$
    ShopR->>ShopR: combineLatest(site$, isLoggedIn$)\nfilter(site && isLoggedIn)\nmap(site)\ninitialize...
    App-->>ShopS: site$
    User-->>ShopS: isLoggedIn$
    ShopS->>ShopS: combineLatest(site$, isLoggedIn$)\nfilter(site && isLoggedIn)\nmap(site)\ninitialize...
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~30 minutes

Possibly related PRs

Poem

I hop where origins are found,
I stitch the site and login sound,
I guard the file that might be gone,
I flatten nests and fix the dawn,
A rabbit's patch — the code moves on. 🐇✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The title succinctly highlights that the pull request addresses state-related issues within the editor, which aligns with the changes fixing state initialization and guarding against undefined values across multiple components.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-states

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
editor/src/app/shop/settings/shop-settings.state.ts (2)

59-68: Null guard prevents initialization errors.

The early return on lines 59-61 correctly guards against accessing properties on undefined or null currentSiteSettings. This prevents runtime errors during state initialization before shop settings are loaded.

Consider using optional chaining for additional robustness to handle cases where group_config or weightUnit might be missing:

   if (!currentSiteSettings) {
     return;
   }
-  return (
-    currentSiteSettings
-      .find((group) => {
-        return group.slug === 'group_config';
-      })
-      .settings.find((setting) => setting.slug === 'weightUnit').value || ''
-  );
+  return (
+    currentSiteSettings
+      .find((group) => group.slug === 'group_config')
+      ?.settings.find((setting) => setting.slug === 'weightUnit')?.value || ''
+  );

73-82: Null guard prevents initialization errors.

The early return correctly guards against accessing properties on undefined or null currentSiteSettings, consistent with the getCurrentWeightUnit selector.

Consider using optional chaining for additional robustness, similar to getCurrentWeightUnit:

   if (!currentSiteSettings) {
     return;
   }
-  return (
-    currentSiteSettings
-      .find((group) => {
-        return group.slug === 'group_config';
-      })
-      .settings.find((setting) => setting.slug === 'currency').value || ''
-  );
+  return (
+    currentSiteSettings
+      .find((group) => group.slug === 'group_config')
+      ?.settings.find((setting) => setting.slug === 'currency')?.value || ''
+  );
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ca0b84b and 8f0630e.

📒 Files selected for processing (3)
  • editor/src/app/preview/preview.component.ts (3 hunks)
  • editor/src/app/shop/regional-costs/shop-regional-costs.state.ts (2 hunks)
  • editor/src/app/shop/settings/shop-settings.state.ts (4 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
editor/src/app/shop/regional-costs/shop-regional-costs.state.ts (1)
editor/src/app/user/user.state.ts (1)
  • isLoggedIn (49-51)
editor/src/app/shop/settings/shop-settings.state.ts (1)
editor/src/app/user/user.state.ts (1)
  • isLoggedIn (49-51)
🔇 Additional comments (5)
editor/src/app/shop/regional-costs/shop-regional-costs.state.ts (2)

38-38: LGTM!

The combineLatest import is correctly added and used in ngxsOnInit to gate initialization on both site and authentication state.


80-99: Approve authentication-gated initialization and logout cleanup

The ngxsOnInit logic waits for a valid site and authenticated user, and the ShopState logout handler resets all shop sections—including regional costs—on logout.

editor/src/app/preview/preview.component.ts (1)

27-54: LGTM!

Formatting improvements to the component decorator. No behavioral changes.

editor/src/app/shop/settings/shop-settings.state.ts (2)

34-35: LGTM!

The imports for combineLatest and UserState are correctly added to support authentication-gated initialization.


93-112: Correct implementation of authentication-gated initialization.

The refactored ngxsOnInit properly waits for both a valid site and user authentication before initializing shop settings. This implementation is consistent with the pattern used in shop-regional-costs.state.ts, ensuring uniform behavior across shop state modules.

Comment thread editor/src/app/preview/preview.component.ts Outdated
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
editor/src/app/sites/media/entry-gallery-image-editor.component.ts (1)

187-189: Good defensive check; consider adding user feedback.

The early return guard prevents runtime errors when galleryFile is not found (e.g., invalid image order in URL, out-of-date media cache). This addresses the undefined error issues mentioned in the PR objectives. However, the user remains on the page without feedback. Consider navigating back or displaying an error message to improve the user experience.

Apply this diff to navigate back when the file is not found:

 if (!this.galleryFile) {
+  this._location.back();
   return;
 }

Alternatively, display an error message or log the issue for debugging.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f48b7d6 and 7e53f88.

📒 Files selected for processing (1)
  • editor/src/app/sites/media/entry-gallery-image-editor.component.ts (6 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
editor/src/app/sites/media/entry-gallery-image-editor.component.ts (1)
editor/src/app/shared/animations.ts (1)
  • Animations (9-29)

@uldisrudzitis uldisrudzitis merged commit 583dee8 into master Oct 13, 2025
3 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Oct 27, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant