Created
July 30, 2018 09:56
-
-
Save 3cL1p5e7/3a6d212a7d8d4e51406a6e79533a8155 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
camelCasePropNamesFormatter | |
} from 'services/utils'; | |
class GoogleApi { | |
static errorMessages = [ | |
{ | |
message: 'popup_closed_by_user', | |
isError: false | |
} | |
]; | |
gapi = null; | |
constructor() { | |
this.loadScript(`${GOOGLE_PLATFORM_RESOURCE}`, ::this.loadAndIntializeGapi); | |
} | |
loadScript(src, callback = () => {}) { | |
const script = document.createElement('script'); | |
if (script.readyState) { //IE | |
script.onreadystatechange = () => { | |
if (script.readyState === 'loaded' | |
|| script.readyState === 'complete') { | |
script.onreadystatechange = null; | |
callback(); | |
} | |
}; | |
} else { | |
script.onload = callback; | |
} | |
script.src = src; | |
document.head.appendChild(script); | |
} | |
loadAndIntializeGapi() { | |
const gapi = this.gapi = window.gapi; | |
gapi.load('auth2', () => { | |
gapi.auth2.init({ | |
client_id: `${GOOGLE_API_KEY}`, | |
fetch_basic_profile: false, | |
scope: 'profile email openid', | |
hosted_domain: `${DOMAIN}`, | |
prompt: 'select_account' | |
}); | |
}); | |
} | |
login() { | |
try { | |
return this.signIn(); | |
} catch(error) { | |
return {error}; | |
} | |
} | |
signIn() { | |
const gapi = this.gapi; | |
if (!gapi || !gapi.auth2.getAuthInstance()) { | |
throw 'No google api'; | |
} | |
const auth2 = gapi.auth2.getAuthInstance(); | |
return auth2.signIn({}) | |
.then(userData => { | |
const error = !userData.Zi ? 'No data received from Google API' : null; | |
const user = camelCasePropNamesFormatter(userData.Zi || {}); | |
return {error, user}; | |
}) | |
.catch(::this.resolveError); | |
} | |
resolveError(responce) { | |
const errorMessage = responce.error; | |
const findedMessage = GoogleApi.errorMessages | |
.find(err => err.message === errorMessage); | |
let resolvedError = errorMessage; | |
if (findedMessage) { | |
resolvedError = findedMessage.isError ? errorMessage : null; | |
} | |
return {error: resolvedError}; | |
} | |
} | |
export default new GoogleApi(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const isEmailValid = (email) => { | |
const regex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | |
return regex.test(email); | |
}; | |
export const toCamelCaseFormatter = snake => { | |
return snake.replace(/_([a-z])/g, g => g[1].toUpperCase()); | |
} | |
const propNamesWalker = (item, recursive, formatter) => { | |
if (typeof item !== 'object') { | |
return item; | |
} | |
let newItem = Array.isArray(item) ? [] : {}; | |
Object.keys(item).map(key => { | |
let value = item[key]; | |
if (recursive && Array.isArray(value)) { | |
value = value.map(item => propNamesWalker(item, recursive, formatter)); | |
} else if (recursive && value !== null && typeof value === 'object') { | |
value = propNamesWalker(value, recursive, formatter); | |
} | |
newItem[formatter(key)] = value; | |
}); | |
return newItem; | |
}; | |
export const camelCasePropNamesFormatter = (item, recursive = false) => | |
propNamesWalker(item, recursive, toCamelCaseFormatter); | |
export const snakeCasePropNamesFormatter = (item, recursive) => | |
propNamesWalker(item, recursive, snakeCase); | |
export const capitalizeString = string => { | |
return string.charAt(0).toUpperCase() + string.slice(1); | |
}; | |
export const getTextShapes = ({text, style = ''}) => { | |
let element = document.createElement('div'); | |
element.innerHTML = text; | |
element.style.position = 'absolute'; | |
element.style.left = -1000; | |
element.style.top = -1000; | |
element.style = style; | |
document.body.appendChild(element); | |
const height = element.clientHeight; | |
const width = element.clientWidth; | |
document.body.removeChild(element); | |
element = null; | |
return {height, width}; | |
}; | |
export const getMouthParentalCase = (value) => { | |
return value.format('D MMMM').split(' ')[1]; | |
}; | |
export const caseByCount = (number, titles) => { | |
const cases = [2, 0, 1, 1, 1, 2]; | |
return titles[(number % 100 > 4 && number % 100 < 20) ? 2 : cases[(number % 10 < 5) ? number % 10 : 5]]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment