Skip to content

Commit

Permalink
Solara
Browse files Browse the repository at this point in the history
  • Loading branch information
IdeaS0ft committed Sep 6, 2024
1 parent 5e8ede1 commit 0fac3fd
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 107 deletions.
8 changes: 3 additions & 5 deletions solara/lib/core/dashboard/brand/BrandController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ class BrandController {
}

async initializeApp() {
this.model.brandKey = this.model.getQueryFromUrl('brand_key');
this.model.source = this.model.getQueryFromUrl('source') || 'remote';

try {
const { sectionItems, brandName } = await this.model.fetchSections();
const {sectionItems, brandName} = await this.model.fetchSections();
this.view.updateAppNameTitle(brandName);
await this.loadSections(sectionItems);

const { isCurrentBrand, contentChanged } = await this.model.fetchCurrentBrand();
const {isCurrentBrand, contentChanged} = await this.model.fetchCurrentBrand();

if (!isCurrentBrand) {
this.view.showSwitchButton();
} else if (contentChanged) {
Expand Down
153 changes: 51 additions & 102 deletions solara/lib/core/dashboard/brand/BrandModel.js
Original file line number Diff line number Diff line change
@@ -1,129 +1,78 @@
// BrandModel.js
import BrandLocalSource from './source/BrandLocalSource.js';
import BrandRemoteSource from './source/BrandRemoteSource.js';

const DataSource = {
LOCAL: 'local',
REMOTE: 'remote',
};

class BrandModel {
constructor() {
this.sectionItems = [];
this.brandName = '';
this.brandKey = '';
this.isCurrentBrand = false;
this.source = 'remote';
this.savingInProgress = false;
this.localSource = new BrandLocalSource();
this.remoteSource = new BrandRemoteSource();

this.brandKey = this.getQueryFromUrl('brand_key');

const sourceFromUrl = this.getQueryFromUrl('source');
this.source = sourceFromUrl === DataSource.LOCAL ? DataSource.LOCAL : DataSource.REMOTE;
}

getQueryFromUrl(name) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(name);
return this.localSource.getQueryFromUrl(name);
}

async fetchSections() {
try {
if (!this.brandKey) {
throw new Error('No brand_key provided in URL');
}

const url = `/sections?brand_key=${encodeURIComponent(this.brandKey)}`;
console.log('Fetching sections from:', url);

const response = await fetch(url);
const result = await response.json();
if (!response.ok) {
throw new Error(result.error);
}
this.sectionItems = result.result.sections;
this.brandName = result.result.brand_name;
return { sectionItems: this.sectionItems, brandName: this.brandName };
} catch (error) {
console.error('Error fetching config sections:', error);
throw error;
switch (this.source) {
case DataSource.LOCAL:
return await this.localSource.fetchSections(this.brandKey);
case DataSource.REMOTE:
return await this.remoteSource.fetchSections(this.brandKey);
default:
throw new Error('Unknown data source');
}
}

async fetchCurrentBrand() {
try {
const response = await fetch('/brand/current');
let result = await response.json();
if (!response.ok) {
throw new Error(result.error);
}
this.isCurrentBrand = result.key === this.brandKey;
return { isCurrentBrand: this.isCurrentBrand, contentChanged: result.content_changed };
} catch (error) {
console.error('Error fetching current brand:', error);
throw error;
switch (this.source) {
case DataSource.LOCAL:
return await this.localSource.fetchCurrentBrand(this.brandKey);
case DataSource.REMOTE:
return await this.remoteSource.fetchCurrentBrand(this.brandKey);
default:
throw new Error('Unknown data source');
}
}

async saveSection(sectionItem, configuration) {
if (this.savingInProgress) return;
this.savingInProgress = true;

const dataToSend = {
brand_key: this.brandKey,
key: sectionItem.key,
data: configuration
};

try {
const response = await fetch(`/section/edit`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(dataToSend)
});

const result = await response.json();

if (!response.ok) {
throw new Error(result.error);
}

console.log(`${sectionItem.name} configuration saved successfully!`);
return true;
} catch (error) {
console.error('Error saving configuration:', error);
throw error;
} finally {
this.savingInProgress = false;
switch (this.source) {
case DataSource.LOCAL:
return await this.localSource.saveSection(sectionItem, configuration, this.brandKey);
case DataSource.REMOTE:
return await this.remoteSource.saveSection(sectionItem, configuration, this.brandKey);
default:
throw new Error('Unknown data source');
}
}

async switchToBrand() {
try {
const response = await fetch('/switch', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({brand_key: this.brandKey}),
});

const result = await response.json();

if (!response.ok) {
throw new Error(result.error);
}

console.log('Switch to brand result:', result);
return true;
} catch (error) {
console.error('Error switching to brand:', error);
throw error;
switch (this.source) {
case DataSource.LOCAL:
return await this.localSource.switchToBrand(this.brandKey);
case DataSource.REMOTE:
return await this.remoteSource.switchToBrand(this.brandKey);
default:
throw new Error('Unknown data source');
}
}

async checkBrandHealth() {
try {
const response = await fetch(`/brand/doctor?brand_key=${encodeURIComponent(this.brandKey)}`);
const result = await response.json();

if (!response.ok) {
throw new Error(result.error);
}

return result.result;
} catch (error) {
console.error('Error calling doctor API:', error);
throw error;
switch (this.source) {
case DataSource.LOCAL:
return await this.localSource.checkBrandHealth(this.brandKey);
case DataSource.REMOTE:
return await this.remoteSource.checkBrandHealth(this.brandKey);
default:
throw new Error('Unknown data source');
}
}
}
Expand Down
123 changes: 123 additions & 0 deletions solara/lib/core/dashboard/brand/source/BrandLocalSource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
class BrandLocalSource {
constructor() {
this.savingInProgress = false;
}

getQueryFromUrl(name) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(name);
}

async fetchSections(brandKey) {
try {
if (!brandKey) {
throw new Error('No brand_key provided in URL');
}

const url = `/sections?brand_key=${encodeURIComponent(brandKey)}`;
console.log('Fetching sections from:', url);

const response = await fetch(url);
const result = await response.json();
if (!response.ok) {
throw new Error(result.error);
}
return {sectionItems: result.result.sections, brandName: result.result.brand_name};
} catch (error) {
console.error('Error fetching config sections:', error);
throw error;
}
}

async fetchCurrentBrand(brandKey) {
try {
const response = await fetch('/brand/current');
let result = await response.json();
if (!response.ok) {
throw new Error(result.error);
}
let isCurrentBrand = result.key === brandKey;
return {isCurrentBrand: isCurrentBrand, contentChanged: result.content_changed};
} catch (error) {
console.error('Error fetching current brand:', error);
throw error;
}
}

async saveSection(sectionItem, configuration, brandKey) {
if (this.savingInProgress) return;
this.savingInProgress = true;

const dataToSend = {
brand_key: brandKey,
key: sectionItem.key,
data: configuration
};

try {
const response = await fetch(`/section/edit`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(dataToSend)
});

const result = await response.json();

if (!response.ok) {
throw new Error(result.error);
}

console.log(`${sectionItem.name} configuration saved successfully!`);
return true;
} catch (error) {
console.error('Error saving configuration:', error);
throw error;
} finally {
this.savingInProgress = false;
}
}

async switchToBrand(brandKey) {
try {
const response = await fetch('/switch', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({brand_key: brandKey}),
});

const result = await response.json();

if (!response.ok) {
throw new Error(result.error);
}

console.log('Switch to brand result:', result);
return true;
} catch (error) {
console.error('Error switching to brand:', error);
throw error;
}
}

async checkBrandHealth(brandKey) {
try {
const response = await fetch(`/brand/doctor?brand_key=${encodeURIComponent(brandKey)}`);
const result = await response.json();

if (!response.ok) {
throw new Error(result.error);
}

return result.result;
} catch (error) {
console.error('Error calling doctor API:', error);
throw error;
}
}
}

export default BrandLocalSource;
27 changes: 27 additions & 0 deletions solara/lib/core/dashboard/brand/source/BrandRemoteSource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class BrandRemoteSource {
constructor() {
}

// Keep functions for future implementation
async fetchSections(brandKey) {
// Empty for now
}

async fetchCurrentBrand(brandKey) {
// Empty for now
}

async saveSection(sectionItem, configuration, brandKey) {
// Empty for now
}

async switchToBrand(brandKey) {
// Empty for now
}

async checkBrandHealth(brandKey) {
// Empty for now
}
}

export default BrandRemoteSource;

0 comments on commit 0fac3fd

Please sign in to comment.