Pinafore-Web-Client-Frontend/src/routes/_components/settings/instance/PushNotificationSettings.html

121 lines
3.6 KiB
HTML
Raw Normal View History

2018-10-28 23:28:22 +01:00
<div class="push-notifications">
{#if pushNotificationsSupport === false}
<p>Your browser doesn't support push notifications.</p>
2018-10-28 23:28:22 +01:00
{:elseif $notificationPermission === "denied"}
<p role="alert">You have denied permission to show notifications.</p>
{:elseif $loggedInInstancesInOrder.length > 1}
<p>Note that you can only have push notifications for one instance at a time.</p>
2018-10-28 23:28:22 +01:00
{/if}
<form id="push-notification-settings"
disabled="{!pushNotificationsSupport}"
ref:form
aria-label="Push notification settings">
{#each options as option, i (option.key)}
{#if i > 0}
<br>
{/if}
<input type="checkbox"
id="push-notifications-{option.key}"
name="{option.key}"
disabled="{!pushNotificationsSupport}"
on:change="onPushSettingsChange(event)">
<label for="push-notifications-{option.key}">{option.label}</label>
{/each}
2018-10-28 23:28:22 +01:00
</form>
</div>
<style>
.push-notifications {
background: var(--form-bg);
border: 1px solid var(--main-border);
border-radius: 4px;
display: block;
padding: 20px;
line-height: 2em;
}
form[disabled="true"] {
2018-10-28 23:28:22 +01:00
opacity: 0.5;
}
p {
margin: 0 0 10px 0;
2018-10-28 23:28:22 +01:00
}
</style>
<script>
import { store } from '../../../_store/store'
import { importShowTextConfirmationDialog } from '../../dialog/asyncDialogs/importShowTextConfirmationDialog.js'
2018-10-28 23:28:22 +01:00
import { logOutOfInstance } from '../../../_actions/instances'
import { updatePushSubscriptionForInstance, updateAlerts } from '../../../_actions/pushSubscription'
2018-12-23 00:37:51 +01:00
import { toast } from '../../toast/toast'
import { get } from '../../../_utils/lodash-lite'
2018-10-28 23:28:22 +01:00
export default {
async oncreate () {
2019-08-03 22:49:37 +02:00
const { instanceName, options } = this.get()
2018-10-28 23:28:22 +01:00
await updatePushSubscriptionForInstance(instanceName)
const { form } = this.refs
const pushSubscription = this.store.getInstanceData(instanceName, 'pushSubscriptions')
2018-10-28 23:28:22 +01:00
2019-08-03 22:49:37 +02:00
for (const { key } of options) {
form.elements[key].checked = get(pushSubscription, ['alerts', key])
}
2018-10-28 23:28:22 +01:00
},
store: () => store,
data: () => ({
options: [
{
key: 'follow',
label: 'New Followers'
},
{
key: 'favourite',
label: 'Favorites'
},
{
key: 'reblog',
label: 'Boosts'
},
{
key: 'mention',
label: 'Mentions'
},
{
key: 'poll',
label: 'Poll results'
}
]
}),
2018-10-28 23:28:22 +01:00
computed: {
pushNotificationsSupport: ({ $pushNotificationsSupport }) => $pushNotificationsSupport
},
methods: {
async onPushSettingsChange (e) {
const { instanceName, options } = this.get()
const { form } = this.refs
const alerts = {}
2019-08-03 22:49:37 +02:00
for (const { key } of options) {
alerts[key] = form.elements[key].checked
2018-10-28 23:28:22 +01:00
}
try {
await updateAlerts(instanceName, alerts)
} catch (err) {
e.target.checked = !e.target.checked
// TODO: Better way to detect missing authorization scope
if (err.message.startsWith('403:')) {
2019-08-03 22:49:37 +02:00
const showTextConfirmationDialog = await importShowTextConfirmationDialog()
showTextConfirmationDialog({
text: `You need to reauthenticate in order to enable push notification. Log out of ${instanceName}?`
}).on('positive', () => {
/* no await */ logOutOfInstance(instanceName)
2018-10-28 23:28:22 +01:00
})
} else {
toast.say(`Failed to update push notification settings: ${err.message}`)
}
}
}
}
}
2018-12-23 00:37:51 +01:00
</script>