Last active
May 28, 2021 13:00
-
-
Save NullVoxPopuli/a6189f3f9fc6ba3ba87976d73e84b09f to your computer and use it in GitHub Desktop.
Convert to derived data - new
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
import Component from '@glimmer/component'; | |
import { tracked } from '@glimmer/tracking'; | |
import { action } from '@ember/object'; | |
export default class MyRepository extends Component { | |
@tracked showDetailVisible = false; | |
@tracked branches = ''; | |
@action | |
async showDetail(name, organizationName, token) { | |
let requestOptions = { | |
method: 'GET', | |
headers: { | |
'Accept': 'application/vnd.github.v3+json', | |
'Authorization': 'token ' + token | |
} | |
}; | |
let response = await fetch('https://api.github.com/repos/'+organizationName+'/'+name+'/branches', requestOptions); | |
let data = await response.json(); | |
if (!response.ok) { | |
this.requestError = 'Some of the properties are wrong.'; | |
} else { | |
this.branches = data; | |
this.showDetailVisible = !this.showDetailVisible; | |
} | |
} | |
} |
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
import Controller from '@ember/controller'; | |
import { cached, tracked } from '@glimmer/tracking'; | |
import { action } from '@ember/object'; | |
import { filterBy } from '@ember/object/computed'; | |
import { task } from 'ember-concurrency'; | |
export default class ApplicationController extends Controller { | |
@tracked requestError = ''; | |
@tracked dataFetched = false; | |
@tracked repositories = ''; | |
@tracked filteredRepositories = ''; | |
@tracked languages = ''; | |
@tracked isPrivate = false; | |
@tracked languageFilter = ''; | |
@action | |
isPrivateCheck(event) { | |
this.isPrivate = event.target.checked; | |
if (this.languageFilter != 'All') { | |
this.filteredRepositories = this.repositories.filterBy('private', this.isPrivate).filterBy('language', this.languageFilter); | |
} else { | |
this.filteredRepositories = this.repositories.filterBy('private', this.isPrivate) | |
} | |
} | |
@action | |
languageFilterCheck(value) { | |
this.languageFilter = value; | |
if (value != 'All') { | |
this.filteredRepositories = this.repositories.filterBy('language', value).filterBy('private', this.isPrivate); | |
} else { | |
this.filteredRepositories = this.repositories.filterBy('private', this.isPrivate); | |
} | |
} | |
get repositories() { | |
return this.submitToken.lastSuccessful?.value ?? []; | |
} | |
get filteredRepositories() { | |
return this.repositories; | |
} | |
@cached1 | |
get dropdownLanguages() { | |
return [...new Set(this.repositories.map(a => a.language))]; | |
} | |
@task | |
*submitToken() { | |
let requestOptions = { | |
method: 'GET', | |
headers: { | |
'Accept': 'application/vnd.github.v3+json', | |
'Authorization': 'token ' + this.token | |
} | |
}; | |
this.requestError = ''; | |
let response = yield fetch('https://api.github.com/orgs/'+this.organizationName+'/repos', requestOptions); | |
let repositories = yield response.json(); | |
if (!response.ok) { | |
this.requestError = 'Token or organization name is invalid.'; | |
} else { | |
let data = yield Promise.all(repositories.map(async (entry) => { | |
return await getJson('https://api.github.com/repos/'+this.organizationName+'/'+entry.name+'/branches', requestOptions); | |
})); | |
data.forEach(function(entry, index) { | |
repositories[index].branch_count = entry.length; | |
}); | |
return repositories; | |
console.log(repositories); | |
this.repositories = repositories; | |
this.filteredRepositories = repositories; | |
this.dataFetched = true; | |
let dropdownLanguages = [...new Set(repositories.map(a => a.language))]; | |
let removedNullFromLanguages = dropdownLanguages.filter(function (e) {return e != null;}); | |
let allFilter = 'All'; | |
let addAllFilter = [allFilter].concat(removedNullFromLanguages); | |
this.languages = addAllFilter; | |
} | |
} | |
} | |
async function getJson(url, options) { | |
let response = await fetch(url, options); | |
return await response.json(); | |
} |
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
{ | |
"version": "0.17.1", | |
"EmberENV": { | |
"FEATURES": {}, | |
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false, | |
"_APPLICATION_TEMPLATE_WRAPPER": true, | |
"_JQUERY_INTEGRATION": true | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js", | |
"ember": "3.18.1", | |
"ember-template-compiler": "3.18.1", | |
"ember-testing": "3.18.1" | |
}, | |
"addons": { | |
"@glimmer/component": "1.0.0", | |
"ember-concurrency": "2.0.3", | |
"ember-cached-decorator-polyfill": "0.1.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment