Skip to content

Commit

Permalink
api && local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
bukinoshita committed Feb 13, 2018
1 parent f0175c3 commit a543fa8
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 69 deletions.
2 changes: 1 addition & 1 deletion renderer/pages/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion renderer/pages/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion renderer/pages/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion renderer/pages/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
95 changes: 30 additions & 65 deletions renderer/services/api.js
Original file line number Diff line number Diff line change
@@ -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
80 changes: 80 additions & 0 deletions renderer/services/local-storage.js
Original file line number Diff line number Diff line change
@@ -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 })))
})
}

0 comments on commit a543fa8

Please sign in to comment.