2022-04-08 19:49:11 +03:00
|
|
|
import Component from '@glimmer/component';
|
2022-04-28 19:16:35 +03:00
|
|
|
import fetch from 'fetch';
|
2022-04-08 19:49:11 +03:00
|
|
|
import {action} from '@ember/object';
|
2022-04-28 19:55:36 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2022-04-28 19:16:35 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2022-04-08 19:49:11 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2022-04-28 19:16:35 +03:00
|
|
|
|
|
|
|
const API_URL = 'https://resources.ghost.io/resources';
|
|
|
|
const API_KEY = 'b30afc1721f5d8d021ec3450ef';
|
|
|
|
const RESOURCE_COUNT = 1;
|
2022-04-08 19:49:11 +03:00
|
|
|
|
2022-04-22 13:42:51 +03:00
|
|
|
export default class Resources extends Component {
|
2022-04-28 19:16:35 +03:00
|
|
|
@service dashboardStats;
|
2022-04-08 19:49:11 +03:00
|
|
|
@tracked loading = null;
|
2022-04-28 19:16:35 +03:00
|
|
|
@tracked error = null;
|
|
|
|
@tracked resources = null;
|
|
|
|
@tracked resource = null;
|
2022-04-08 19:49:11 +03:00
|
|
|
|
|
|
|
@action
|
2022-04-28 19:16:35 +03:00
|
|
|
load() {
|
|
|
|
this.loading = true;
|
|
|
|
this.fetch.perform().then(() => {
|
|
|
|
this.loading = false;
|
2022-05-02 11:45:15 +03:00
|
|
|
}).catch((error) => {
|
2022-04-28 19:16:35 +03:00
|
|
|
this.error = error;
|
|
|
|
this.loading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-02 11:45:15 +03:00
|
|
|
get tag() {
|
|
|
|
// Depending on the state of the site, we might want to show resources from different tags.
|
|
|
|
if (this.areMembersEnabled && this.hasPaidTiers) {
|
|
|
|
return 'business';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.areMembersEnabled) {
|
|
|
|
return 'growth';
|
|
|
|
}
|
|
|
|
return 'building';
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:16:35 +03:00
|
|
|
@task
|
|
|
|
*fetch() {
|
|
|
|
const order = encodeURIComponent('published_at DESC');
|
|
|
|
const key = encodeURIComponent(API_KEY);
|
|
|
|
const limit = encodeURIComponent(RESOURCE_COUNT);
|
2022-05-02 11:52:27 +03:00
|
|
|
const filter = encodeURIComponent('tag:' + this.tag);
|
2022-05-02 11:45:15 +03:00
|
|
|
let response = yield fetch(`${API_URL}/ghost/api/content/posts/?limit=${limit}&order=${order}&key=${key}&include=none&filter=${filter}`);
|
2022-04-28 19:16:35 +03:00
|
|
|
if (!response.ok) {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
console.error('Failed to fetch resources', {response});
|
|
|
|
this.error = 'Failed to fetch';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = yield response.json();
|
|
|
|
this.resources = result.posts || [];
|
|
|
|
this.resource = this.resources[0]; // just get the first
|
|
|
|
}
|
|
|
|
|
|
|
|
get hasPaidTiers() {
|
|
|
|
return this.dashboardStats.siteStatus?.hasPaidTiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
get areNewslettersEnabled() {
|
|
|
|
return this.dashboardStats.siteStatus?.newslettersEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
get areMembersEnabled() {
|
|
|
|
return this.dashboardStats.siteStatus?.membersEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
get hasNothingEnabled() {
|
2022-04-28 19:55:36 +03:00
|
|
|
return (!this.areMembersEnabled && !this.areNewslettersEnabled && !this.hasPaidTiers);
|
2022-04-28 19:16:35 +03:00
|
|
|
}
|
2022-04-08 19:49:11 +03:00
|
|
|
}
|