spee.ch/views/partials/publishChannel.handlebars

118 lines
5.3 KiB
Handlebars
Raw Normal View History

2017-09-19 17:47:24 +02:00
<div id="claim-channel-input-area">
2017-09-19 21:54:23 +02:00
<p>
2017-09-20 20:36:20 +02:00
<div id="input-error-channel-select" class="info-message info-message--failure"></div>
2017-09-20 00:39:54 +02:00
<label for="channel-name-select">Channel:</label>
2017-09-20 20:36:20 +02:00
<select type="text" id="channel-name-select" name="channel" value="channel" onclick="toggleChannel(event)">
2017-09-19 21:54:23 +02:00
{{#if user}}
<option value="{{user.channelName}}" >{{user.channelName}}</option>
2017-09-19 21:54:23 +02:00
{{/if}}
<option value="@speech" >Anonymous</option>
2017-09-20 23:39:20 +02:00
<option value="login">Login</option>
2017-09-19 21:54:23 +02:00
<option value="new" >New</option>
</select>
</p>
2017-09-20 23:39:20 +02:00
<div id="channel-login-details" hidden="true">
<p>
<form id="channel-login-form">
<div>
<label for="login-channel-name">Channel Name: </label>
@<input type="text" name="login-channel-name" id="login-channel-name" class="input-text input-text--primary" placeholder="" value="">
</div>
<div>
<label for="login-channel-password" >Password: </label>
<input type="password" name="login-channel-password" id="login-channel-password" class="input-text input-text--primary" placeholder="" value="">
</div>
</form>
</p>
<button onclick="loginToChannel(event)">Login</button>
</div>
2017-09-19 21:54:23 +02:00
<div id="channel-create-details" hidden="true">
<p>
2017-09-20 20:36:20 +02:00
<form id="publish-channel-form">
<div>
<div id="input-error-channel-name" class="info-message info-message--failure"></div>
2017-09-20 23:39:20 +02:00
<label for="new-channel-name">Channel Name: </label>
@<input type="text" name="new-channel-name" id="new-channel-name" class="input-text input-text--primary" placeholder="exampleChannel" value="" oninput="checkChannelName(event.target.value)">
2017-09-20 20:36:20 +02:00
<span id="input-success-channel-name" class="info-message info-message--success"></span>
</div>
<div>
<div id="input-error-channel-password" class="info-message info-message--failure"></div>
2017-09-20 23:39:20 +02:00
<label for="new-channel-password">Password: </label>
<input type="password" name="new-channel-password" id="new-channel-password" placeholder="" value="" class="input-text input-text--primary">
2017-09-20 20:36:20 +02:00
</div>
</form>
2017-09-19 21:54:23 +02:00
</p>
2017-09-20 23:39:20 +02:00
<button onclick="publishNewChannel(event)">Create Channel</button>
2017-09-19 17:47:24 +02:00
</div>
</div>
2017-09-20 23:39:20 +02:00
<script src="/assets/js/authFunctions.js"></script>
2017-09-19 17:47:24 +02:00
<script type="text/javascript">
2017-09-20 20:36:20 +02:00
function toggleChannel (event) {
const createChannelTool = document.getElementById('channel-create-details');
2017-09-20 23:39:20 +02:00
const loginToChannelTool = document.getElementById('channel-login-details');
2017-09-19 17:47:24 +02:00
const selectedOption = event.target.selectedOptions[0].value;
2017-09-20 23:39:20 +02:00
if (selectedOption === 'new') {
createChannelTool.hidden = false;
loginToChannelTool.hidden = true;
} else if (selectedOption === 'login') {
loginToChannelTool.hidden = false;
2017-09-19 17:47:24 +02:00
createChannelTool.hidden = true;
2017-09-20 00:39:54 +02:00
} else {
2017-09-20 23:39:20 +02:00
loginToChannelTool.hidden = true;
createChannelTool.hidden = true;
hideError(document.getElementById('input-error-channel-select'));
2017-09-19 17:47:24 +02:00
}
}
2017-09-20 20:36:20 +02:00
function publishNewChannel (event) {
const channelName = `@${document.getElementById('new-channel-name').value}`;
const password = document.getElementById('new-channel-password').value;
const channelNameErrorDisplayElement = document.getElementById('input-error-channel-name');
const passwordErrorDisplayElement = document.getElementById('input-error-channel-password');
// prevent default so this script can handle submission
event.preventDefault();
// validate submission
validateNewChannelSubmission(channelName, password)
.then(() => {
2017-09-20 23:39:20 +02:00
document.getElementById('channel-create-details').innerHTML = '<p>Your channel is being created...</p>';
return sendAuthRequest(channelName, password, '/signup') // post the request
2017-09-20 20:36:20 +02:00
})
.then(() => {
console.log('success');
2017-09-20 23:39:20 +02:00
document.getElementById('channel-create-details').innerHTML = '<p>Your channel has been successfully created!</p>';
// referesh window logged in as the channel
window.location.href = `/`;
2017-09-20 20:36:20 +02:00
})
.catch(error => {
if (error.name === 'ChannelNameError'){
showError(channelNameErrorDisplayElement, error.message);
} else if (error.name === 'ChannelPasswordError'){
showError(passwordErrorDisplayElement, error.message);
} else {
console.log('failure:', error);
}
})
2017-09-19 17:47:24 +02:00
}
2017-09-20 23:39:20 +02:00
function loginToChannel (event) {
const channelName = `@${document.getElementById('login-channel-name').value}`;
const password = document.getElementById('login-channel-password').value;
// prevent default
event.preventDefault()
// send request
sendAuthRequest(channelName, password, '/login')
.then(() => {
console.log('login success');
//window.location.href = `/${channelName}`;
})
.catch(error => {
console.log('login failure:', error);
})
}
2017-09-19 17:47:24 +02:00
</script>