Skip to content

Commit 7fa9cbb

Browse files
committed
Migrate Google Spreadsheet API to v4
1 parent ca4514c commit 7fa9cbb

File tree

7 files changed

+64
-31
lines changed

7 files changed

+64
-31
lines changed

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.17.1

index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<link href="https://fonts.googleapis.com/css?family=Open+Sans|Oswald" rel="stylesheet">
99

1010
<script src="https://addevent.com/libs/atc/1.6.1/atc.min.js"></script>
11-
<script src='https://cdnjs.cloudflare.com/ajax/libs/tabletop.js/1.5.1/tabletop.min.js'></script>
1211

1312
<title>Calendrier des événements - CYCLEPOP</title>
1413
</head>

src/assets/scss/bulma.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ $widescreen: $desktop;
77
$fullhd: $desktop;
88

99
// Colors
10-
$primary: #ff5252;
10+
$primary: #D04143;
1111

1212
// Images placeholder dimensions
1313
$dimensions: 16 24 32 48 64 80 96 128;

src/core/DataStore.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import Evnt from './Evnt'
22
import Ride from './Ride'
33
class DataStore {
4-
constructor (tabletop) {
5-
this.tabletop = tabletop
4+
constructor (backend) {
5+
this.backend = backend
66
}
77

8-
initialize () {
9-
this._buildEvents()
10-
this._buildRides()
8+
async initialize () {
9+
await this._buildEvents()
10+
await this._buildRides()
1111
this._buildDependencies()
1212
}
1313

@@ -23,18 +23,14 @@ class DataStore {
2323
return this.rides.find(ev => ev.slug === slug)
2424
}
2525

26-
_buildEvents () {
27-
this.events = this.tabletop
28-
.sheets('calendrier')
29-
.all()
30-
.map(attrs => new Evnt({ attrs: attrs }))
26+
async _buildEvents () {
27+
const eventsData = await this.backend.dataFor({ sheet: 'calendrier' })
28+
this.events = eventsData.map(attrs => new Evnt({ attrs: attrs }))
3129
}
3230

33-
_buildRides () {
34-
this.rides = this.tabletop
35-
.sheets('sorties')
36-
.all()
37-
.map(attrs => new Ride({ attrs: attrs }))
31+
async _buildRides () {
32+
const ridesData = await this.backend.dataFor({ sheet: 'sorties' })
33+
this.rides = ridesData.map(attrs => new Ride({ attrs: attrs }))
3834
}
3935

4036
_buildDependencies () {

src/core/DataStoreProxy.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import DataStore from './DataStore'
2+
import GoogleSheetsBackend from './GoogleSheetsBackend'
23

34
class DataStoreProxy {
45
static cachedDataStore = undefined
@@ -8,25 +9,18 @@ class DataStoreProxy {
89
if (DataStoreProxy.cachedDataStore) {
910
resolve(DataStoreProxy.cachedDataStore)
1011
} else {
11-
this._fetchSpreadsheet((data, tabletop) => {
12-
const dataStore = new DataStore(tabletop)
13-
dataStore.initialize()
12+
const backend = new GoogleSheetsBackend({
13+
spreadsheetId: window.CONFIG.spreadsheetId,
14+
apiKey: window.CONFIG.apiKey
15+
})
16+
const dataStore = new DataStore(backend)
17+
dataStore.initialize().then(() => {
1418
DataStoreProxy.cachedDataStore = dataStore
1519
resolve(dataStore)
1620
})
1721
}
1822
})
1923
}
20-
21-
static _fetchSpreadsheet (callback) {
22-
console.log('Fetching JSON from Google Spreadsheet...')
23-
window.Tabletop.init({
24-
key: window.CONFIG.spreadsheetUrl,
25-
callback: callback,
26-
simpleSheet: false,
27-
parseNumbers: true
28-
})
29-
}
3024
}
3125

3226
export default DataStoreProxy

src/core/GoogleSheetsBackend.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class GoogleSheetsBackend {
2+
constructor (opts) {
3+
this.spreadsheetId = opts.spreadsheetId
4+
this.apiKey = opts.apiKey
5+
}
6+
7+
dataFor (opts) {
8+
return new Promise((resolve) => {
9+
this._fetchJsonSheetData(opts['sheet']).then(data => {
10+
resolve(this._dataToObjects(data))
11+
})
12+
})
13+
}
14+
15+
_fetchJsonSheetData (sheet) {
16+
return new Promise((resolve) => {
17+
const url = `https://sheets.googleapis.com/v4/spreadsheets/${this.spreadsheetId}/values/${sheet}?key=${this.apiKey}`
18+
console.debug(`Fetching ${sheet} spreadsheet...`)
19+
fetch(url).then(response => {
20+
response.json().then(data => {
21+
resolve(data.values)
22+
})
23+
})
24+
})
25+
}
26+
27+
_dataToObjects (data) {
28+
let objects = []
29+
const headers = data.shift()
30+
data.forEach(line => {
31+
let obj = {}
32+
headers.forEach((key, index) => {
33+
const value = line[index]
34+
obj[key] = value || ''
35+
})
36+
objects.push(obj)
37+
})
38+
return objects
39+
}
40+
}
41+
42+
export default GoogleSheetsBackend

src/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ require('@/assets/scss/main.scss')
99
moment.locale('fr-ca')
1010

1111
window.CONFIG = {
12-
spreadsheetUrl: 'https://docs.google.com/spreadsheets/d/1_zXpbThqwS0xxYcF8NouVxW3UVMA55KlVttf9rvsDZ0/',
12+
spreadsheetId: '1_zXpbThqwS0xxYcF8NouVxW3UVMA55KlVttf9rvsDZ0',
13+
apiKey: 'AIzaSyBKlqE-VpGkZsAQLvNVutvOwt4QHLdb6Hw',
1314
rideWithGpsUrl: `https://ridewithgps.com/routes/[routeId].json?apikey=338acd0e&version=2`,
1415
eventTypes: {
1516
'sortie-locale': {

0 commit comments

Comments
 (0)