Skip to content

Commit

Permalink
Merge pull request #10 from ddgll/dev-bugfix
Browse files Browse the repository at this point in the history
BUGFIX
  • Loading branch information
ddgll authored Aug 17, 2017
2 parents 690df9d + 9e92a9e commit 3b18cec
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 18 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ IndexedDB wrapper for Vuejs based on Dexie
})
```

# 0.1.4 Enhancement
BUGFIX on listSelect vuex action
BUGFIX on toggleSelect vuex action

# 0.1.3 Enhancement
Add Dexie DB version in options

Expand Down
9 changes: 5 additions & 4 deletions dev/list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<option v-for="item in selection" :value="item.value">{{ item.label }}</option>
</select>
</li>
<li v-for="test in tests" :class="{'selected': selected === test.id}">
{{ test.title }} <button type="button" @click="remove(test)" class="remove">&times;</button>
<li v-for="test in tests" :class="{'selected': selected === test.id}" @click="testsSelect">
{{ test.title }} <button type="button" @click.stop.prevent="remove(test)" class="remove">&times;</button>
</li>
</ul>
</div>
Expand All @@ -32,8 +32,8 @@
<option value="cd">Création DESC</option>
</select>
</li>
<li v-for="test in testsStore">
{{ test.title }} <button type="button" @click="remove(test)" class="remove">&times;</button>
<li v-for="test in testsStore" @click="testsToggleSelection">
{{ test.title }} <button type="button" @click.stop.prevent="remove(test)" class="remove">&times;</button>
</li>
</ul>
</div>
Expand All @@ -60,6 +60,7 @@ export default {
this.$db.tests.toArray().then( tests => this.tests = tests )
},
methods: {
...mapActions(['testsSelect', 'testsToggleSelection']),
add (value){
console.log('ADD', value)
this.$store.dispatch('testsAdd', { id: uuid(), title: value, created_at: new Date(), updated_at: new Date() } )
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-idb",
"version": "0.1.3",
"version": "0.1.4",
"description": "IndexedDB wrapper for Vuejs based on Dexie",
"main": "dist/index.js",
"author": "David Grill <grilldotcom@gmail.com>",
Expand All @@ -25,22 +25,22 @@
},
"devDependencies": {
"axios": "^0.16.1",
"babel-core": "^6.24.1",
"babel-core": "^6.26.0",
"babel-eslint": "^7.2.3",
"babel-loader": "^7.0.0",
"babel-loader": "^7.1.1",
"babel-plugin-transform-async-to-generator": "^6.24.1",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-polyfill": "^6.23.0",
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"chai": "^3.5.0",
"cross-env": "^5.0.0",
"css-loader": "^0.28.2",
"cross-env": "^5.0.5",
"css-loader": "^0.28.4",
"expect.js": "^0.3.1",
"extract-text-webpack-plugin": "^2.1.0",
"file-loader": "^0.11.1",
"file-loader": "^0.11.2",
"karma": "^1.7.0",
"karma-chrome-launcher": "^2.1.1",
"karma-coverage": "^1.1.1",
Expand All @@ -55,7 +55,7 @@
"raw-loader": "^0.5.1",
"sinon": "^2.3.1",
"sinon-chai": "^2.10.0",
"style-loader": "^0.18.1",
"style-loader": "^0.18.2",
"stylus": "^0.54.5",
"stylus-loader": "^3.0.1",
"uglifyjs-webpack-plugin": "^0.4.3",
Expand Down
195 changes: 195 additions & 0 deletions src/modules/list-module.1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import getTypes from '../types/list-types'
import { arrayMax, jsUcfirst } from '../contants'
import filterBy from '../filter-by'
import orderBy from '../order-by'

export default (name, options, db, api) => {
const types = getTypes(name)

const Name = jsUcfirst(name)
const NAME = name.toUpperCase()

const _id = options.primary ? options.primary : 'id'
const _label = options.label ? options.label : 'label'
const _updated_at = options.updated_at ? options.updated_at : 'updated_at'

const state = {
collection: [],
selection: [],
loading: false,
loaded: false,
selected: null,
filter: {},
sort: _label,
reverse: false,
last: null
}

// getters
const getters = {
[`is${Name}Loading`]: state => state.loading,
[`is${Name}Loaded`]: state => state.loaded,
[`get${Name}Last`]: state => state.last,
[`get${Name}Selection`]: state => state.selection,
[`get${Name}SelectionCollection`]: state => state.collection.filter(entity => state.selection.indexOf(entity[_id]) > -1),
[`get${Name}SelectionCount`]: state => state.selection.length,
[`is${Name}InSelection`]: state => id => (state.selection.indexOf(id) > -1) ? true : false,
[`get${Name}`]: state => orderBy(filterBy(state.collection, state.filter), state.sort, state.reverse),
[`get${Name}Sorted`]: state => orderBy(state.collection, state.sort, state.reverse),
[`get${Name}Filtered`]: state => filterBy(state.collection, state.filter),
[`get${Name}Original`]: state => state.collection,
[`get${Name}Selected`]: state => state.selected !== null ? state.collection[state.selected] : null,
[`get${Name}Count`]: state => (field, value) => field ? state.collection.filter(entity => entity[field] === value).length : state.collection.length,
[`get${Name}Ids`]: state => (field, value) => field ? state.collection.filter(entity => entity[field] === value).map(entity => entity[_id]) : state.collection.map(entity => entity[_id]),
[`get${Name}Selectable`]: state => orderBy(state.collection.map(entity => {
return {
label: entity[_label],
value: entity[_id]
}
}), 'label', false)
}

// actions
const actions = {
[`${name}Select`]({ commit, state }, payload) {
commit(types[`${NAME}_SELECT`], payload)
},
[`${name}Load`]({ commit }){
console.log('LOAD DATA')
commit(types[`${NAME}_LOAD`])
if(api && api.all){
api.all(state.last).then(res => commit(types[`${NAME}_LOAD_SUCCESS`], res.data), err => commit(types[`${NAME}_LOAD_FAIL`], res.data))
}
},
[`${name}Add`]({ commit }, payload){
commit(types[`${NAME}_ADD`], payload)
if(api && api.add){
api.add(payload).then(res => commit(types[`${NAME}_ADD_SUCCESS`], res.data), err => commit(types[`${NAME}_ADD_FAIL`], res.data))
}
},
[`${name}Update`]({ commit }, payload){
commit(types[`${NAME}_UPDATE`], payload)
if(api && api.update){
api.update(payload).then(res => commit(types[`${NAME}_UPDATE_SUCCESS`], res.data), err => commit(types[`${NAME}_UPDATE_FAIL`], res.data))
}
},
[`${name}Remove`]({ commit }, payload){
commit(types[`${NAME}_REMOVE`], payload)
if(api && api.remove){
api.remove(payload).then(res => commit(types[`${NAME}_REMOVE_SUCCESS`], res.data), err => commit(types[`${NAME}_REMOVE_FAIL`], payload))
}
},
[`${name}SetSort`]({ commit }, payload){
console.log('SET SOTR', payload)
commit(types[`${NAME}_SET_SORT`], payload)
},
[`${name}SetFilter`]({ commit }, payload){
commit(types[`${NAME}_SET_FILTER`], payload)
},
[`${name}ToggleSelection`]({ commit }, payload) {
commit(types[`${NAME}_TOGGLE_SELECTION`], payload)
},
}

const mutations = {
// TOGGLE SELECTION
[types[`${NAME}_TOGGLE_SELECTION`]] (state, entity) {
const index = state.selection.indexOf(entity[_id]);
if (index > -1) {
state.selection = [ ...state.selection.slice(0, index), ...state.selection.slice(index+1) ]
}else{
state.selection.push(entity[_id])
}
},
// SELECT
[types[`${NAME}_SELECT`]] (state, entity) {
const ids = state.collection.map(e => e[_id])
const index = ids.indexOf(entity[_id])
(index > -1) ? state.selected = index : state.selected = null
},
// LOAD
[types[`${NAME}_LOAD`]] (state) {
state.loading = true
},
[types[`${NAME}_LOAD_SUCCESS`]] (state, collection) {
state.loading = false
state.collection = collection
const dates = state.collection.map(entity => entity[_updated_at])
state.last = arrayMax(dates)
},
[types[`${NAME}_LOAD_FAIL`]] (state) {
state.loading = false
state.loaded = false
state.collection = []
},
// ADD
[types[`${NAME}_ADD`]] (state, entity) {
state.collection = [ ...state.collection, entity ]
},
[types[`${NAME}_ADD_SUCCESS`]] (state, entity) {
state.loading = false
const index = state.collection.findIndex(e => e[_id] === entity[_id])
if(index > -1){
state.collection = [ ...state.collection.slice(0, index), entity, ...state.collection.slice(index+1) ]
if(entity[_updated_at] > state.last) state.last = entity[_updated_at]
}
},
[types[`${NAME}_ADD_FAIL`]] (state, entity) {
state.loading = false
const index = state.collection.findIndex(e => e[_id] == entity[_id])
if(index > -1){
state.collection = [ ...state.collection.slice(0, index), ...state.collection.slice(index+1) ]
}
},
// REMOVE
[types[`${NAME}_REMOVE`]](state, entity) {
state.collection = state.collection.filter(e => e[_id] !== entity[_id])
},
[types[`${NAME}_REMOVE_FAIL`]](state, entity) {
state.collection = [ ...state.collection, entity ]
},
// UPDATE
[types[`${NAME}_UPDATE`]] (state, entity) {
const index = state.collection.findIndex(e => e[_id] == entity[_id])
if(index > -1){
state.collection = [ ...state.collection.slice(0, index), entity , ...state.collection.slice(index+1) ]
}
state.loading = true
},
[types[`${NAME}_UPDATE_SUCCESS`]] (state, entity) {
state.loading = false
const index = state.collection.findIndex(e => e[_id] == entity[_id])
if(index > -1){
state.collection = [ ...state.collection.slice(0, index), entity, ...state.collection.slice(index+1) ]
if(entity[_updated_at] > state.last) state.last = entity[_updated_at]
}
},
[types[`${NAME}_UPDATE_FAIL`]] (state, entity) {
state.loading = false
const index = state.collection.findIndex(e => e[_id] == entity[_id])
if(index > -1){
state.collection = [ ...state.collection.slice(0, index), entity, ...state.collection.slice(index+1) ]
}
},
// LAST
[types[`${NAME}_SET_LAST`]](state, payload) {
state.last = payload
},
// SET SORT
[types[`${NAME}_SET_SORT`]](state, payload) {
state.sort = payload ? payload.sort : null
state.reverse = payload ? payload.reverse : false
},
// SET FILTER
[types[`${NAME}_SET_FILTER`]](state, payload) {
state.filter = payload
},
}

return {
state,
getters,
actions,
mutations
}
}
8 changes: 4 additions & 4 deletions src/modules/list-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export default (name, options, db, api) => {
// actions
const actions = {
[`${name}Select`]({ commit }, payload) {
commit(types[`${NAME}_SELECT`], payload)
const index = state.collection.findIndex(e => e[_id] == entity[_id])
commit(types[`${NAME}_SELECT`], index)
},
[`${name}Load`]({ commit }){
console.log('LOAD DATA')
Expand Down Expand Up @@ -93,8 +94,7 @@ export default (name, options, db, api) => {

const mutations = {
// SELECT
[types[`${NAME}_SELECT`]] (state, entity) {
const index = state.collection.findIndex(e => e[_id] == entity[_id])
[types[`${NAME}_SELECT`]] (state, index) {
(index > -1) ? state.selected = index : state.selected = null
},
// TOGGLE SELECTION
Expand All @@ -103,7 +103,7 @@ export default (name, options, db, api) => {
if (index > -1) {
state.selection = [ ...state.selection.slice(0, index), ...state.selection.slice(index+1) ]
}else{
state.selection.push(entiry[_id])
state.selection.push(entity[_id])
}
},
// LOAD
Expand Down

0 comments on commit 3b18cec

Please sign in to comment.