Skip to content

async code refactor #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions __mocks__/axios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const get = url => {
return Promise.resolve({ data: { title: 'delectus aut autem' } });
};

exports.get = get;
5 changes: 5 additions & 0 deletions __mocks__/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const fetchData = () => {
return Promise.resolve({ title: 'delectus aut autem' });
};

exports.fetchData = fetchData;
32 changes: 5 additions & 27 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
const { generateText, createElement, validateInput } = require('./util');

const initApp = () => {
// Initializes the app, registers the button click listener
const newUserButton = document.querySelector('#btnAddUser');
newUserButton.addEventListener('click', addUser);
};
const { printTitle } = require('./util');

const addUser = () => {
// Fetches the user input, creates a new HTML element based on it
// and appends the element to the DOM
const newUserNameInput = document.querySelector('input#name');
const newUserAgeInput = document.querySelector('input#age');
const button = document.querySelector('button');

if (
!validateInput(newUserNameInput.value, true, false) ||
!validateInput(newUserAgeInput.value, false, true)
) {
return;
}

const userList = document.querySelector('.user-list');
const outputText = generateText(
newUserNameInput.value,
newUserAgeInput.value
);
const element = createElement('li', outputText, 'user-item');
userList.appendChild(element);
};

// Start the app!
initApp();
button.addEventListener('click', printTitle);

exports.printTitle = printTitle;
329 changes: 325 additions & 4 deletions dist/main.js

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const axios = require('axios');

const fetchData = () => {
console.log('Fetching data...');
return axios
.get('https://jsonplaceholder.typicode.com/todos/1')
.then(response => {
return response.data;
});
};

exports.fetchData = fetchData;
16 changes: 1 addition & 15 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,7 @@
</head>

<body>
<section class="control-panel">
<div class="input-container">
<label for="name">Name</label>
<input type="text" id="name">
</div>
<div class="input-container">
<label for="age">Age</label>
<input type="number" id="age">
</div>
<button id="btnAddUser" class="button">Add User</button>
</section>
<hr>
<section class="user-output">
<ul class="user-list"></ul>
</section>
<button>Get Post Title!</button>
<script src="dist/main.js"></script>
</body>

Expand Down
Loading