Skip to content

Commit f799a09

Browse files
committed
feat(useSessionStorage): Added useSessionStorage and refactored file system structure
1 parent db2d1b4 commit f799a09

File tree

160 files changed

+573
-142
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+573
-142
lines changed

README.md

Lines changed: 25 additions & 23 deletions
File renamed without changes.
File renamed without changes.

src/components/useCookie/useCookie.ts renamed to src/functions/useCookie/useCookie.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
trySerialize,
99
tryDeserialize,
1010
isNullOrUndefined
11-
} from '@src/utils'
11+
} from '@src/shared/utils'
1212
import { ref, onMounted, Ref } from '@src/api'
1313

1414
export interface UseCookieOptions {
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/components/useInterval/useInterval.ts renamed to src/functions/useInterval/useInterval.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ref, Ref } from '@src/api'
2-
import { useIntervalFn } from '@src/components/useIntervalFn'
2+
import { useIntervalFn } from '@src/functions/useIntervalFn'
33

44
export function useInterval(ms = 0, runOnMount = true) {
55
const counter = ref(0)

src/components/useLocalStorage/stories/useLocalStorage.md renamed to src/functions/useLocalStorage/stories/useLocalStorage.md

Lines changed: 10 additions & 10 deletions

src/components/useLocalStorage/useLocalStorage.spec.ts renamed to src/functions/useLocalStorage/useLocalStorage.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const testComponent = (
6161
}
6262
})
6363

64-
describe('useItem', () => {
64+
describe('useLocalStorage', () => {
6565
it('should get a item with the given value', async () => {
6666
const itemKey = 'itemKey'
6767
const itemValue = 'itemValue'
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { createStorage, StorageOptions } from '@src/shared/createStorage'
2+
import { onMounted, Ref } from '@src/api'
3+
4+
export function useLocalStorage(
5+
key: string,
6+
options?: StorageOptions,
7+
runOnMount = true
8+
) {
9+
const { item, getItem, setItem, removeItem } = createStorage(
10+
localStorage,
11+
key,
12+
options
13+
)
14+
15+
onMounted(() => runOnMount && getItem())
16+
17+
return {
18+
item,
19+
getItem,
20+
setItem,
21+
removeItem
22+
}
23+
}

src/components/useLocation/useLocation.ts renamed to src/functions/useLocation/useLocation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ref, onMounted, onUnmounted, Ref } from '@src/api'
2-
import { patchHistoryMethodsOnce } from '@src/utils'
2+
import { patchHistoryMethodsOnce } from '@src/shared/utils'
33

44
export interface UseLocationState {
55
trigger: string
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/components/useRaf/useRaf.ts renamed to src/functions/useRaf/useRaf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ref, Ref } from '@src/api'
2-
import { TFps, useRafFn } from '@src/components/useRafFn'
2+
import { TFps, useRafFn } from '@src/functions/useRafFn'
33

44
const fpsLimit = 60
55
export function useRaf(fps: TFps = fpsLimit, runOnMount = true) {
File renamed without changes.

src/components/useSearchParams/useSearchParams.ts renamed to src/functions/useSearchParams/useSearchParams.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { ref, onMounted, onUnmounted, Ref } from '@src/api'
2-
import { patchHistoryMethodsOnce, normalizeEntriesData } from '@src/utils'
2+
import {
3+
patchHistoryMethodsOnce,
4+
normalizeEntriesData
5+
} from '@src/shared/utils'
36

47
const normalizeParams = (urlParamsObj: { [key: string]: string }) => (
58
paramAcc: any,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './useSessionStorage'
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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>item</td>
12+
<td>{{ item }}</td>
13+
</tr>
14+
<tr>
15+
<td colspan="2">
16+
<button class="button is-primary" @click="handleSetItem">
17+
Set / Update item
18+
</button>
19+
<button class="button is-danger" @click="removeItem()">
20+
Remove item
21+
</button>
22+
</td>
23+
</tr>
24+
<tr>
25+
<td>jsonItem</td>
26+
<td>{{ jsonItem }}</td>
27+
</tr>
28+
<tr>
29+
<td colspan="2">
30+
<button class="button is-primary" @click="handleSetJsonItem">
31+
Set / Update JSON item
32+
</button>
33+
<button class="button is-danger" @click="jsonRemoveItem()">
34+
Remove JSON item
35+
</button>
36+
</td>
37+
</tr>
38+
</tbody>
39+
</table>
40+
</template>
41+
42+
<script lang="ts">
43+
import Vue from 'vue'
44+
import { useSessionStorage } from '@src/vue-use-kit'
45+
46+
export default Vue.extend({
47+
name: 'useSessionStorageDemo',
48+
setup() {
49+
const { item, setItem, removeItem } = useSessionStorage('normalItem')
50+
51+
const {
52+
item: jsonItem,
53+
setItem: jsonSetItem,
54+
removeItem: jsonRemoveItem
55+
} = useSessionStorage('jsonItem', {
56+
isParsing: true
57+
})
58+
59+
let counter = 0
60+
const handleSetItem = () => {
61+
counter++
62+
setItem(`count${counter}`)
63+
}
64+
65+
const handleSetJsonItem = () => {
66+
counter++
67+
jsonSetItem({
68+
counter: counter,
69+
counterTest: `test${counter}`
70+
})
71+
}
72+
73+
return {
74+
item,
75+
handleSetItem,
76+
removeItem,
77+
jsonItem,
78+
handleSetJsonItem,
79+
jsonRemoveItem,
80+
counter
81+
}
82+
}
83+
})
84+
</script>
Lines changed: 77 additions & 0 deletions
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 UseSessionStorageDemo from './UseSessionStorageDemo.vue'
5+
6+
const functionName = 'useSessionStorage'
7+
const functionPath = path.resolve(__dirname, '..')
8+
const notes = require(`./${functionName}.md`).default
9+
10+
const basicDemo = () => ({
11+
components: { StoryTitle, demo: UseSessionStorageDemo },
12+
template: `
13+
<div class="container">
14+
<story-title
15+
function-path="${functionPath}"
16+
source-name="${functionName}"
17+
demo-name="UseSessionStorageDemo.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|useSessionStorage', module)
27+
.addParameters({ notes })
28+
.add('Demo', basicDemo)

0 commit comments

Comments
 (0)