Validation for relay URL entry.

This commit is contained in:
Ryan Breen
2023-01-24 23:17:15 -05:00
parent 84a8d5ddeb
commit 6d9235d8c0
3 changed files with 31 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ Alpine.data('options', () => ({
profileIndex: 0,
relays: [],
newRelay: '',
urlError: '',
async init() {
await this.getProfileNames();
@@ -35,17 +36,38 @@ Alpine.data('options', () => ({
payload: [this.profileIndex, this.relays],
});
await this.getRelaysForProfile();
this.newRelay = '';
},
async addRelay() {
this.relays.push({ url: this.newRelay, read: true, write: true });
await this.saveRelaysForProfile();
try {
let url = new URL(this.newRelay);
if (url.protocol !== 'wss:') {
this.setUrlError('Must be a websocket url');
}
let urls = this.relays.map(v => v.url);
if (urls.includes(url.href)) {
this.setUrlError('URL already exists');
return;
}
this.relays.push({ url: url.href, read: true, write: true });
await this.saveRelaysForProfile();
} catch (error) {
this.setUrlError('Invalid websocket URL');
}
},
async deleteRelay(index) {
this.relays.splice(index, 1);
await this.saveRelaysForProfile();
},
setUrlError(message) {
this.urlError = message;
setTimeout(() => {
this.urlError = '';
}, 3000);
},
}));
Alpine.start();