Skip to content

Commit

Permalink
Expose register method to subscribe new user
Browse files Browse the repository at this point in the history
  • Loading branch information
nico2che committed Jan 28, 2022
1 parent 6b97bbb commit 57e8ec2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
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
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
}
})()

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

0 comments on commit 57e8ec2

Please sign in to comment.