summaryrefslogtreecommitdiff
path: root/client/db.js
blob: ee31d0d64e9851a5642ccae078472104b594faf2 (plain)
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
function fetchDB(cb) {
  let raw_db;
  let url;
  if (window.location.origin.match('paintings.asdf.us')) {
    url = '/db.json'
  } else {
    url = '/paintings/db.json'
  }
  fetch(url, {
    method: 'GET'
  }).then(res => {
    if (res.status !== 200) {
      return null
    }
    return res.json()
  }).then(json => {
    if (json) {
      raw_db = parse(json)
    }
    cb(raw_db)  
  }).catch((err) => {
    console.warn(err)
  })
}

function parse(db) {
  Object.keys(db).forEach(key => {
    db[key] = db[key]
      .filter((el) => ! el.disabled)
      .sort((a,b) => a.__index<b.__index?-1:a.__index===b.__index?0:1)
  })
  return db
}

const backupDB = {
  painting: [],
  page: [{image:{uri:'about:blank'}}],
}

export default { fetch: fetchDB, backupDB }