-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcovid19.js
executable file
·147 lines (136 loc) · 4.07 KB
/
covid19.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2020 Alfian Busyro (twitter: arufian_b)
/**
* This component will retrieve Account's or Contact's country or city name,
* then retrieve COVID-19 statistics data from API,
* and lastly show the statistics data in Account's or Contact's record page
*
* @author arufian
*/
import { LightningElement, wire, api } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import { loadScript } from "lightning/platformResourceLoader";
import ChartJS from "@salesforce/resourceUrl/chart";
import ACCOUNT_BILLING_CITY from '@salesforce/schema/Account.BillingCity'
import ACCOUNT_BILLING_COUNTRY from '@salesforce/schema/Account.BillingCountry'
import CONTACT_MAILING_CITY from '@salesforce/schema/Contact.MailingCity'
import CONTACT_MAILING_COUNTRY from '@salesforce/schema/Contact.MailingCountry'
const BASE_URL = 'https://covid19-data-aru.herokuapp.com/';
const DEFAULT_TITILE = 'COVID-19 Information';
export default class Covid19 extends LightningElement {
@api recordId;
@api objectApiName;
confirmed;
recovered;
deaths;
title;
fields;
record;
isLoading;
error;
errorMessage;
chart;
constructor() {
super();
this.title = DEFAULT_TITILE;
}
@wire(getRecord, { recordId: '$recordId', fields: '$fields' })
load(result) {
if (result.data) {
this.record = result.data.fields;
this.fetchApi()
}
}
async connectedCallback() {
if (this.objectApiName === 'Account') {
this.fields = [ACCOUNT_BILLING_CITY, ACCOUNT_BILLING_COUNTRY];
this.errorMessage = `Please check this Account's Billing City or Country, and fill with correct value`;
} else {
this.fields = [CONTACT_MAILING_CITY, CONTACT_MAILING_COUNTRY];
this.errorMessage = `Please check this Contact's Mailing City or Country, and fill with correct value`;
}
this.isLoading = true;
try {
loadScript(this, ChartJS);
} catch (error) {
console.error(error)
this.dispatchEvent(
new ShowToastEvent({
title: "Error Loading Chart JS",
message: error.message,
variant: "error"
})
);
}
}
_buildChart(apiData) {
let canvas = this.template.querySelector("canvas");
let context = canvas.getContext("2d");
const data = apiData.map((item) => item.confirmed);
const date = apiData.map((item) => item.date);
if(this.chart) this.chart.reset();
this.chart = new window.Chart(context, {
type: "bar",
data: {
labels: date,
datasets: [
{
label: "Confirmed cases",
data,
backgroundColor: "rgba(255, 99, 132, 0.2)",
borderColor: "rgba(255, 99, 132, 1)",
borderWidth: 1
}
]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}
async fetchApi() {
this.title = DEFAULT_TITILE;
const { BillingCity, BillingCountry, MailingCity, MailingCountry } = this.record;
let param;
let query = 'country';
if (this.objectApiName === 'Account') {
if (BillingCountry.value) param = BillingCountry.value;
else {
param = BillingCity.value;
query = 'city';
}
} else {
if (MailingCountry.value) param = MailingCountry.value;
else {
param = MailingCity.value;
query = 'city';
}
}
this.isLoading = false;
if (param === null) {
this.error = true;
return;
}
try {
const resp = await fetch(`${BASE_URL}?${query}=${param}`);
const json = await resp.json();
const { confirmed, recovered, deaths } = json.latest;
const { flag, name } = json.country_info;
this.confirmed = confirmed;
this.recovered = recovered;
this.deaths = deaths;
this.title = `${name}'s COVID-19 Information ${flag}`;
this.error = false;
setTimeout(() => this._buildChart(json.graph), 400);
} catch (e) {
console.error(e);
this.error = true;
this.errorMessage += ' and try to refresh the page';
}
}
}