Last active
March 19, 2018 08:40
-
-
Save divyanshu013/01526809b528e4a0d3311d882f2a9b04 to your computer and use it in GitHub Desktop.
APIs to handle CUD operations in the todos native auth app
This file contains hidden or 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 CONFIG from './../constants/Config'; | |
const getHeaders = (accessToken) => ({ | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${accessToken}` | |
}); | |
const logger = message => console.log(message); | |
class TodoModel { | |
add(todo, screenProps) { | |
if (!screenProps.accessToken) { | |
console.log('Access Token not present'); | |
return; | |
} | |
const body = JSON.stringify({ | |
title: todo.title, | |
name: screenProps.name, | |
avatar: screenProps.avatar | |
}); | |
fetch(CONFIG.server, { | |
method: 'POST', | |
headers: getHeaders(screenProps.accessToken), | |
body | |
}) | |
.then(res => res.json()) | |
.then(logger) | |
.catch(logger); | |
} | |
update = (todo, screenProps) => { | |
if (!screenProps.accessToken) { | |
console.log('Access Token not present'); | |
return; | |
} | |
const body = JSON.stringify({ | |
completed: todo.completed, | |
id: todo._id | |
}); | |
fetch(CONFIG.server, { | |
method: 'PUT', | |
headers: getHeaders(screenProps.accessToken), | |
body | |
}) | |
.then(res => res.json()) | |
.then(logger) | |
.catch(logger); | |
}; | |
destroy = (todo, screenProps) => { | |
if (!screenProps.accessToken) { | |
console.log('Access Token not present'); | |
return; | |
} | |
const body = JSON.stringify({ | |
id: todo._id | |
}); | |
fetch(CONFIG.server, { | |
method: 'DELETE', | |
headers: getHeaders(screenProps.accessToken), | |
body | |
}) | |
.then(res => res.json()) | |
.then(logger) | |
.catch(logger); | |
}; | |
} | |
export default TodoModel; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment