Skip to content

Commit 019918f

Browse files
committed
feat(useCookie): Adding useCookie function
1 parent 6eb018f commit 019918f

File tree

10 files changed

+212
-1
lines changed

10 files changed

+212
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ Vue.use(VueCompositionAPI);
104104
[![Demo](https://img.shields.io/badge/demo-🚀-yellow.svg)](https://microcipcip.github.io/vue-use-kit/?path=/story/animations-usetimeoutfn--demo)
105105
- Side Effects
106106
- [`useBeforeUnload`](./src/components/useBeforeUnload/stories/useBeforeUnload.md) — shows browser alert when user try to reload or close the page.
107-
[![Demo](https://img.shields.io/badge/demo-🚀-yellow.svg)](https://microcipcip.github.io/vue-use-kit/?path=/story/ui-usebeforeunload--demo)
107+
[![Demo](https://img.shields.io/badge/demo-🚀-yellow.svg)](https://microcipcip.github.io/vue-use-kit/?path=/story/side-effects-usebeforeunload--demo)
108+
- [`useCookie`](./src/components/useCookie/stories/useCookie.md) — provides way to read, update and delete a cookie.
109+
[![Demo](https://img.shields.io/badge/demo-🚀-yellow.svg)](https://microcipcip.github.io/vue-use-kit/?path=/story/side-effects-usecookie--demo)
108110
- UI
109111
- [`useClickAway`](./src/components/useClickAway/stories/useClickAway.md) — triggers callback when user clicks outside target area.
110112
[![Demo](https://img.shields.io/badge/demo-🚀-yellow.svg)](https://microcipcip.github.io/vue-use-kit/?path=/story/ui-useclickaway--demo)

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"@storybook/theming": "^5.3.13",
9494
"@storybook/vue": "^5.3.13",
9595
"@types/jest": "^23.3.2",
96+
"@types/js-cookie": "^2.2.4",
9697
"@types/node": "^10.11.0",
9798
"@types/throttle-debounce": "^2.1.0",
9899
"@typescript-eslint/eslint-plugin": "^2.18.0",
@@ -138,6 +139,7 @@
138139
"vue-template-compiler": "^2.6.11"
139140
},
140141
"dependencies": {
142+
"js-cookie": "^2.2.1",
141143
"throttle-debounce": "^2.1.0"
142144
}
143145
}

src/components/useCookie/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './useCookie'
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<template>
2+
<table class="table is-fullwidth">
3+
<thead>
4+
<tr>
5+
<th>Prop</th>
6+
<th>Value</th>
7+
</tr>
8+
</thead>
9+
<tbody>
10+
<tr>
11+
<td>cookie</td>
12+
<td>{{ cookie }}</td>
13+
</tr>
14+
<tr>
15+
<td colspan="2">
16+
<button
17+
class="button is-primary"
18+
@click="counter++ && updateCookie(`count${counter}`)"
19+
>
20+
Update cookie
21+
</button>
22+
<button class="button is-danger" @click="deleteCookie()">
23+
Delete cookie
24+
</button>
25+
</td>
26+
</tr>
27+
<tr>
28+
<td>jsonCookie</td>
29+
<td>{{ jsonCookie }}</td>
30+
</tr>
31+
<tr>
32+
<td colspan="2">
33+
<button
34+
class="button is-primary"
35+
@click="
36+
counter++ &&
37+
jsonUpdateCookie({
38+
counter: counter,
39+
counterTest: `test${counter}`
40+
})
41+
"
42+
>
43+
Update json cookie
44+
</button>
45+
<button class="button is-danger" @click="jsonDeleteCookie()">
46+
Delete json cookie
47+
</button>
48+
</td>
49+
</tr>
50+
</tbody>
51+
</table>
52+
</template>
53+
54+
<script lang="ts">
55+
import Vue from 'vue'
56+
import { ref } from '@vue/composition-api'
57+
import { useCookie } from '@src/vue-use-kit'
58+
59+
export default Vue.extend({
60+
name: 'UseCookieDemo',
61+
setup() {
62+
const counter = ref(0)
63+
64+
const { cookie, updateCookie, deleteCookie } = useCookie('normalCookie')
65+
66+
const {
67+
cookie: jsonCookie,
68+
updateCookie: jsonUpdateCookie,
69+
deleteCookie: jsonDeleteCookie
70+
} = useCookie('jsonCookie')
71+
72+
return {
73+
cookie,
74+
updateCookie,
75+
deleteCookie,
76+
jsonCookie,
77+
jsonUpdateCookie,
78+
jsonDeleteCookie,
79+
counter
80+
}
81+
}
82+
})
83+
</script>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# useCookie
2+
3+
Vue function that provides way to read, update and delete a cookie.
4+
5+
## Reference
6+
7+
```typescript
8+
// function useCookie()
9+
```
10+
11+
### Parameters
12+
13+
- `value: string` lorem ipsa
14+
15+
### Returns
16+
17+
- `value: Ref<string>` lorem ipsa
18+
19+
## Usage
20+
21+
```html
22+
<template></template>
23+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { storiesOf } from '@storybook/vue'
2+
import path from 'path'
3+
import StoryTitle from '@src/helpers/StoryTitle.vue'
4+
import UseCookieDemo from './UseCookieDemo.vue'
5+
6+
const functionName = 'useCookie'
7+
const functionPath = path.resolve(__dirname, '..')
8+
const notes = require(`./${functionName}.md`).default
9+
10+
const basicDemo = () => ({
11+
components: { StoryTitle, demo: UseCookieDemo },
12+
template: `
13+
<div class="container">
14+
<story-title
15+
function-path="${functionPath}"
16+
source-name="${functionName}"
17+
demo-name="UseCookieDemo.vue"
18+
>
19+
<template v-slot:title></template>
20+
<template v-slot:intro></template>
21+
</story-title>
22+
<demo />
23+
</div>`
24+
})
25+
26+
storiesOf('side effects|useCookie', module)
27+
.addParameters({ notes })
28+
.add('Demo', basicDemo)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// import { mount } from '@src/helpers/test'
2+
// import { useCookie } from '@src/vue-use-kit'
3+
4+
afterEach(() => {
5+
jest.clearAllMocks()
6+
})
7+
8+
describe('useCookie', () => {
9+
it('should do something', () => {
10+
// Add test here
11+
})
12+
})
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import Cookies from 'js-cookie'
2+
import { ref, Ref } from '@vue/composition-api'
3+
4+
type CookieValueType = string | JSON
5+
6+
const tryParse = (val: string, enableParsing: boolean) => {
7+
if (!enableParsing) return val
8+
try {
9+
return JSON.parse(val)
10+
} catch (err) {
11+
return val
12+
}
13+
}
14+
15+
const tryStringify = (val: CookieValueType, enableParsing: boolean) => {
16+
if (!enableParsing) return val
17+
try {
18+
return JSON.stringify(val)
19+
} catch (err) {
20+
return val
21+
}
22+
}
23+
24+
export function useCookie(cookieName: string, enableParseJSON = false) {
25+
const getCookie = () => {
26+
const cookieVal = Cookies.get(cookieName)
27+
if (typeof cookieVal === 'undefined') return null
28+
return tryParse(cookieVal, enableParseJSON)
29+
}
30+
31+
const cookie = ref(getCookie())
32+
33+
const updateCookie = (
34+
newVal: CookieValueType,
35+
options?: Cookies.CookieAttributes
36+
) => {
37+
const newCookieValue = tryStringify(newVal, enableParseJSON)
38+
Cookies.set(cookieName, newCookieValue, options)
39+
cookie.value = newVal
40+
}
41+
42+
const deleteCookie = (options?: Cookies.CookieAttributes) => {
43+
Cookies.remove(cookieName, options)
44+
cookie.value = null
45+
}
46+
47+
return { cookie, updateCookie, deleteCookie }
48+
}

src/vue-use-kit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ export * from './components/useTimeoutFn'
2424
export * from './components/useTimeout'
2525
// Site Effects
2626
export * from './components/useBeforeUnload'
27+
export * from './components/useCookie'

0 commit comments

Comments
 (0)