Feat: Tag rows before storing them in vuex

This commit is contained in:
Bertrand Benjamin 2018-12-03 09:49:36 +01:00
parent 3e36c2a444
commit f112e8dc9d
3 changed files with 51 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 { readdir, readFile } from 'fs'
import path from 'path' import path from 'path'
import Papa from 'papaparse' import Papa from 'papaparse'
import { appendTag } from '../../libs/data_processing'
var today = new Date() var today = new Date()
var monthAgo = new Date() var monthAgo = new Date()
@ -66,7 +67,6 @@ export default {
state.csv_files = csvs state.csv_files = csvs
}, },
SET_DATA: (state, { data }) => { SET_DATA: (state, { data }) => {
data.data = data.data.filter(x => x.Libellé !== undefined)
state.rows = data state.rows = data
}, },
APPEND_DATA: (state, { content }) => { APPEND_DATA: (state, { content }) => {
@ -108,10 +108,19 @@ export default {
header: true header: true
} }
var parsed = Papa.parse(content, parseConfig) 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) { set_start (context, start) {
context.commit('SET_START', { start }) context.commit('SET_START', { start })
}, },

View File

@ -21,7 +21,15 @@
<box @click.native="set_poste('other')" postename="other"></box> <box @click.native="set_poste('other')" postename="other"></box>
</b-card-group> </b-card-group>
<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> </div>
</template> </template>
@ -49,6 +57,10 @@ export default {
{ {
key: 'Libellé', key: 'Libellé',
sortable: true sortable: true
},
{
key: 'tags',
sortable: true
} }
], ],
poste: {} poste: {}