<template>
<div class="advanced-dataset">
<div v-if="loading" class="loading">
Loading data...
</div>
<div v-else>
<div class="actions">
<sw-button @click="loadData" :disabled="isSubscribed">
Load Once
</sw-button>
<sw-button
@click="toggleSubscription"
:variant="isSubscribed ? 'danger' : 'primary'"
>
{{ isSubscribed ? 'Unsubscribe' : 'Subscribe' }}
</sw-button>
</div>
<div v-if="error" class="error">
<p>Error: {{ error }}</p>
</div>
<div v-if="salesChannel.name" class="data">
<h4>Sales Channel Data</h4>
<div class="field">
<label>Name:</label>
<sw-text-field v-model="salesChannel.name" />
</div>
<div class="field">
<label>Active:</label>
<sw-switch-field v-model="salesChannel.active" />
</div>
<div class="field">
<label>Type:</label>
<span>{{ salesChannel.type }}</span>
</div>
<sw-button @click="saveChanges" variant="primary">
Save Changes
</sw-button>
</div>
<div v-if="isSubscribed" class="subscription-info">
<p>Live updates active - data will update automatically</p>
<p>Last update: {{ lastUpdate }}</p>
</div>
</div>
</div>
</template>
<script>
import { defineComponent } from 'vue';
import { data, notification } from '@shopware-ag/meteor-admin-sdk';
import { SwButton, SwTextField, SwSwitchField } from '@shopware-ag/meteor-component-library';
export default defineComponent({
components: {
'sw-button': SwButton,
'sw-text-field': SwTextField,
'sw-switch-field': SwSwitchField,
},
data() {
return {
salesChannel: {},
loading: false,
error: null,
isSubscribed: false,
lastUpdate: null,
unsubscribe: null,
};
},
methods: {
async loadData() {
this.loading = true;
this.error = null;
try {
const result = await data.get({
id: 'sw-sales-channel-detail__salesChannel',
selectors: ['name', 'active', 'type'],
});
this.salesChannel = result;
this.lastUpdate = new Date().toLocaleTimeString();
notification.dispatch({
title: 'Data Loaded',
message: 'Sales channel data retrieved',
variant: 'success'
});
} catch (err) {
this.error = err.message || 'Failed to load data';
notification.dispatch({
title: 'Error',
message: this.error,
variant: 'error'
});
} finally {
this.loading = false;
}
},
toggleSubscription() {
if (this.isSubscribed) {
if (this.unsubscribe) {
this.unsubscribe();
}
this.isSubscribed = false;
notification.dispatch({
title: 'Unsubscribed',
message: 'No longer listening for changes',
variant: 'info'
});
} else {
this.unsubscribe = data.subscribe(
'sw-sales-channel-detail__salesChannel',
(response) => {
this.salesChannel = response.data;
this.lastUpdate = new Date().toLocaleTimeString();
},
{
selectors: ['name', 'active', 'type'],
}
);
this.isSubscribed = true;
notification.dispatch({
title: 'Subscribed',
message: 'Now listening for live updates',
variant: 'success'
});
}
},
async saveChanges() {
try {
await data.update({
id: 'sw-sales-channel-detail__salesChannel',
data: this.salesChannel,
});
notification.dispatch({
title: 'Saved',
message: 'Changes saved successfully',
variant: 'success'
});
} catch (err) {
notification.dispatch({
title: 'Error',
message: 'Failed to save changes',
variant: 'error'
});
}
}
},
beforeUnmount() {
if (this.unsubscribe) {
this.unsubscribe();
}
}
});
</script>
<style scoped>
.advanced-dataset {
padding: 20px;
}
.loading {
padding: 40px;
text-align: center;
color: #718096;
}
.actions {
display: flex;
gap: 12px;
margin-bottom: 24px;
}
.error {
padding: 16px;
background: #fff5f5;
border: 1px solid #fc8181;
border-radius: 4px;
color: #c53030;
margin-bottom: 24px;
}
.data {
padding: 20px;
background: #f7fafc;
border-radius: 8px;
margin-bottom: 24px;
}
.data h4 {
margin-bottom: 20px;
color: #2d3748;
}
.field {
margin-bottom: 16px;
}
.field label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: #4a5568;
}
.subscription-info {
padding: 16px;
background: #c6f6d5;
border: 1px solid #68d391;
border-radius: 4px;
color: #22543d;
}
.subscription-info p {
margin-bottom: 4px;
}
</style>