pinafore/routes/settings/instances.html

123 lines
3.9 KiB
HTML
Raw Normal View History

2018-01-08 01:00:42 +01:00
<:Head>
2018-01-13 19:53:25 +01:00
<title>Instances</title>
2018-01-08 01:00:42 +01:00
</:Head>
<Layout page='settings'>
2018-01-13 19:53:25 +01:00
<SettingsLayout page='instances'>
<h1>Instances</h1>
2018-01-08 01:00:42 +01:00
{{#if $isUserLoggedIn}}
<ul class="instances-list">
{{#each $loggedInInstancesAsList as instance}}
<li class="instance-card">
<span>{{instance.name}}</span>
</li>
{{/each}}
</ul>
{{else}}
2018-01-13 19:53:25 +01:00
<p>Connect to an instance to start using Pinafore.</p>
{{/if}}
2018-01-08 01:00:42 +01:00
{{#if !$isUserLoggedIn}}
<form class="add-new-instance" on:submit='onSubmit(event)'>
<label for="instanceInput">Instance name:</label>
<input type="text" id="instanceInput" bind:value='$instanceNameInSearch' placeholder=''>
<button class="primary" type="submit" id="submitButton">Add instance</button>
</form>
{{/if}}
2018-01-08 01:00:42 +01:00
{{#if !$isUserLoggedIn}}
<p>Don't have an instance? <a href="https://joinmastodon.org">Join Mastodon!</a></p>
{{/if}}
2018-01-09 02:44:29 +01:00
</SettingsLayout>
2018-01-08 01:00:42 +01:00
</Layout>
<style>
2018-01-08 07:00:16 +01:00
@media (max-width: 767px) {
input {
width: 90%;
}
}
2018-01-08 01:00:42 +01:00
input {
2018-01-08 07:00:16 +01:00
width: 250px;
2018-01-08 01:00:42 +01:00
}
form.add-new-instance {
2018-01-12 18:01:46 +01:00
background: var(--form-bg);
2018-01-08 01:00:42 +01:00
padding: 5px 10px 15px;
2018-01-12 18:01:46 +01:00
margin: 20px auto;
border: 1px solid var(--form-border);
2018-01-08 01:00:42 +01:00
}
form.add-new-instance label, form.add-new-instance input, form.add-new-instance button {
2018-01-08 01:00:42 +01:00
display: block;
margin: 20px 5px;
}
</style>
<script>
import Layout from '../_components/Layout.html';
2018-01-09 02:44:29 +01:00
import SettingsLayout from './_components/SettingsLayout.html'
2018-01-08 07:00:16 +01:00
import { registerApplication, generateAuthLink, getAccessTokenFromAuthCode } from '../_utils/mastodon'
import { databasePromise } from '../_utils/database'
import { store } from '../_utils/store'
2018-01-08 07:13:15 +01:00
import { goto } from 'sapper/runtime.js'
2018-01-08 01:00:42 +01:00
export default {
2018-01-08 07:00:16 +01:00
oncreate: function () {
if (process.browser) {
(async () => {
let params = new URLSearchParams(location.search)
if (params.has('code')) {
this.onReceivedOauthCode(params.get('code'))
2018-01-08 07:00:16 +01:00
}
})()
}
},
2018-01-08 01:00:42 +01:00
components: {
2018-01-09 02:44:29 +01:00
Layout,
SettingsLayout
2018-01-08 01:00:42 +01:00
},
store: () => store,
2018-01-08 01:00:42 +01:00
methods: {
onReceivedOauthCode: async function(code) {
2018-01-13 07:24:54 +01:00
let currentRegisteredInstanceName = this.store.get('currentRegisteredInstanceName')
let currentRegisteredInstance = this.store.get('currentRegisteredInstance')
let instanceData = await (await getAccessTokenFromAuthCode(
currentRegisteredInstanceName,
currentRegisteredInstance.client_id,
currentRegisteredInstance.client_secret,
code
)).json()
2018-01-13 07:24:54 +01:00
// TODO: handle error
let loggedInInstances = this.store.get('loggedInInstances')
let loggedInInstancesInOrder = this.store.get('loggedInInstancesInOrder')
loggedInInstances[currentRegisteredInstanceName] = instanceData
if (!loggedInInstancesInOrder.includes(currentRegisteredInstanceName)) {
loggedInInstancesInOrder.push(currentRegisteredInstanceName)
}
this.store.set({
2018-01-13 07:26:20 +01:00
instanceNameInSearch: '',
loggedInInstances: loggedInInstances,
currentInstance: currentRegisteredInstanceName,
loggedInInstancesInOrder: loggedInInstancesInOrder
})
this.store.save()
goto('/')
},
2018-01-09 02:23:33 +01:00
onSubmit: async function(event) {
2018-01-08 01:00:42 +01:00
event.preventDefault()
let instanceName = this.store.get('instanceNameInSearch')
2018-01-08 07:00:16 +01:00
instanceName = instanceName.replace(/^https?:\/\//, '').replace('/$', '')
2018-01-13 07:24:54 +01:00
let instanceData = await (await registerApplication(instanceName)).json()
// TODO: handle error
this.store.set({
currentRegisteredInstanceName: instanceName,
currentRegisteredInstance: instanceData
})
this.store.save()
2018-01-13 07:24:54 +01:00
let oauthUrl = generateAuthLink(instanceName, instanceData.client_id)
2018-01-08 07:00:16 +01:00
document.location.href = oauthUrl
},
2018-01-08 01:00:42 +01:00
}
}
</script>