Skip to content

Commit e4b7c8b

Browse files
committed
Fix a few bugs
1 parent b13b9a9 commit e4b7c8b

File tree

3 files changed

+31
-41
lines changed

3 files changed

+31
-41
lines changed

resources/js/Pages/Dashboard.vue

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
id,
4141
type,
4242
colspan: 1,
43-
title: t('app', `New "${info.name}" Widget`),
43+
title: t('app', `New “{name} Widget`, {name: info.name}),
4444
subtitle: null,
4545
name: info.name,
4646
bodyHtml: '',
@@ -57,17 +57,22 @@
5757
}
5858
5959
function updateWidget(updates: Widget) {
60-
widgets.value = widgets.value.map((widget: Widget) => {
61-
if (widget.id === updates.id) {
62-
return updates;
60+
widgets.value = widgets.value.map((widget: CompleteWidget) => {
61+
if (widget.new || widget.id === updates.id) {
62+
return {
63+
...widget,
64+
...updates,
65+
};
6366
}
6467
6568
return widget;
6669
});
6770
}
6871
6972
async function deleteWidget(id: number) {
70-
const widget = widgets.value.find((widget: Widget) => widget.id === id);
73+
const widget = widgets.value.find(
74+
(widget: CompleteWidget) => widget.id === id
75+
);
7176
if (!widget) {
7277
return;
7378
}
@@ -83,12 +88,18 @@
8388
}
8489
8590
// Optimistically remove the widget from the UI
86-
widgets.value = widgets.value.filter((widget: Widget) => widget.id !== id);
91+
widgets.value = widgets.value.filter(
92+
(widget: CompleteWidget) => widget.id !== id
93+
);
94+
95+
// If this was a new widget, the database doesn't know about it
96+
// and we don't need to make the API call
97+
if (widget.new) {
98+
return;
99+
}
87100
88101
try {
89-
await axios.post(getCpUrl(`widgets/${props.id}/delete`), {
90-
id: props.id,
91-
});
102+
await axios.delete(getCpUrl(`widgets/${id}`));
92103
// @TODO display success message
93104
// Craft.cp.displaySuccess(t('app', '“{name}” deleted.', {name: widget.getLabel()}));
94105
// @TODO allow undo in the toast message

resources/js/components/DynamicHtmlRenderer.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import {defineComponent, computed} from 'vue';
2+
import {computed, defineComponent} from 'vue';
33
44
const props = defineProps<{
55
html: string;
@@ -10,5 +10,5 @@
1010
);
1111
</script>
1212
<template>
13-
<component :is="dynamicComponent" />
13+
<component :is="dynamicComponent" v-if="html" />
1414
</template>

resources/js/widgets/BaseWidget.vue

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
settingsNamespace: '',
1919
});
2020
21-
const {getCpUrl, getActionUrl} = useHelpers();
21+
const {getCpUrl} = useHelpers();
2222
const state = ref<'idle' | 'loading' | 'deleting' | 'error'>('idle');
2323
2424
function setView(view: 'default' | 'settings') {
@@ -55,6 +55,7 @@
5555
emit('update', {
5656
...data.info,
5757
view: 'default',
58+
new: false,
5859
});
5960
} catch (error) {
6061
state.value = 'error';
@@ -64,38 +65,16 @@
6465
}
6566
6667
async function deleteWidget() {
67-
if (['deleting', 'loading'].includes(state.value)) {
68-
return;
69-
}
68+
emit('delete', props.id);
69+
}
7070
71-
if (
72-
!confirm(
73-
t('app', 'Are you sure you want to delete “{name}”?', {
74-
name: props.name,
75-
})
76-
)
77-
) {
71+
function handleCancel() {
72+
if (props.new) {
73+
deleteWidget();
7874
return;
7975
}
8076
81-
state.value = 'deleting';
82-
try {
83-
await axios.post(getCpUrl(`widgets/${props.id}/delete`), {
84-
id: props.id,
85-
});
86-
emit('delete', props.id);
87-
state.value = 'idle';
88-
// @TODO display success message
89-
// Craft.cp.displaySuccess(t('app', '“{name}” deleted.', {name: widget.getLabel()}));
90-
// @TODO allow undo in the toast message
91-
} catch (error) {
92-
state.value = 'error';
93-
// @TODO handle errors
94-
console.error(error);
95-
// Craft.cp.displayError(t('app', 'Couldn’t delete “{name}”.', {
96-
// name: props.name,
97-
// }))
98-
}
77+
setView('default');
9978
}
10079
10180
const action = computed(() =>
@@ -147,7 +126,7 @@
147126
type="submit"
148127
>{{ t('app', 'Save') }}</craft-button
149128
>
150-
<craft-button type="reset" @click="setView('default')">{{
129+
<craft-button type="reset" @click="handleCancel">{{
151130
t('app', 'Cancel')
152131
}}</craft-button>
153132
<div class="tw:ml-auto"></div>

0 commit comments

Comments
 (0)