Skip to content

Commit 3ea103c

Browse files
authored
Merge pull request #24 from peek-travel/feature/odyssey-ui-elements
Wrap odyssey activity picker in live view component
2 parents cf23752 + d94cf84 commit 3ea103c

20 files changed

Lines changed: 792 additions & 1 deletion

README.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,122 @@ module.exports = {
9595

9696
This will ensure your application includes all the necessary styles for PeekAppSDK components.
9797

98+
## JavaScript Integration
99+
100+
To use PeekAppSDK's JavaScript hooks in your Phoenix LiveView application, add this to your `assets/js/app.js` file:
101+
102+
```javascript
103+
// Import PeekAppSDK hooks
104+
import PeekAppSDKHooks from "peek_app_sdk"
105+
106+
// If you don't have any other hooks:
107+
let liveSocket = new LiveSocket("/live", Socket, {
108+
longPollFallbackMs: 2500,
109+
params: {_csrf_token: csrfToken},
110+
hooks: PeekAppSDKHooks
111+
})
112+
113+
// If you have other hooks, merge them:
114+
let Hooks = {
115+
...PeekAppSDKHooks,
116+
// your other hooks here
117+
}
118+
119+
let liveSocket = new LiveSocket("/live", Socket, {
120+
longPollFallbackMs: 2500,
121+
params: {_csrf_token: csrfToken},
122+
hooks: Hooks
123+
})
124+
```
125+
126+
The import works because PeekAppSDK includes a `package.json` file that tells Node.js where to find the JavaScript hooks.
127+
128+
## Odyssey UI Components Integration
129+
130+
PeekAppSDK includes Odyssey UI components that provide rich interactive elements for your Phoenix LiveView applications. To integrate these components:
131+
132+
### 1. Add the Odyssey UI Components Import
133+
134+
In your Phoenix web module (e.g., `lib/your_app_web.ex`), add the import to both the `html` and `live_view` functions:
135+
136+
```elixir
137+
def html do
138+
quote do
139+
use Phoenix.Component
140+
import PeekAppSDK.UI.Odyssey # Add this line
141+
import YourAppWeb.CoreComponents
142+
# ... other imports
143+
end
144+
end
145+
146+
def live_view do
147+
quote do
148+
use Phoenix.LiveView,
149+
layout: {YourAppWeb.Layouts, :app}
150+
151+
import PeekAppSDK.UI.Odyssey # Add this line
152+
import YourAppWeb.CoreComponents
153+
# ... other imports
154+
end
155+
end
156+
```
157+
158+
### 2. Include Odyssey Web Components JavaScript
159+
160+
In your `assets/js/app.js` file, add the Odyssey web components:
161+
162+
```javascript
163+
// Import Odyssey web components (required for UI components to work)
164+
require('../../deps/peek_app_sdk/priv/static/odyssey_web_components.min.js');
165+
166+
// Import PeekAppSDK hooks
167+
import peekAppSDK from "peek_app_sdk"
168+
169+
const Hooks = {
170+
...peekAppSDK
171+
}
172+
173+
let liveSocket = new LiveSocket("/live", Socket, {
174+
longPollFallbackMs: 2500,
175+
params: {_csrf_token: csrfToken},
176+
hooks: Hooks
177+
})
178+
```
179+
180+
### 3. Include Odyssey Styles
181+
182+
In your `assets/css/app.css` file, add the Odyssey assets to your Tailwind sources:
183+
184+
```css
185+
@import "tailwindcss/base";
186+
@import "tailwindcss/components";
187+
@import "tailwindcss/utilities";
188+
189+
@source "../css";
190+
@source "../js";
191+
@source "../../lib/your_app_web";
192+
@source "../../deps/peek_app_sdk/assets"; /* Add this line */
193+
```
194+
195+
### 4. Using Odyssey Components
196+
197+
Once integrated, you can use Odyssey components in your LiveView templates:
198+
199+
```heex
200+
<.form for={@form} phx-change="activity_changed">
201+
<.odyssey_activity_picker
202+
install_id={@app_installation_id}
203+
field={@form[:activity_id]}
204+
/>
205+
</.form>
206+
```
207+
208+
The activity picker component will automatically handle activity selection and update the form field when activities are chosen.
209+
210+
### Updating Odyssey Components
211+
212+
Simply update odyssey_web_components.js and odyssey_web_components.css in your assets folder and run `mix assets.deploy` and get merged into main. When upstream apps run a mix deps.update peek_app_sdk they'll get the updated odyssey assets.
213+
98214
# PeekAppSDK Metrics
99215

100216
This document describes how to use the metrics functionality in the PeekAppSDK.

assets/js/odyssey.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const Hooks = {
2+
OdysseyActivityPicker: {
3+
mounted() {
4+
const picker = this.el.querySelector("odyssey-product-picker")
5+
6+
if (picker) {
7+
picker.addEventListener("click", (event) => {
8+
event.preventDefault()
9+
event.stopPropagation()
10+
})
11+
12+
13+
picker.addEventListener("change", (event) => {
14+
event.preventDefault()
15+
event.stopPropagation()
16+
17+
const hiddenInput = this.el.querySelector('input[type="hidden"]')
18+
hiddenInput.value = event.detail.selectedIds.join(",")
19+
20+
// Trigger a change event on the hidden input to notify Phoenix
21+
hiddenInput.dispatchEvent(new Event('input', { bubbles: true }))
22+
})
23+
}
24+
}
25+
}
26+
}
27+
28+
export default Hooks

assets/odyssey/odyssey_web_components.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)