Skip to content

Node.js based forum software built for the modern web

feat: basic register() method and modal tpl

Julian Lam754248862271
diff --git a/public/language/en-GB/intents.json b/public/language/en-GB/intents.json
new file mode 100644
index 0000000000..114797c91f
--- /dev/null
+++ b/public/language/en-GB/intents.json
@@ -0,0 +1,13 @@
+{
+ "title": "Register Open Social Web Handle",
+ "description": "Enter your Open Social Web handle to register for Activity Intents. This will allow you to share content from this forum to your social profile.",
+ "handle-label": "Open Social Web Handle",
+ "handle-placeholder": "[email protected]",
+ "register-button": "Register Handle",
+ "registered-handles": "Registered Handles",
+ "no-handles": "No handles registered yet.",
+ "intents-label": "Supported Intents",
+ "register-success": "Handle registered successfully!",
+ "register-error": "Failed to register handle. Please check the handle format and try again.",
+ "invalid-handle": "Please enter a valid handle in the format @[email protected]"
+}
diff --git a/public/src/modules/intents.js b/public/src/modules/intents.js
index 60b22afec8..316b926b0a 100644
--- a/public/src/modules/intents.js
+++ b/public/src/modules/intents.js
@@ -2,6 +2,7 @@
import { dialog } from 'bootbox';
import { get } from 'api';
+import * as alerts from './alerts';
const STORAGE_KEY = 'ap:intents:handles';
@@ -65,9 +66,10 @@ export async function refresh(handle) {
try {
const result = await get(`/api/v3/intents/query/${handle}`);
- if (result && Array.isArray(result.intents)) {
- save(handle, result.intents);
- return { intents: result.intents };
+ if (result && result.intents && typeof result.intents === 'object') {
+ const intents = Object.keys(result.intents);
+ save(handle, intents);
+ return { intents };
}
} catch (e) {
// Network or server error — handle may not support intents
@@ -76,6 +78,73 @@ export async function refresh(handle) {
}
export function register() {
- // Throws a modal asking user to enter their Open Social Web handle
- // Use dialog(), template is at src/view/modals/intents/register.tpl
+ const map = list();
+ const handles = Array.from(map.entries()).map(([handle, intents]) => ({ handle, intents }));
+
+ app.parseAndTranslate('modals/intents/register', {
+ description: '[[intents:description]]',
+ handles,
+ }, (html) => {
+ const modal = bootbox.dialog({
+ title: '[[intents:title]]',
+ message: html,
+ });
+
+ // html.on('hidden.bs.modal', function () {
+ // html.remove();
+ // });
+
+ const handleInput = modal.find('#intents-handle-input');
+ const submitBtn = modal.find('#intents-register-btn');
+
+ const validateHandle = () => {
+ const val = handleInput.val().trim();
+ // Validate: must be in format @username@domain or username@domain
+ const valid = /^@?[\w.-]+@[\w.-]+\.[\w]{2,}$/.test(val);
+ submitBtn.prop('disabled', !valid);
+ };
+
+ handleInput.on('input', validateHandle);
+ validateHandle();
+
+ modal.find('#intents-register-form').on('submit', async (ev) => {
+ ev.preventDefault();
+ const handle = handleInput.val().trim();
+
+ submitBtn.prop('disabled', true).text('[[global:loading]]');
+
+ try {
+ const result = await refresh(handle);
+ if (result) {
+ modal.modal('hide');
+ alerts.success('[[intents:register-success]]');
+ require(['hooks'], (hooks) => {
+ hooks.fire('action:intents.registered', { handle, intents: result.intents });
+ });
+ }
+ } catch (e) {
+ alerts.error('[[intents:register-error]]');
+ } finally {
+ submitBtn.prop('disabled', false).text('[[intents:register-button]]');
+ }
+ });
+
+ modal.on('click', '[data-action="remove"]', function () {
+ const handleToRemove = $(this).attr('data-handle');
+ const map = list();
+ map.delete(handleToRemove);
+ const entries = Array.from(map.entries()).map(([h, i]) => ({ handle: h, intents: i }));
+ try {
+ localStorage.setItem(STORAGE_KEY, JSON.stringify(entries));
+ } catch (e) {
+ // silently fail
+ }
+ $(this).closest('li').remove();
+
+ if (!map.size) {
+ modal.find('#intents-registered-list').closest('hr').next('h6, p').remove();
+ modal.find('#intents-registered-list').closest('hr').prev('p').after('<p class="text-muted mt-3">[[intents:no-handles]]</p>');
+ }
+ });
+ });
}
\ No newline at end of file
diff --git a/src/views/modals/intents/register.tpl b/src/views/modals/intents/register.tpl
index 00807ddd43..d4cc1d0c2f 100644
--- a/src/views/modals/intents/register.tpl
+++ b/src/views/modals/intents/register.tpl
@@ -1,7 +1,39 @@
-<!-- Bootstrap modal
+<div class="modal-intents-register">
+ <p class="text-muted">{{{ if description }}}{description}{{{ end }}}</p>
- - Shows existing registered handles
- - Bootstrap form input to register a new handle
- - Add button to kickstart handle registration
+ <form id="intents-register-form" class="mt-3">
+ <div class="mb-3">
+ <label class="form-label" for="intents-handle-input">[[intents:handle-label]]</label>
+ <input
+ id="intents-handle-input"
+ type="text"
+ class="form-control"
+ placeholder="[[intents:handle-placeholder]]"
+ autocomplete="off"
+ />
+ </div>
--->
\ No newline at end of file
+ <button type="submit" class="btn btn-primary" id="intents-register-btn" disabled>
+ [[intents:register-button]]
+ </button>
+ </form>
+
+ {{{ if (handles && handles.length) }}}
+ <hr />
+ <h6 class="mt-3">[[intents:registered-handles]]</h6>
+ <ul class="list-group list-group-flush" id="intents-registered-list">
+ {{{ each handles }}}
+ <li class="list-group-item d-flex justify-content-between align-items-center">
+ <span>{./handle}</span>
+ <button type="button" class="btn btn-sm btn-outline-danger" data-action="remove" data-handle="{./handle}">
+ <i class="fa fa-times"></i>
+ </button>
+ </li>
+ {{{ end }}}
+ </ul>
+ {{{ end }}}
+
+ {{{ if (!handles || !handles.length) }}}
+ <p class="text-muted mt-3">[[intents:no-handles]]</p>
+ {{{ end }}}
+</div>