Created
April 19, 2017 16:20
-
-
Save apertureless/15c2089053eb854e2ca3fedb19dd7112 to your computer and use it in GitHub Desktop.
medium post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import axios from 'axios' | |
import LineChart from '@/components/LineChart' | |
export default { | |
components: { | |
LineChart | |
}, | |
props: {}, | |
data () { | |
return { | |
package: null, | |
packageName: '', | |
period: 'last-month', | |
loaded: false, | |
downloads: [], | |
labels: [], | |
showError: false, | |
errorMessage: 'Please enter a package name' | |
} | |
}, | |
mounted () { | |
if (this.$route.params.package) { | |
this.package = this.$route.params.package | |
this.requestData() | |
} | |
}, | |
methods: { | |
resetState () { | |
this.loaded = false | |
this.showError = false | |
}, | |
requestData () { | |
if (this.package === null || this.package === '' || this.package === 'undefined') { | |
this.showError = true | |
return | |
} | |
this.resetState() | |
axios.get(`https://api.npmjs.org/downloads/range/${this.period}/${this.package}`) | |
.then(response => { | |
console.log(response.data) | |
this.downloads = response.data.downloads.map(download => download.downloads) | |
this.labels = response.data.downloads.map(download => download.day) | |
this.packageName = response.data.package | |
this.setURL() | |
this.loaded = true | |
}) | |
.catch(err => { | |
this.errorMessage = err.response.data.error | |
this.showError = true | |
}) | |
}, | |
setURL () { | |
history.pushState({ info: `npm-stats ${this.package}` }, this.package, `/#/${this.package}`) | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment