-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0175c3
commit a543fa8
Showing
6 changed files
with
114 additions
and
69 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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 |
This file contains 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
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 }))) | ||
}) | ||
} |