Skip to content

Commit

Permalink
Merge pull request #16 from Yoonit-Labs/feature/plugin-store
Browse files Browse the repository at this point in the history
Feature/plugin store
  • Loading branch information
gabrielrizzo authored Sep 21, 2021
2 parents 8c0bd00 + c12d857 commit 9ce1a0d
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 13 deletions.
36 changes: 34 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Yoox

## How to run the project
## How to install
``
npm install @yoonit/yoox-store-js
``
Expand All @@ -13,7 +13,7 @@ import yourStore from './yourstore'
const myStore = Yoox.store(yourStore, { persist: true, onLoad: hideLoad })
```

## store object
## Store object
It's only needed to create an object with keys: set, get, mix and state.
<br>

Expand Down Expand Up @@ -160,3 +160,35 @@ It's only necessary to pass { persist: true } to the second parameter on store,

- It's highly recommended to use onLoad method to guarantee that persisted data have been loaded to Yoox state. Ie: Show load until onLoad callback is called.

## Vue Plugin

If you are using Vue 2:

````
// main.js
import { VueInstall } from '@yoonit/perse-sdk-js'
import myStore from '../yoox'
Vue.use(VueInstall, myStore)
// Inside project
this.$Yoox.set('userData', user)
````

If you are using Vue 3:

````
// main.js
import { VueNextInstall } from '@yoonit/perse-sdk-js'
import myStore from '../yoox'
createApp(App).use(VueNextInstall, myStore)
// Inside project. It's important to notice that there is no '$' on vue 3 global variable
this.__Yoox.set('userData', user)
````
6 changes: 4 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { store } from './yoox'
import { VueInstall } from "./plugins/vue"
import { VueNextInstall } from './plugins/vue3'

export default { store }
export default { store, VueInstall, VueNextInstall }

export { store }
export { store, VueInstall, VueNextInstall }
9 changes: 0 additions & 9 deletions src/plugins/vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ export const VueInstall = {
}, // data

methods: {
/**
* Returns the dispatch method
* @param payload
* @returns {*}
*/
dispatch(payload) {
return this.localStore.dispatch(payload)
},

/**
* Returns the get method of a given get name
* @param payload
Expand Down
62 changes: 62 additions & 0 deletions src/plugins/vue3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
export const VueNextInstall = {
install: (app, storeInstance) => {
app.mixin({
data () {
return {
localState: {},
__Yoox: undefined
}
},
created () {
/**
* Observe to update yoox and state on changes
*/
storeInstance.observe((state) => {
this.localState = { ...state }
this.__Yoox = { ...storeInstance }
})
},
computed: {
/**
* Returns local state with vue reactivity
* @returns {{}|*}
*/
state () {
return this.localState
}
}, // computed
methods: {
/**
* Returns the get method of a given get name
* @param payload
* @returns {*}
*/
get (payload) {
return this.__Yoox.get(payload)
},

/**
* Returns the mix method of a given name
* @param action
* @param payload
* @returns {*}
*/
mix (action, payload) {
return this.__Yoox.mix(action, payload)
},

/**
* Update yoox according to action name and payload
* @param action
* @param payload
* @returns {*}
*/
set (action, payload) {
return this.__Yoox.set(action, payload)
}
}
})
}
}

export default VueNextInstall

0 comments on commit 9ce1a0d

Please sign in to comment.