From a543fa8d9d8bfb5811220fdb599e263b9f75b719 Mon Sep 17 00:00:00 2001 From: Bu Kinoshita Date: Fri, 19 Jan 2018 16:45:41 -0200 Subject: [PATCH] api && local storage --- renderer/pages/add.js | 2 +- renderer/pages/edit.js | 2 +- renderer/pages/home.js | 2 +- renderer/pages/settings.js | 2 +- renderer/services/api.js | 95 ++++++++++-------------------- renderer/services/local-storage.js | 80 +++++++++++++++++++++++++ 6 files changed, 114 insertions(+), 69 deletions(-) create mode 100644 renderer/services/local-storage.js diff --git a/renderer/pages/add.js b/renderer/pages/add.js index 60a5e3a..b6d5fa0 100644 --- a/renderer/pages/add.js +++ b/renderer/pages/add.js @@ -15,7 +15,7 @@ import Button from './../components/button' import ButtonLink from './../components/button-link' // Services -import { addTask, getUser, updateUser } from './../services/api' +import { addTask, getUser, updateUser } from './../services/local-storage' // Theme import { colors, typography } from './../theme' diff --git a/renderer/pages/edit.js b/renderer/pages/edit.js index 5e74fe5..3648611 100644 --- a/renderer/pages/edit.js +++ b/renderer/pages/edit.js @@ -15,7 +15,7 @@ import Button from './../components/button' import ButtonLink from './../components/button-link' // Services -import { getUser, updateTask } from './../services/api' +import { getUser, updateTask } from './../services/local-storage' // Theme import { colors, typography } from './../theme' diff --git a/renderer/pages/home.js b/renderer/pages/home.js index cdc204a..99d1077 100644 --- a/renderer/pages/home.js +++ b/renderer/pages/home.js @@ -17,7 +17,7 @@ import Backlog from './../components/home/backlog' import Done from './../components/home/done' // Services -import { getUser, updateUser } from './../services/api' +import { getUser, updateUser } from './../services/local-storage' class Home extends Component { constructor() { diff --git a/renderer/pages/settings.js b/renderer/pages/settings.js index 0a2388f..f87be35 100644 --- a/renderer/pages/settings.js +++ b/renderer/pages/settings.js @@ -18,7 +18,7 @@ import Social from './../components/settings/social' import Content from './../components/content' // Sertvices -import { getUser, updateUser } from './../services/api' +import { getUser, updateUser } from './../services/local-storage' import { exportUser, importUser, clearHistory } from './../services/settings' class Settings extends Component { diff --git a/renderer/services/api.js b/renderer/services/api.js index 38999d9..e6d1ea8 100644 --- a/renderer/services/api.js +++ b/renderer/services/api.js @@ -1,79 +1,44 @@ 'use strict' -// Packages -import uid from 'uid-promise' +import axios from 'axios' -export const getUser = () => { - const storage = JSON.parse(localStorage.getItem('taskr')) +import { getCookie } from './cookies' - if (storage) { - return storage - } - - const cfg = { - user: { - tasks: [], - createOn: 'Today' - } - } +const apiUrl = process.env.API_URL - localStorage.setItem('taskr', JSON.stringify(cfg)) +const api = axios.create({ + baseURL: apiUrl, + headers: { + Accept: 'application/json' + }, + withCredentials: true +}) - return cfg -} +api.interceptors.request.use(config => { + const accessToken = getCookie('taskr') -export const updateUser = user => { - return localStorage.setItem('taskr', JSON.stringify({ user })) -} + if (accessToken) { + config.headers.authorization = accessToken + } -export const addTask = ({ title, description, project, tab = 'Today' }) => { - return new Promise(async (resolve, reject) => { - if (!title) { - return reject(new TypeError('title is required')) - } + return config +}) - const { user } = getUser() - const id = await uid(20) - const task = { - id, - title, - description, - project, - createdAt: new Date(), - updatedAt: new Date(), - type: tab.toLowerCase() +api.interceptors.response.use( + response => { + if (response.data) { + return response.data } - const tasks = [...user.tasks, task] - user.tasks = tasks - resolve(localStorage.setItem('taskr', JSON.stringify({ user }))) - }) -} - -export const updateTask = ({ id, newTask }) => { - return new Promise(async (resolve, reject) => { - if (!newTask && !id) { - return reject(new TypeError('id and task is required')) + return response + }, + error => { + if (error.response && error.response.data) { + return Promise.reject(error.response.data.error) } - const { user } = getUser() - const task = user.tasks.filter(task => task.id === id)[0] - - task.title = newTask.title || task.title - task.description = newTask.description || task.description - task.project = newTask.project || task.project - task.updatedAt = new Date() - - const tasks = user.tasks.filter(t => { - if (t.id === id) { - return (t = task) - } - - return t - }) - - user.tasks = tasks + return Promise.reject(error) + } +) - resolve(localStorage.setItem('taskr', JSON.stringify({ user }))) - }) -} +export default api diff --git a/renderer/services/local-storage.js b/renderer/services/local-storage.js new file mode 100644 index 0000000..822d5d2 --- /dev/null +++ b/renderer/services/local-storage.js @@ -0,0 +1,80 @@ +'use strict' + +// Packages +import uid from 'uid-promise' + +export const getUser = () => { + const storage = JSON.parse(localStorage.getItem('taskr')) + + if (storage) { + return storage + } + + const cfg = { + user: { + tasks: [], + createOn: 'Today', + onboard: false + } + } + + localStorage.setItem('taskr', JSON.stringify(cfg)) + + return cfg +} + +export const updateUser = user => { + return localStorage.setItem('taskr', JSON.stringify({ user })) +} + +export const addTask = ({ title, description, project, tab = 'Today' }) => { + return new Promise(async (resolve, reject) => { + if (!title) { + return reject(new TypeError('title is required')) + } + + const { user } = getUser() + const id = await uid(20) + const task = { + id, + title, + description, + project, + createdAt: new Date(), + updatedAt: new Date(), + type: tab.toLowerCase() + } + const tasks = [...user.tasks, task] + user.tasks = tasks + + resolve(localStorage.setItem('taskr', JSON.stringify({ user }))) + }) +} + +export const updateTask = ({ id, newTask }) => { + return new Promise(async (resolve, reject) => { + if (!newTask && !id) { + return reject(new TypeError('id and task is required')) + } + + const { user } = getUser() + const task = user.tasks.filter(task => task.id === id)[0] + + task.title = newTask.title || task.title + task.description = newTask.description || task.description + task.project = newTask.project || task.project + task.updatedAt = new Date() + + const tasks = user.tasks.filter(t => { + if (t.id === id) { + return (t = task) + } + + return t + }) + + user.tasks = tasks + + resolve(localStorage.setItem('taskr', JSON.stringify({ user }))) + }) +}