Skip to content

Instantly share code, notes, and snippets.

@divyanshu013
Last active March 19, 2018 08:40
Show Gist options
  • Save divyanshu013/01526809b528e4a0d3311d882f2a9b04 to your computer and use it in GitHub Desktop.
Save divyanshu013/01526809b528e4a0d3311d882f2a9b04 to your computer and use it in GitHub Desktop.
APIs to handle CUD operations in the todos native auth app
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