-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useCookie): Adding useCookie function
- Loading branch information
1 parent
6eb018f
commit 019918f
Showing
10 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useCookie' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<template> | ||
<table class="table is-fullwidth"> | ||
<thead> | ||
<tr> | ||
<th>Prop</th> | ||
<th>Value</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>cookie</td> | ||
<td>{{ cookie }}</td> | ||
</tr> | ||
<tr> | ||
<td colspan="2"> | ||
<button | ||
class="button is-primary" | ||
@click="counter++ && updateCookie(`count${counter}`)" | ||
> | ||
Update cookie | ||
</button> | ||
<button class="button is-danger" @click="deleteCookie()"> | ||
Delete cookie | ||
</button> | ||
</td> | ||
</tr> | ||
<tr> | ||
<td>jsonCookie</td> | ||
<td>{{ jsonCookie }}</td> | ||
</tr> | ||
<tr> | ||
<td colspan="2"> | ||
<button | ||
class="button is-primary" | ||
@click=" | ||
counter++ && | ||
jsonUpdateCookie({ | ||
counter: counter, | ||
counterTest: `test${counter}` | ||
}) | ||
" | ||
> | ||
Update json cookie | ||
</button> | ||
<button class="button is-danger" @click="jsonDeleteCookie()"> | ||
Delete json cookie | ||
</button> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import Vue from 'vue' | ||
import { ref } from '@vue/composition-api' | ||
import { useCookie } from '@src/vue-use-kit' | ||
export default Vue.extend({ | ||
name: 'UseCookieDemo', | ||
setup() { | ||
const counter = ref(0) | ||
const { cookie, updateCookie, deleteCookie } = useCookie('normalCookie') | ||
const { | ||
cookie: jsonCookie, | ||
updateCookie: jsonUpdateCookie, | ||
deleteCookie: jsonDeleteCookie | ||
} = useCookie('jsonCookie') | ||
return { | ||
cookie, | ||
updateCookie, | ||
deleteCookie, | ||
jsonCookie, | ||
jsonUpdateCookie, | ||
jsonDeleteCookie, | ||
counter | ||
} | ||
} | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# useCookie | ||
|
||
Vue function that provides way to read, update and delete a cookie. | ||
|
||
## Reference | ||
|
||
```typescript | ||
// function useCookie() | ||
``` | ||
|
||
### Parameters | ||
|
||
- `value: string` lorem ipsa | ||
|
||
### Returns | ||
|
||
- `value: Ref<string>` lorem ipsa | ||
|
||
## Usage | ||
|
||
```html | ||
<template></template> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { storiesOf } from '@storybook/vue' | ||
import path from 'path' | ||
import StoryTitle from '@src/helpers/StoryTitle.vue' | ||
import UseCookieDemo from './UseCookieDemo.vue' | ||
|
||
const functionName = 'useCookie' | ||
const functionPath = path.resolve(__dirname, '..') | ||
const notes = require(`./${functionName}.md`).default | ||
|
||
const basicDemo = () => ({ | ||
components: { StoryTitle, demo: UseCookieDemo }, | ||
template: ` | ||
<div class="container"> | ||
<story-title | ||
function-path="${functionPath}" | ||
source-name="${functionName}" | ||
demo-name="UseCookieDemo.vue" | ||
> | ||
<template v-slot:title></template> | ||
<template v-slot:intro></template> | ||
</story-title> | ||
<demo /> | ||
</div>` | ||
}) | ||
|
||
storiesOf('side effects|useCookie', module) | ||
.addParameters({ notes }) | ||
.add('Demo', basicDemo) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// import { mount } from '@src/helpers/test' | ||
// import { useCookie } from '@src/vue-use-kit' | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
describe('useCookie', () => { | ||
it('should do something', () => { | ||
// Add test here | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Cookies from 'js-cookie' | ||
import { ref, Ref } from '@vue/composition-api' | ||
|
||
type CookieValueType = string | JSON | ||
|
||
const tryParse = (val: string, enableParsing: boolean) => { | ||
if (!enableParsing) return val | ||
try { | ||
return JSON.parse(val) | ||
} catch (err) { | ||
return val | ||
} | ||
} | ||
|
||
const tryStringify = (val: CookieValueType, enableParsing: boolean) => { | ||
if (!enableParsing) return val | ||
try { | ||
return JSON.stringify(val) | ||
} catch (err) { | ||
return val | ||
} | ||
} | ||
|
||
export function useCookie(cookieName: string, enableParseJSON = false) { | ||
const getCookie = () => { | ||
const cookieVal = Cookies.get(cookieName) | ||
if (typeof cookieVal === 'undefined') return null | ||
return tryParse(cookieVal, enableParseJSON) | ||
} | ||
|
||
const cookie = ref(getCookie()) | ||
|
||
const updateCookie = ( | ||
newVal: CookieValueType, | ||
options?: Cookies.CookieAttributes | ||
) => { | ||
const newCookieValue = tryStringify(newVal, enableParseJSON) | ||
Cookies.set(cookieName, newCookieValue, options) | ||
cookie.value = newVal | ||
} | ||
|
||
const deleteCookie = (options?: Cookies.CookieAttributes) => { | ||
Cookies.remove(cookieName, options) | ||
cookie.value = null | ||
} | ||
|
||
return { cookie, updateCookie, deleteCookie } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters