Feat: Tag rows before storing them in vuex

This commit is contained in:
Bertrand Benjamin 2018-12-03 09:49:36 +01:00
parent 4c7523dbb8
commit 697435de30
3 changed files with 52 additions and 3 deletions

View File

@ -0,0 +1,27 @@
export function appendTag (row, keywords, field='Libellé') {
// Append row.tag
// if row.libellé contains one of words and not invert it gets tagged
// if row.libellé contains no words and invert it gets tagged
// according to keywords [{name: string, words: [], invert: bool}]
// row['tags'] =
row.tags = keywords.filter(k => {
return strContains(row[field], k.words, k.invert)
})
}
function strContains (string, words, invert) {
// Does a string contain one of words or the opposite
if (!words) {
return true
}
if (invert) {
return words.every(v => {
return string.indexOf(v) < 0
})
} else {
return words.some(v => {
return string.indexOf(v) >= 0
})
}
}

View File

@ -1,6 +1,7 @@
import { readdir, readFile } from 'fs'
import path from 'path'
import Papa from 'papaparse'
import { appendTag } from '../../libs/data_processing'
var today = new Date()
var monthAgo = new Date()
@ -66,7 +67,6 @@ export default {
state.csv_files = csvs
},
SET_DATA: (state, { data }) => {
data.data = data.data.filter(x => x.Libellé !== undefined)
state.rows = data
},
APPEND_DATA: (state, { content }) => {
@ -108,10 +108,19 @@ export default {
header: true
}
var parsed = Papa.parse(content, parseConfig)
context.commit('SET_DATA', { data: parsed })
context.dispatch('clean_store_data', parsed)
}
})
},
clean_store_data (context, parsed) {
var postes = Object.values(context.rootGetters['config/postes'])
parsed.data = parsed.data.filter(x => x.Libellé !== undefined)
parsed.data.forEach(row => {
appendTag(row, postes, 'Libellé')
})
context.commit('SET_DATA', { data: parsed })
},
set_start (context, start) {
context.commit('SET_START', { start })
},

View File

@ -23,7 +23,16 @@
<postes-comparison></postes-comparison>
<b-table striped hover :items="filtered_rows(poste.words, poste.invert)" :fields='fields'></b-table>
<b-table striped hover :items="filtered_rows(poste.words, poste.invert)" :fields='fields'>
<template slot="tags" slot-scope="data">
<div v-for="tag in data.item.tags">
<div v-if="tag.name !== 'Tout'">
<font-awesome-icon :icon="tag.icon" class="fa"/>
</div>
</div>
</template>
</b-table>
</div>
</template>
@ -53,6 +62,10 @@ export default {
{
key: 'Libellé',
sortable: true
},
{
key: 'tags',
sortable: true
}
],
poste: {}