Skip to content
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

Implement async-await on client-side #15

Merged
merged 2 commits into from
Nov 5, 2016
Merged
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
56 changes: 20 additions & 36 deletions src/src/App.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// Core
import React, { Component } from 'react';

// Helpers
import uuid from 'uuid';
import Codemirror from 'react-codemirror';
import { File } from './File';
import getFileContent from './utils/getFileContent';

import 'codemirror/lib/codemirror.css'
// UI
import './App.css';

import { File } from './File';
import Codemirror from 'react-codemirror';
import 'codemirror/lib/codemirror.css'
import 'codemirror/mode/javascript/javascript';

class App extends Component {

constructor() {
super();
this.state = {
Expand Down Expand Up @@ -44,39 +47,18 @@ class App extends Component {
}
}

Uint8ToString(u8a) {
var CHUNK_SZ = 0x8000;
var c = [];
for (var i=0; i < u8a.length; i+=CHUNK_SZ) {
c.push(String.fromCharCode.apply(null, u8a.subarray(i, i+CHUNK_SZ)));
}
return c.join("");
}
async handleFileClick(e) {
const fileName = e.target.textContent;

handleFileClick(e) {
let self = this;
this.setState({
selectedName: e.target.textContent
});
fetch('http://localhost:5000/content/' + encodeURIComponent(e.target.textContent), {
method: 'GET', headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type,x-requested-with,Authorization,Access-Control-Allow-Origin',
'Access-Control-Allow-Credentials': 'true',
'Content-Type': 'application/json'
},
mode: 'cors'
}).then((response) => {
var reader = response.body.getReader();
return reader.read();
}).then((result, done) => {
if (!done) {
var u8 = new Uint8Array(result.value);
self.setState({
content: self.Uint8ToString(u8)
});
}
try {
const fileContent = await getFileContent(fileName);
this.setState({
selectedName: fileName,
content: fileContent
});
} catch (error) {
console.error('error', error);
}
}

updateCode(newCode) {
Expand Down Expand Up @@ -109,6 +91,7 @@ class App extends Component {
<div className="Header">
ZİYA
</div>

<div id="Sidebar">
{
this.state.files.map((item) => (
Expand All @@ -121,6 +104,7 @@ class App extends Component {
))
}
</div>

<div id="Content" className={this.state.content ? '' : 'hidden'}>
<Codemirror
className="Editor"
Expand Down
8 changes: 0 additions & 8 deletions src/src/App.test.js

This file was deleted.

5 changes: 5 additions & 0 deletions src/src/constants/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const config = {
serverURL: 'http://localhost:5000',
};

export default config;
7 changes: 0 additions & 7 deletions src/src/logo.svg

This file was deleted.

12 changes: 12 additions & 0 deletions src/src/utils/Uint8ToString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function Uint8ToString(u8a) {
const CHUNK_SZ = 0x8000;
const c = [];

for (let i=0; i < u8a.length; i+=CHUNK_SZ) {
c.push(String.fromCharCode.apply(null, u8a.subarray(i, i+CHUNK_SZ)));
}

return c.join('');
}

export default Uint8ToString;
28 changes: 28 additions & 0 deletions src/src/utils/getFileContent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import config from '../constants/config';
import Uint8ToString from './Uint8ToString';

async function getFileContent(fileName) {
const encodedName = encodeURIComponent(fileName);

const requestOptions = {
method: 'GET',
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type,x-requested-with,Authorization,Access-Control-Allow-Origin',
'Access-Control-Allow-Credentials': 'true',
'Content-Type': 'application/json'
},
mode: 'cors'
};

try {
const response = await fetch(`${config.serverURL}/content/${encodedName}`, requestOptions);
const readedResult = await response.body.getReader().read();
const u8 = new Uint8Array(readedResult.value)
return Uint8ToString(u8);
} catch (e) {
console.error('Error occured on getFileContent:', e);
}
}

export default getFileContent;