Validation for relay URL entry.
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user