Skip to content

Commit 66536d7

Browse files
committedSep 13, 2023
fix: zero button component now handles loading state updates correctly
1 parent d9604b2 commit 66536d7

24 files changed

+39
-69
lines changed
 
-2 Bytes
Binary file not shown.
Binary file not shown.
-3 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
-2 Bytes
Binary file not shown.
Binary file not shown.
-7 Bytes
Binary file not shown.
Binary file not shown.
-3 Bytes
Binary file not shown.
Binary file not shown.
-4 Bytes
Binary file not shown.
Binary file not shown.
-5 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

‎modules/zero-components/button/components/button.vue

+21-8
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
</template>
1515

1616
<script setup>
17-
// ===================================================================== Imports
18-
19-
2017
// ======================================================================= Props
2118
const props = defineProps({
2219
tag: { // 'button', 'a' or 'nuxt-link'
@@ -51,13 +48,16 @@ const props = defineProps({
5148
}
5249
})
5350
51+
// ======================================================================== Data
5452
const emit = defineEmits(['clicked'])
53+
const store = useZeroButtonStore()
54+
const id = props.loader
55+
const loading = ref(false)
5556
5657
// ======================================================================= Setup
57-
const { $button } = useNuxtApp()
58-
const id = props.loader || useUuid().v4()
59-
const button = await $button(id).register()
60-
const loading = button && button.loading
58+
if (id) {
59+
store.set({ id, loading: false })
60+
}
6161
6262
// ==================================================================== Computed
6363
const component = computed(() => {
@@ -66,14 +66,27 @@ const component = computed(() => {
6666
return resolveComponent('NuxtLink')
6767
})
6868
69+
// ======================================================================= Watch
70+
watch(store.buttons, (buttons) => {
71+
const found = buttons.find(button => button.id === id)
72+
if (found) {
73+
loading.value = found.loading
74+
}
75+
})
76+
6977
// ===================================================================== Methods
7078
const clickHandler = (e) => {
7179
e.stopPropagation()
7280
if (!props.disabled) {
7381
if (typeof props.loader === 'string') {
74-
$button(id).set({ loading: true })
82+
store.set({ id, loading: true })
7583
}
7684
emit('clicked', e)
7785
}
7886
}
87+
88+
// ======================================================================= Hooks
89+
onBeforeUnmount(() => {
90+
store.remove(id)
91+
})
7992
</script>

‎modules/zero-components/button/index.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ export default {
22
components: [
33
{ file: 'button.vue', name: 'ZeroButton' }
44
],
5-
plugins: [
6-
{ file: 'button.js', name: 'zero-button' }
7-
],
85
stores: [
9-
{ file: 'button.js', name: 'zero-button-store' }
6+
{ file: 'button.js', name: 'useZeroButtonStore' }
107
]
118
}

‎modules/zero-components/button/plugins/button.js

-45
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
// ///////////////////////////////////////////////////////////////////// Imports
22
// -----------------------------------------------------------------------------
33
import { defineStore } from 'pinia'
4-
import { ref } from '#imports'
4+
import { reactive } from '#imports'
55

66
// /////////////////////////////////////////////////////////////////////// State
77
// -----------------------------------------------------------------------------
8-
const buttons = ref({})
8+
const buttons = reactive([])
99

1010
// ///////////////////////////////////////////////////////////////////// Actions
1111
// -----------------------------------------------------------------------------
12-
// /////////////////////////////////////////////////////////////////// setButton
13-
const setButton = (payload) => {
14-
buttons.value[payload.id] = payload
15-
return payload
12+
// ///////////////////////////////////////////////////////////////////////// set
13+
const set = (payload) => {
14+
const index = buttons.findIndex(button => button.id === payload.id)
15+
index === -1 ? buttons.push(payload) : buttons.splice(index, 1, payload)
1616
}
1717

18-
// //////////////////////////////////////////////////////////////// removeButton
19-
const removeButton = (id) => {
20-
delete buttons.value[id]
18+
// ////////////////////////////////////////////////////////////////////// remove
19+
const remove = (id) => {
20+
const index = buttons.findIndex(button => button.id === id)
21+
if (id !== -1) {
22+
buttons.splice(index, 1)
23+
}
2124
}
2225

2326
// ////////////////////////////////////////////////////////////////////// Export
@@ -26,6 +29,6 @@ export const useZeroButtonStore = defineStore('zero-button', () => ({
2629
// ----- state
2730
buttons,
2831
// ----- actions
29-
setButton,
30-
removeButton
32+
set,
33+
remove
3134
}))

‎modules/zero-components/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const registerStores = (submodule, stores) => {
5050
stores.forEach((store) => {
5151
addImports({
5252
name: store.name,
53+
as: store.name,
5354
from: resolve(`${submodule}/stores/${store.file}`)
5455
})
5556
})

‎nuxt.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const baseUrls = {
88
production: ''
99
}
1010

11-
const frontendPort = 10050
11+
const frontendPort = 10070
1212

1313
const seo = {
1414
siteName: 'Singularity',

‎pages/index.vue

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</template>
88

99
<script setup>
10+
// ===================================================================== Imports
1011
import BlockBuilder from '@/components/blocks/block-builder'
1112
1213
// ======================================================================== Data

0 commit comments

Comments
 (0)