Skip to content

Commit

Permalink
Adds widget migration
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpolman committed Sep 19, 2024
1 parent fca1d2c commit f14fdca
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 29 deletions.
44 changes: 25 additions & 19 deletions apps/api/src/app/migrations/20240919125803-widget-sub.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,33 @@ module.exports = {
const widgets = await widgetColl.find({}).toArray();
const pools = await poolColl.find({}).toArray();
const brands = await brandColl.find({}).toArray();
const operations = widgets.map((w) => {
const pool = pools.find((p) => String(p._id) == w.poolId);
const brand = brands.find((b) => b.poolId == w.poolId);
if (!pool) return;
const operations = widgets
.map((w) => {
try {
const pool = pools.find((p) => String(p._id) == w.poolId);
const brand = brands.find((b) => b.poolId == w.poolId);
if (!pool) throw new Error('Pool not found');

return {
updateOne: {
filter: { _id: w._id },
update: {
$set: {
sub: pool.sub,
slug: pool.slug,
name: pool.settings.title,
description: pool.settings.description,
logoImgURL: brand && brand.logoImgUrl,
backgroundImgURL: brand && brand.backgroundImgUrl,
return {
updateOne: {
filter: { _id: w._id },
update: {
$set: {
sub: pool.sub,
slug: pool.settings.slug,
name: pool.settings.title,
description: pool.settings.description,
logoImgURL: brand && brand.logoImgUrl,
backgroundImgURL: brand && brand.backgroundImgUrl,
},
},
},
},
},
};
});
};
} catch (error) {
console.error('Operation Error', error);
}
})
.filter((o) => !!o);

await widgetColl.bulkWrite(operations);
},
Expand Down
1 change: 1 addition & 0 deletions apps/studio/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare module 'vue' {
export interface GlobalComponents {
BAlert: typeof import('bootstrap-vue-next')['BAlert']
BaseCardCollectionMetadata: typeof import('./src/components/BaseCardCollectionMetadata.vue')['default']
BaseCardProfile: typeof import('./src/components/BaseCardProfile.vue')['default']
BaseCardTableHeader: typeof import('./src/components/BaseCardTableHeader.vue')['default']
BaseFormCollectionMetadata: typeof import('./src/components/BaseFormCollectionMetadata.vue')['default']
BaseFormCollectionMetadataQRCodes: typeof import('./src/components/BaseFormCollectionMetadataQRCodes.vue')['default']
Expand Down
136 changes: 136 additions & 0 deletions apps/studio/src/components/BaseCardProfile.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<template>
<b-card variant="darker" class="mb-5">
<h3 class="d-flex align-items-center">
Profile
<b-button variant="dark" :href="url" class="ms-auto">
<BaseIcon icon="external-link-alt" />
</b-button>
</h3>
<b-row>
<b-col md="6">
<BaseFormGroup label="Name">
<b-form-input v-model="name" />
</BaseFormGroup>
<BaseFormGroup label="Slug">
<b-form-input v-model="slug" />
</BaseFormGroup>
<BaseFormGroup label="URL">
<b-form-input v-model="domain" />
</BaseFormGroup>
</b-col>
<b-col md="6">
<BaseFormGroup label="Logo">
<BaseFormInputFile
:image-src="logoImgURL"
:is-loading="isLoadingLogo"
@loading="isLoadingLogo = true"
@done="onUpdate({ logoImgURL: $event })"
/>
</BaseFormGroup>
<BaseFormGroup label="Background">
<BaseFormInputFile
:image-src="backgroundImgURL"
:is-loading="isLoadingBackground"
@loading="isLoadingBackground = true"
@done="onUpdate({ backgroundImgURL: $event })"
/>
</BaseFormGroup>
</b-col>
</b-row>

<h4 class="my-3">Elements</h4>
<b-row>
<b-col v-for="element of elements" class="d-flex mb-2 align-items-center" md="2">
<b-form-input v-model="element.color" type="color" class="me-3" />
<span class="text-opaque">{{ element.label }}</span>
</b-col>
</b-row>

<h4 class="my-3">Colors</h4>
<b-row>
<b-col v-for="element of colors" class="d-flex mb-2 align-items-center" md="2">
<b-form-input v-model="element.color" type="color" class="me-3" />
<span class="text-opaque">{{ element.label }}</span>
</b-col>
</b-row>
</b-card>
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue';
import { WALLET_URL } from '@thxnetwork/studio/config/secrets';
import { mapStores } from 'pinia';
import { useAccountStore } from '@thxnetwork/studio/stores';
import { toast } from '@thxnetwork/studio/utils/toast';
export default defineComponent({
name: 'BaseCardProfile',
props: {
profile: {
type: Object as PropType<TWidget>,
required: true,
},
},
data() {
return {
isLoading: false,
isLoadingLogo: false,
isLoadingBackground: false,
logoImgURL: '',
backgroundImgURL: '',
domain: '',
name: '',
slug: '',
elements: [] as { color: string; label: string }[],
colors: [] as { color: string; label: string }[],
};
},
computed: {
...mapStores(useAccountStore),
url() {
const url = new URL(WALLET_URL);
url.pathname = this.profile._id;
return url.toString();
},
},
mounted() {
this.logoImgURL = this.profile.logoImgURL;
this.backgroundImgURL = this.profile.backgroundImgURL;
this.domain = this.profile.domain;
this.name = this.profile.name;
this.slug = this.profile.slug;
const { elements, colors } = JSON.parse(this.profile.theme);
this.elements = elements;
this.colors = colors;
},
methods: {
async update() {
try {
this.isLoading = true;
await this.accountStore.updateProfile(this.profile._id, {
logoImgURL: '',
backgroundImgURL: '',
name: this.name,
slug: this.slug,
domain: this.domain,
theme: JSON.stringify({ elements: this.elements, colors: this.colors }),
});
} catch (error: any) {
toast(error.message, 'light', 3000, () => {
return;
});
} finally {
this.isLoading = false;
}
},
async onUpdate(update: Partial<TWidget>) {
for (const key in update) {
this[key] = update[key];
}
await this.update();
this.isLoading = false;
},
},
});
</script>
1 change: 1 addition & 0 deletions apps/studio/src/config/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const BASE_URL = import.meta.env.BASE_URL;
export const AUTH_URL = import.meta.env.VITE_AUTH_URL || '';
export const API_URL = import.meta.env.VITE_API_URL || '';
export const WIDGET_URL = import.meta.env.VITE_WIDGET_URL || '';
export const WALLET_URL = import.meta.env.VITE_WALLET_URL || '';
export const PUBLIC_URL = import.meta.env.VITE_PUBLIC_URL || 'https://thx.network';
export const DASHBOARD_URL = import.meta.env.VITE_DASHBOARD_URL || 'https://dashboard.thx.network';
export const STUDIO_URL = import.meta.env.VITE_STUDIO_URL || 'https://studio.thx.network';
Expand Down
6 changes: 6 additions & 0 deletions apps/studio/src/stores/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@ export const useAccountStore = defineStore('account', {
async getProfiles() {
this.profiles = await this.request('/widgets');
},
async updateProfile(profileId: string, profile: Partial<TWidget>) {
await this.request(`/widgets/${profileId}`, {
method: 'PATCH',
body: profile,
});
},
},
});
16 changes: 6 additions & 10 deletions apps/studio/src/views/studio/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@
</b-container>
<div class="bg-dark py-5 flex-grow-1">
<b-container>
<b-card v-for="profile of accountStore.profiles" variant="darker" class="mb-5">
{{ profile }}
<h3>Domain</h3>
<BaseFormGroup label="URL">
<b-form-input v-model="domain" />
</BaseFormGroup>
<h3>Theme</h3>
{{ theme }}
</b-card>
<b-row>
<b-col v-for="profile of accountStore.profiles" md="12">
<BaseCardProfile :profile="profile" />
</b-col>
</b-row>
</b-container>
</div>
</template>
Expand All @@ -36,7 +32,7 @@ export default defineComponent({
async mounted() {
await this.accountStore.get();
await this.accountStore.getProfiles();
console.log(this.accountStore.profiles);
},
methods: {},
});
</script>
1 change: 1 addition & 0 deletions libs/common/src/lib/types/Widget.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
type TWidget = {
_id: string;
sub: string;
slug: string;
name: string;
Expand Down

0 comments on commit f14fdca

Please sign in to comment.