Skip to content

Commit e97a9e5

Browse files
committed
Fixup
1 parent a6a6664 commit e97a9e5

File tree

3 files changed

+57
-16
lines changed

3 files changed

+57
-16
lines changed

src/headless/plugins/pubsub/api.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export default {
204204
* Creates a PubSub node at a given service
205205
* @param {string} jid - The PubSub service JID
206206
* @param {string} node - The node to create
207-
* @param {PubSubConfigOptions} config The configuration options
207+
* @param {PubSubConfigOptions} [config] The configuration options
208208
* @returns {Promise<void>}
209209
*/
210210
async create(jid, node, config) {
@@ -216,14 +216,19 @@ export default {
216216
to="${jid}">
217217
<pubsub xmlns="http://jabber.org/protocol/pubsub">
218218
<create node="${node}"/>
219-
<configure>
219+
${
220+
config
221+
? stx`
222+
<configure>
220223
<x xmlns="${Strophe.NS.XFORM}" type="submit">
221224
<field var="FORM_TYPE" type="hidden">
222225
<value>${Strophe.NS.PUBSUB}#nodeconfig</value>
223226
</field>
224227
${Object.entries(config).map(([k, v]) => stx`<field var="pubsub#${k}"><value>${v}</value></field>`)}
225228
</x>
226-
</configure>
229+
</configure>`
230+
: ''
231+
}
227232
</pubsub>
228233
</iq>`;
229234
return await api.sendIQ(iq);

src/plugins/todo/modals/add-todo-modal.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { _converse, api } from '@converse/headless';
1+
import { _converse, api, converse, parsers } from '@converse/headless';
22
import BaseModal from 'plugins/modal/modal.js';
33
import tplAddTodo from './templates/add-todo-modal.js';
44
import { __ } from 'i18n';
55

6+
const { Strophe } = converse.env;
7+
68
export default class AddTodoModal extends BaseModal {
79
initialize() {
810
super.initialize();
@@ -29,13 +31,29 @@ export default class AddTodoModal extends BaseModal {
2931
*/
3032
async createTodo(ev) {
3133
ev.preventDefault();
32-
3334
const form = /** @type {HTMLFormElement} */ (ev.target);
3435
const data = new FormData(form);
3536
const name = data.get('name');
36-
const jid = data.get('jid');
37+
const jid = data.get('jid') ?? _converse.state.session.get('domain');
38+
39+
const service_jids = await api.disco.entities.find(Strophe.NS.PUBSUB, jid);
40+
if (service_jids.length === 0) {
41+
this.alert(__('Could not find a PubSub service to host your todo list'), 'danger');
42+
this.state.set({ manual_jid: true });
43+
return;
44+
} else if (service_jids.length > 1) {
45+
this.alert(__('Found multiple possible PubSub services to host your todo list, please choose one.'));
46+
this.state.set({ services: service_jids, manual_jid: true });
47+
return;
48+
}
3749

38-
api.pubsub.nodes.create(jid, name);
50+
try {
51+
await api.pubsub.create(service_jids[0].get('jid'), name);
52+
} catch (e) {
53+
const err = await parsers.parseErrorStanza(e);
54+
this.alert(__('Sorry, an error occurred: %s', err.message), 'danger');
55+
return;
56+
}
3957
form.reset();
4058
this.modal.hide();
4159
}

src/plugins/todo/modals/templates/add-todo-modal.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,36 @@ import { __ } from 'i18n';
88
export default (el) => {
99
const i18n_create = __('Create');
1010
const label_name = __('Todo name');
11+
const label_service = __('PubSub service (XMPP Address) for your todo list');
1112

12-
return html` <form
13-
class="converse-form add-chatroom needs-validation"
14-
@submit=${(ev) => el.createTodo(ev)}
15-
novalidate
16-
>
13+
const pubsub_services = el.state.get('services') ?? [];
14+
15+
return html`<form class="converse-form" @submit=${(ev) => el.createTodo(ev)}>
1716
<div class="mb-3">
18-
<label for="chatroom" class="form-label">${label_name}:</label>
19-
<div class="input-group">
20-
<input type="text" class="form-control" id="chatroom" placeholder="${label_name}" required />
21-
</div>
17+
<label for="todo-name" class="form-label">${label_name}:</label>
18+
<input type="text" id="todo-name" name="name" class="form-control" placeholder="${label_name}" required />
2219
</div>
20+
21+
${el.state.get('manual_jid')
22+
? html`<div class="mb-3">
23+
${pubsub_services.length > 1
24+
? html`${__('Available PubSub services')}
25+
<ul class="list-group">
26+
${(pubsub_services ?? []).map((jid) => html`<li class="list-group-item">${jid}</li>`)}
27+
</ul>`
28+
: ''}
29+
30+
<label for="todo-jid" class="form-label">${label_service}:</label>
31+
<input
32+
type="text"
33+
id="todo-jid"
34+
name="jid"
35+
class="form-control"
36+
required
37+
/>
38+
</div>`
39+
: ''}
40+
2341
<input type="submit" class="btn btn-primary mt-3" value="${i18n_create || ''}" />
2442
</form>`;
2543
};

0 commit comments

Comments
 (0)