-
Notifications
You must be signed in to change notification settings - Fork 280
Week1 #55
Week1 #55
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| *{ | ||
| box-sizing:border-box; | ||
| } | ||
|
|
||
| .tableData{ | ||
| font-weight:bold; | ||
|
|
||
| } | ||
| #header { | ||
| margin-top:40px; | ||
| width : 75%; | ||
| height:75px; | ||
| background-color:blue; | ||
| text-align:left; | ||
| padding-top: 25px; | ||
| border:2px solid black; | ||
| font-family: 'Times New Roman', Times, serif; | ||
| font-size: 1em; | ||
| box-shadow:10px 7px 5px 5px rgba(0, 0, 0, 0.075); | ||
| } | ||
| #selectLabel{ | ||
| padding: 2px 10px 2px 15px ; | ||
| color: white; | ||
| font-size:18px; | ||
| font-family: 'Times New Roman', Times, serif; | ||
| } | ||
| #selectID{ | ||
|
|
||
|
|
||
| width:20%; | ||
| height:30px; | ||
| font-size:18px; | ||
| font-family: 'Times New Roman', Times, serif; | ||
| padding: 2px 10px 2px 15px ; | ||
| box-shadow: 7px 5px 5px rgba(0, 0, 0, 0.075); | ||
|
|
||
| } | ||
| #tableContainer{ | ||
| background-color:white; | ||
| font-size:18px; | ||
| font-family: 'Times New Roman', Times, serif; | ||
| margin: 40px 0 0 30px; | ||
| width:30%; | ||
| height:120px; | ||
| box-shadow:10px 7px 5px 5px rgba(0, 0, 0, 0.075); | ||
| padding:5px; | ||
| } | ||
| .alert-error{ | ||
| background-color: bisque; | ||
| font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
| <link rel= "stylesheet" href="app.css"> | ||
| <title>Document</title> | ||
| </head> | ||
| <body> | ||
| <div id = "result"></div> | ||
| <script src="app1.js"></script> | ||
|
|
||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| "use strict"; | ||
| { | ||
| let fetchJSON = (URL, cb)=>{ | ||
| const dataRepositories = new XMLHttpRequest; | ||
| dataRepositories.responseType='json'; | ||
| dataRepositories.open ('GET', URL); | ||
|
|
||
| dataRepositories.onload= ()=> { | ||
| if (dataRepositories.status<400){ | ||
| (cb(null,dataRepositories.response)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is no need to surround your function call with a parentheses, the only reason you may need to do so for now is to convert your returned result into an expression. One of use cases, I know that you have seen // this will return ~> SyntaxError: expected expression, got ')'
function sayHi() {
console.log('Hi')
}()
// this will work
(function sayHi() {
console.log('Hi')
})()
// this too
(function sayHi() {
console.log('Hi')
}()) |
||
| }else { | ||
| cb(new Error(`Network error: ${dataRepositories.status} - ${dataRepositories.statusText}`)); | ||
| } | ||
| }; | ||
|
|
||
| dataRepositories.onerror= ()=>{ | ||
| cb(new Error('Network request failed')); | ||
| } | ||
| dataRepositories.send(); | ||
| }; | ||
|
|
||
| function createAppend (name, parent, options = {}){ | ||
| const elem = document.createElement(name); | ||
| parent.appendChild(elem); | ||
| Object.keys(options).forEach(key =>{ | ||
| if (key === "text"){ | ||
| const optionsKey=options[key].replace(/['"]+/g, ''); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use for replace. |
||
|
|
||
| elem.innerHTML= optionsKey; | ||
|
|
||
| }else{ | ||
| elem.setAttribute(key, options[key]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you are using so if you did that you may call it |
||
| }; | ||
| }); | ||
|
|
||
| return elem; | ||
| } | ||
|
|
||
| const URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100'; | ||
|
|
||
| function renderRepository(container, repository){ | ||
|
|
||
| container.innerHTML=''; | ||
|
|
||
| const table = createAppend('tb',container); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tables tag is not the only thing you have to do here is just to change |
||
| const tBody = createAppend('tbody',table); | ||
| const tr = createAppend('tr',tBody); | ||
| const tr1 = createAppend('tr',tBody); | ||
| const tr2 = createAppend('tr',tBody); | ||
| const tr3 = createAppend('tr',tBody); | ||
|
|
||
| createAppend('td', tr, { text:"Repository: ", class:"tableData" }); | ||
|
|
||
| const nameTag= createAppend('a',tr, {text: JSON.stringify(repository.name)}); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the only valid tag inside the <div id="tableContainer">
<tb> <!--tb is not valid HTML-->
<tbody>
<tr>
<td class="tableData">Repository: </td>
<a href="https://github.com/HackYourFuture/AngularJS" target="_blank">AngularJS</a>
</tr>
<tr>
<td class="tableData">Description: </td>
<td>Class material from angularJS module</td>
</tr>
<tr>
<td class="tableData">Fork: </td>
<td>2</td>
</tr>
<tr>
<td class="tableData">Updated: </td>
<td>4/6/2018, 7:18:06 PM</td>
</tr>
</tbody>
</tb>
</div>you may use the |
||
| nameTag.setAttribute('href', repository.html_url); | ||
| nameTag.setAttribute('target', '_blank'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can set your attributes using the |
||
|
|
||
| createAppend('td', tr1, { text:"Description: ", class:"tableData" }); | ||
| createAppend('td',tr1, {text: JSON.stringify(repository.description)}); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you think what it's useful but not here because it's just converting a string to string. |
||
|
|
||
| createAppend('td', tr2, { text:"Fork: ", class:"tableData" }); | ||
| createAppend('td',tr2, {text: JSON.stringify(repository.forks_count)}); | ||
|
|
||
| createAppend('td', tr3, { text:"Updated: ", class:"tableData" }); | ||
| let updateDate = new Date (repository.updated_at); | ||
| createAppend('td',tr3, {text: updateDate.toLocaleString()}); | ||
|
|
||
| } | ||
|
|
||
| function main(url){ | ||
|
|
||
|
|
||
| fetchJSON(url, (error, data)=>{ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
so Naming is a bit tricky sometimes, but you have to think about your names because the code today is yours but While reading it after around a year from somebody didn't met you before, he won't be able to figure out what is so for naming I suggested you to re-read this agian, if you feel really tired or lazy to read, I will tell you on sentence: |
||
|
|
||
| if (error == null){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a very long if (error == null){ // same as ~> if (!error)
const result = document.getElementById('result'); // you didn't use it but it's just an example
// execute this very loooooooong Code
// execute this very loooooooong Code
// execute this very loooooooong Code
// execute this very loooooooong Code
// execute this very loooooooong Code
// execute this very loooooooong Code
// execute this very loooooooong Code
// execute this very loooooooong Code
} else {
createAppend('div', result, { text:error.message, class: 'alert-error' })
}
console.log(result) // Oops undefinedOR you can do: // in this way I will never forgot to handle the error.
if (error != null) { // same as ~> if (error)
createAppend('div', result, { text:error.message, class: 'alert-error' })
return
}
const result = document.getElementById('result'); // you didn't use it but it's just an example
// execute this very loooooooong Code
// execute this very loooooooong Code
// execute this very loooooooong Code
// execute this very loooooooong Code
// execute this very loooooooong Code
// execute this very loooooooong Code
// execute this very loooooooong Code
// execute this very loooooooong Code
console.log(result) // the result is just right here!!the second one is the most used one and it make your code in less scoped levels, which make you have more control over it. |
||
|
|
||
| const result = document.getElementById('result'); | ||
|
|
||
| createAppend('div',result,{id:'header'}); | ||
| createAppend('label',header,{text:'HYF Repositories',id:'selectLabel'}); | ||
|
|
||
| createAppend('select',header,{class:'rep-select', id:'selectID'}); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is no need for the |
||
| const select = document.getElementById('selectID'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| const container= createAppend('div', result,{ id:'tableContainer'}); | ||
|
|
||
| data.forEach((element,index) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you did take in my advice (credit for jim I saw this in his reviews) in the naming comment. repositories.forEach((repository,index) => {/* my code */})so in this way you will really know what kind of data you are working in the |
||
| const option = createAppend('option',select,{text: element.name, value: index}); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| }); | ||
|
|
||
| select.addEventListener('change', (event)=> { | ||
| const change = data[event.target.value]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is why variables used for, and So nice variable use 💐, but you have to give better name as usual 🙃 . |
||
| renderRepository(container,change); | ||
| }); | ||
|
|
||
| renderRepository(container, data[0]); | ||
|
|
||
| }else{ | ||
| createAppend('div', result, { text:error.message, class: 'alert-error' }); | ||
| } | ||
|
|
||
| }); | ||
| } | ||
| window.onload = () => main(URL); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a constructor, I haven't saw this structure before which is working fine, So I just can't say it's valid or invalid thing, please help 🙃 @remarcmij