Skip to content

Commit

Permalink
feat(Migration): attach lastGuid and session to state from transfer_s…
Browse files Browse the repository at this point in the history
…tored_values
  • Loading branch information
plondon committed Jul 10, 2018
1 parent 067dd63 commit 09aaacf
Showing 1 changed file with 51 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,57 +1,76 @@
import { put, select } from 'redux-saga/effects'
import { call, put, select, take } from 'redux-saga/effects'
import { eventChannel, END } from 'redux-saga'
import { actions, selectors } from 'data'
import { assoc } from 'ramda'

// TODO:: DEPRECATE
// We should not need this in the future. It is used to handle transferring cookies from .info to .com domain

export default () => {
// TODO:: DEPRECATE
// We should not need this but it is used to handle transferring cookies from .info to .com domain
// Consider removing this code in the future
const initialized = function * () {
try {
const alreadyTransferredCookiesKey = 'did_already_transfer_cookies_from_dot_info'
const shouldTransfer = window.localStorage.getItem(alreadyTransferredCookiesKey) == null

const createFrame = (src) => {
const frame = document.createElement('iframe')
frame.src = src
frame.style.display = 'none'
document.body.appendChild(frame)
return frame
}

const tryJsonParse = (value) => {
try {
return JSON.parse(value)
} catch (e) {
return value
}
}

if (shouldTransfer) {
const domainsR = yield select(selectors.core.walletOptions.getDomains)
const domains = domainsR.getOrElse({ root: 'https://blockchain.info' })
const rootURL = domains.root
const frame = createFrame(`${rootURL}/Resources/transfer_stored_values.html`)

window.addEventListener('message', (event) => {
if (event.data.type === 'cookie') {
const cookie = event.data.payload
window.localStorage.setItem(cookie.id, tryJsonParse(cookie.data))
}
const windowChannel = function () {
return eventChannel((emitter) => {
window.addEventListener('message', function (event) {
if (event.data.type === 'cookie') {
const cookie = event.data.payload
window.localStorage.setItem(cookie.id, tryJsonParse(cookie.data))
}

if (event.data.type === 'cookies-sent') {
frame.remove()
const lastGuid = window.localStorage.getItem('ls.guid')
const session = window.localStorage.getItem('ls.session')
window.localStorage.setItem(alreadyTransferredCookiesKey, true)
emitter({ lastGuid, session })
emitter(END)
}
})

return () => {}
})
}

const chan = yield call(windowChannel)

if (event.data.type === 'cookies-sent') {
frame.remove()
window.localStorage.setItem(alreadyTransferredCookiesKey, true)
const lastGuid = window.localStorage.getItem('ls.guid')
console.log(lastGuid)
// yield put(actions.cache.guidEntered(lastGuid))
try {
while (true) {
let { session, lastGuid } = yield take(chan)
yield put(actions.cache.guidEntered(lastGuid))
yield put(actions.session.saveSession(assoc(lastGuid, session, {})))
}
})
} finally {}
}
} catch (e) {
yield put(actions.logs.logErrorMessage('components/login/sagas', 'initialized', e))
}
}

const createFrame = (src) => {
const frame = document.createElement('iframe')
frame.src = src
frame.style.display = 'none'
document.body.appendChild(frame)
return frame
}

const tryJsonParse = (value) => {
try {
return JSON.parse(value)
} catch (e) {
return value
}
}

return {
initialized
}
Expand Down

0 comments on commit 09aaacf

Please sign in to comment.