Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose register method to manually subscribe new user #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,21 @@ const PerfectyPush = (() => {
const askPermissionsDirectly = async () => {
if (Permission.askedAlready()) {
// if we already have asked for permissions, we don't ask again
return true
return Permission.isGranted()
}

await Permission.askIfNotDenied()
if (Permission.isGranted()) {
Logger.info('User has granted permissions')

const userId = Storage.userId()
await ServiceInstaller.installIfMissing()
await Registration.register(userId, true)
if (!Permission.isGranted()) {
return false
}

Logger.info('User has granted permissions')

const userId = Storage.userId()
await ServiceInstaller.installIfMissing()
await Registration.register(userId, true)

return true
}

const checkInstallation = async () => {
Expand All @@ -112,8 +116,35 @@ const PerfectyPush = (() => {
await Registration.check(Storage.userId(), Storage.optedOut())
}

const register = async () => {
Options.askPermissionsDirectly = true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not alter user options but instead adjust the existing code to support explicit registrations called from JS. I will refactor the current code in Perfecty in order to support what you intend to do in a separate PR.

Storage.setOptedOut(false)

if (!Permission.isGranted()) {
return askPermissionsDirectly()
}

const userId = Storage.userId()
await ServiceInstaller.installIfMissing()
return Registration.register(userId, true)
}

const unregister = async () => {
const userId = Storage.userId()
await Registration.unregister(userId)
Storage.setOptedOut(true)
await ServiceInstaller.removeInstallation()
}

const isRegister = () => {
return Permission.isGranted() && Storage.optedOut() === false
}

return {
start
start,
register,
unregister,
isRegister
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isRegistered maybe?

}
})()

Expand Down
11 changes: 9 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ const ESLintPlugin = require('eslint-webpack-plugin')

module.exports = {
entry: {
'perfecty-push-sdk': './src/app.js',
'perfecty-push-sw': './src/service-worker.js'
'perfecty-push-sdk': {
import: './src/app.js',
library: {
name: 'PerfectyPush',
type: 'var',
export: 'default'
}
},
'perfecty-push-sw': './src/service-worker.js',
},
mode: 'development',
devtool: 'source-map',
Expand Down
9 changes: 8 additions & 1 deletion webpack.prod.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ const path = require('path')

module.exports = {
entry: {
'perfecty-push-sdk': './src/app.js',
'perfecty-push-sdk': {
import: './src/app.js',
library: {
name: 'PerfectyPush',
type: 'var',
export: 'default'
}
},
'perfecty-push-sw': './src/service-worker.js'
},
mode: 'production',
Expand Down