Skip to content

Instantly share code, notes, and snippets.

@kaueDM
Created September 13, 2017 12:33
Show Gist options
  • Save kaueDM/995c5fc0d4296c168c17e0487648a133 to your computer and use it in GitHub Desktop.
Save kaueDM/995c5fc0d4296c168c17e0487648a133 to your computer and use it in GitHub Desktop.
export const checkOpenTasks = (uid) => (dispatch) =>
new Promise((resolve, reject) => {
AsyncStorage.getItem(uid).then(item => {
if (item) {
const moments = Object.keys(JSON.parse(item));
const last = Math.max.apply(null, moments); //Último moment mesclado ao objeto
const { action, initial, elapsed } = JSON.parse(item)[last] //Ação realizada pelo último moment mesclado
const now = moment.utc().valueOf(); //Moment presente
const extra = (now - last) - ((now - last) % 1000); //Arredondamento - detalhes no README.md
resolve({ action, initial, elapsed, extra })
} else {
reject(new Error('Nenhuma atividade em aberto'))
}
})
})
@clucasalcantara
Copy link

clucasalcantara commented Sep 13, 2017

export const checkOpenTasks = (uid) => async (dispatch) => {
    await AsyncStorage.getItem(uid).then(item => {
        if (item) {
            const moments = Object.keys(JSON.parse(item));
            const last = Math.max.apply(null, moments); //Último moment mesclado ao objeto
            const { action, initial, elapsed } = JSON.parse(item)[last] //Ação realizada pelo último moment mesclado
            const now = moment.utc().valueOf(); //Moment presente
            const extra = (now - last) - ((now - last) % 1000); //Arredondamento - detalhes no README.md
            
            return { action, initial, elapsed, extra }
        }

        throw new Error('Nenhuma atividade em aberto')
    })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment