feat(chart): add chart implementation

This commit is contained in:
2021-12-23 00:29:13 +01:00
parent 566e75f99a
commit 2556908042
13 changed files with 261 additions and 17 deletions

26
src/service/expenses.js Normal file
View File

@@ -0,0 +1,26 @@
export const formatExpenses = (expenses) => {
const formattedExpenses = []
const reduceExpenses = []
for (const expense of expenses) {
if (reduceExpenses[expense.expenseDate] === undefined) {
reduceExpenses[expense.expenseDate] = expense.value
} else {
reduceExpenses[expense.expenseDate] += expense.value
}
}
for (const date in reduceExpenses) {
formattedExpenses[formattedExpenses.length] = { x: date, y: reduceExpenses[date] }
}
console.log(reduceExpenses)
console.log(formattedExpenses)
return {
datasets: [{
label: 'My First Dataset',
data: formattedExpenses,
fill: false,
borderColor: 'rgb(75, 192, 192)',
tension: 0.1
}]
}
}

147
src/service/google_oAuth.js Normal file
View File

@@ -0,0 +1,147 @@
var googleAuth = (function () {
function installClient () {
var apiUrl = 'https://apis.google.com/js/api.js'
return new Promise((resolve) => {
var script = document.createElement('script')
script.src = apiUrl
script.onreadystatechange = script.onload = function () {
if (!script.readyState || /loaded|complete/.test(script.readyState)) {
setTimeout(function () {
resolve()
}, 500)
}
}
document.getElementsByTagName('head')[0].appendChild(script)
})
}
function initClient (config) {
return new Promise((resolve) => {
window.gapi.load('auth2', () => {
window.gapi.auth2.init(config)
.then(() => {
resolve(window.gapi)
})
})
})
}
function Auth () {
if (!(this instanceof Auth))
return new Auth()
this.GoogleAuth = null /* window.gapi.auth2.getAuthInstance() */
this.isAuthorized = false
this.isInit = false
this.prompt = null
this.isLoaded = function () {
/* eslint-disable */
console.warn('isLoaded() will be deprecated. You can use "this.$gAuth.isInit"')
return !!this.GoogleAuth
}
this.currentUser = () => {
return this.GoogleAuth.currentUser;
}
this.load = (config, prompt) => {
installClient()
.then(() => {
return initClient(config)
})
.then((gapi) => {
this.GoogleAuth = gapi.auth2.getAuthInstance()
this.isInit = true
this.prompt = prompt
this.isAuthorized = this.GoogleAuth.isSignedIn.get()
})
}
this.signIn = (successCallback, errorCallback) => {
return new Promise((resolve, reject) => {
if (!this.GoogleAuth) {
if (typeof errorCallback === 'function') errorCallback(false)
reject(false)
return
}
this.GoogleAuth.signIn()
.then(googleUser => {
if (typeof successCallback === 'function') successCallback(googleUser)
this.isAuthorized = this.GoogleAuth.isSignedIn.get()
resolve(googleUser)
})
.catch(error => {
if (typeof errorCallback === 'function') errorCallback(error)
reject(error)
})
})
}
this.getAuthCode = (successCallback, errorCallback) => {
return new Promise((resolve, reject) => {
if (!this.GoogleAuth) {
if (typeof errorCallback === 'function') errorCallback(false)
reject(false)
return
}
this.GoogleAuth.grantOfflineAccess({ prompt: this.prompt })
.then(function (resp) {
if (typeof successCallback === 'function') successCallback(resp.code)
resolve(resp.code)
})
.catch(function (error) {
if (typeof errorCallback === 'function') errorCallback(error)
reject(error)
})
})
}
this.signOut = (successCallback, errorCallback) => {
return new Promise((resolve, reject) => {
if (!this.GoogleAuth) {
if (typeof errorCallback === 'function') errorCallback(false)
reject(false)
return
}
this.GoogleAuth.signOut()
.then(() => {
if (typeof successCallback === 'function') successCallback()
this.isAuthorized = false
resolve(true)
})
.catch(error => {
if (typeof errorCallback === 'function') errorCallback(error)
reject(error)
})
})
}
}
return new Auth()
})()
function installGoogleAuthPlugin(Vue, options) {
//set config
let GoogleAuthConfig = null
let GoogleAuthDefaultConfig = { scope: 'profile email', discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest'] }
let prompt = 'select_account'
if (typeof options === 'object') {
GoogleAuthConfig = Object.assign(GoogleAuthDefaultConfig, options)
if (options.scope) GoogleAuthConfig.scope = options.scope
if (options.prompt) prompt = options.prompt
if (!options.clientId) {
console.warn('clientId is required')
}
} else {
console.warn('invalid option type. Object type accepted only')
}
//Install Vue plugin
Vue.gAuth = googleAuth
Object.defineProperties(Vue.prototype, {
$gAuth: {
get: function () {
return Vue.gAuth
}
}
})
Vue.gAuth.load(GoogleAuthConfig, prompt)
}
export default installGoogleAuthPlugin

71
src/service/noscomptes.js Normal file
View File

@@ -0,0 +1,71 @@
import Vue from 'vue'
export const logWithGoogle = (oauthToken) => {
const headers = {
"Authorization": "Bearer "+oauthToken
};
return Vue.axios.get("http://localhost:8081/users",{headers}).then(response => {return response.data})
}
export const getAccounts = (oauthToken, userId) => {
const headers = {
"Authorization": "Bearer "+oauthToken
};
return Vue.axios.get("http://localhost:8081/users/"+userId+"/accounts", {headers}).then(response => {console.log(response);return response.data})
}
export const getSharedAccounts = (oauthToken, userId) => {
const headers = {
"Authorization": "Bearer "+oauthToken
};
return Vue.axios.get("http://localhost:8081/users/"+userId+"/sharedaccounts", {headers}).then(response => {console.log(response);return response.data})
}
export const createAccount = (oauthToken, userId, account) => {
const headers = {
"Authorization": "Bearer " + oauthToken
};
return Vue.axios.post("http://localhost:8081/users/"+userId+"/accounts", account, {headers}).then(response => {
return response.data
})
}
export const deleteAnAccount = (oauthToken, userId, accountId) => {
const headers = {
"Authorization": "Bearer " + oauthToken
};
return Vue.axios.delete("http://localhost:8081/users/"+userId+"/accounts/"+accountId, {headers}).then(response => {
return response.data
})
}
export const getAnAccount = (oauthToken, userId, accountId) => {
const headers = {
"Authorization": "Bearer " + oauthToken
};
return Vue.axios.get("http://localhost:8081/users/"+userId+"/accounts/"+accountId, {headers}).then(response => {
return response.data
})
}
export const getExpenses = (oauthToken, userId, accountId) => {
const headers = {
"Authorization": "Bearer " + oauthToken
};
return Vue.axios.get("http://localhost:8081/users/"+userId+"/accounts/"+accountId+"/expenses", {headers}).then(response => {
return response.data
})
}
export const sendCSVImportExpenses = (oauthToken, userId, accountId, file) => {
const headers = {
"Authorization": "Bearer " + oauthToken,
'Content-Type': 'multipart/form-data'
};
let fileForm = new FormData()
fileForm.set('attachment',file)
return Vue.axios.post("http://localhost:8081/users/"+userId+"/accounts/"+accountId+"/expenses", fileForm, {headers}).then(response => {
return response.data
})
}

25
src/service/utils.js Normal file
View File

@@ -0,0 +1,25 @@
/**
* Set localStorage
*/
export const setStore = (name, content) => {
if (!name) return
if (typeof content !== 'string') {
content = JSON.stringify(content)
}
return window.localStorage.setItem(name, content)
}
/**
* Get localStorage
*/
export const getStore = (name) => {
if (!name) return
return JSON.parse(window.localStorage.getItem(name))
}
/**
* Clear localStorage
*/
export const removeItem = (name) => {
console.log(name)
if (!name) return
return window.localStorage.removeItem(name)
}