Skip to content

Commit

Permalink
Started work on adding functionality to editor
Browse files Browse the repository at this point in the history
(Started with the article editor)
Issue #1
  • Loading branch information
TJScalzo committed Aug 11, 2017
1 parent a03d89d commit 110c269
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 15 deletions.
15 changes: 8 additions & 7 deletions components/editArticle.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const editArticle = () => (
<div>
<form id="article">
Title: <input type="text" name="title" placeholder="Untitled"></input><br />
Author: <input type="text" name="author" placeholder="You"></input><br />
Content: <textarea name="content" rows="10" placeholder="Markdown Available"></textarea><br />
Abstract: <textarea name="abstract" rows="2" placeholder="Markdown Available"></textarea><br />
Thumbnail: <input type="text" name="thumbnail" placeholder="(Link to a photo)"></input><br />
Type: <select id="selection" name="type"><option value="article">Article</option><option value="project">Project</option><option value="user">User</option></select><br />
Title: <input id="article_title" type="text" name="title" placeholder="Titled Article"></input><br />
Author Username(s): <div id="article_authors" className="taggle_div"></div><br />
URL Slug: <input id="article_slug" type="text" name="slug" placeholder="titled-article"></input><br />
Content: <textarea id="article_content" name="content" rows="10" placeholder="Markdown Available"></textarea><br />
Abstract: <textarea id="article_abstract" name="abstract" rows="2" placeholder="Markdown Available"></textarea><br />
Thumbnail: <input id="article_thumbnail" type="text" name="thumbnail" placeholder="(Link to a photo)"></input><br />
Type: <select id="article_type" name="type"><option value="article">Article</option><option value="project">Project</option><option value="user">User</option></select><br />
Tags: <div id="article_tags" className="taggle_div"></div><br />
Needed/Related Tags: <div id="article_needed_tags" className="taggle_div"></div><br />

<input type="submit" value="Submit"></input>
<input id="article_submit" type="button" value="Submit"></input>
</form>
</div>
)
Expand Down
7 changes: 7 additions & 0 deletions database.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function articleExists(slug, callback) {
callback(exists);
});
}
exports.articleExists = articleExists;

exports.getFile = function(fid) {
return knex('files').where({id: fid}).select('*');
Expand All @@ -67,17 +68,23 @@ function fileExists(slug, callback) {
callback(exists);
});
}
exports.fileExists = fileExists;

exports.getUser = function(uid) {
return knex('users').where({'users.id': uid}).select('users.username', 'users.first_name', 'users.last_name', 'users.article_id', 'users.profile_picture', 'articles.url_slug').leftJoin('articles', 'users.article_id', 'articles.id');
}

exports.getUserIDByUsername = function(username) {
return knex('users').where({username: username}).select('id');
}

function userExists(username, callback) {
knex('users').where({username: username}).select('*').then(function(user) {
var exists = (user.length > 0);
callback(exists);
});
}
exports.userExists = userExists;


// Functions to edit info in the database:
Expand Down
40 changes: 32 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,49 @@ app.prepare()
});
});

server.get('/api/users/:id', function (req, res) {
server.get('/api/users/id/:id', function (req, res) {
db.getUser(req.params.id).then(function(user) {
res.json(user);
});
});

server.get('/api/users/username/:username', function (req, res) {
db.getUserIDByUsername(req.params.username).then(function(userID) {
res.json(userID);
});
});

server.get('/api/files', function (req, res) {
db.allFiles().then(function(files) {
res.json(files);
});
});

// server.post('/api/createuser', urlencodedParser, function (req, res) {
// req.body.first_name
//
// db.allArticles().then(function(articles) {
// res.json(articles);
// });
// });
server.post('/api/edit/article', function (req, res) {
console.log(req.body);

// db.editArticle()

res.send("Success!");
});

server.get('/api/exists/article/:slug', function (req, res) {
db.articleExists(req.params.slug, function(exists) {
res.send(exists);
})
});

server.get('/api/exists/user/:username', function (req, res) {
db.userExists(req.params.username, function(exists) {
res.send(exists);
})
});

server.get('/api/exists/file/:slug', function (req, res) {
db.fileExists(req.params.slug, function(exists) {
res.send(exists);
})
});

server.use(express.static("static"));

Expand Down
65 changes: 65 additions & 0 deletions static/scripts/editScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,71 @@ document.getElementById('selection').onchange = function() {
document.getElementById(selection).removeAttribute('hidden');
};

var article_authors = new Taggle("article_authors");
var article_tags = new Taggle("article_tags");
var article_needed_tags = new Taggle("article_needed_tags");
var file_tags = new Taggle("file_tags");

document.getElementById('article_submit').onclick = attemptSubmitArticle;

function getAuthorIDs() {
var authors = squash(article_authors.getTagValues());
var authorIDs = [];

for (var n in authors) {
var authorRequest = new XMLHttpRequest();
authorRequest.open("GET", "http://localhost:3000/api/users/username/" + authors[n], false);
authorRequest.send( null );
console.log(authorRequest.responseText);
if (authorRequest.responseText == "[]") {
window.alert(authors[n] + " isn't a legit user, yo!");
return undefined;
}
authorIDs.push(authorRequest.responseText);
}
return authorIDs;
}

function attemptSubmitArticle() {
var authorIDs = getAuthorIDs();
if (authorIDs = undefined) {
return false;
}
var slug = document.getElementById("article_slug").value;

var slugRequest = new XMLHttpRequest();
slugRequest.open("GET", "http://localhost:3000/api/exists/article/" + slug, false);
slugRequest.send( null );
if (slugRequest.responseText == "true") {
window.alert(slug + " is already being used!");
return false;
} else {
var data = {};

data.title = document.getElementById('article_title').value;
data.content = document.getElementById('article_content').value;
data.abstract = document.getElementById('article_abstract').value;
data.thumbnail = document.getElementById('article_thumbnail').value;
var type_selector = document.getElementById('article_type');
data.type = type_selector.options[type_selector.selectedIndex].value;
data.tags = squash(article_tags.getTagValues());
data.needed_tags = squash(article_needed_tags.getTagValues());

var articleRequest = new XMLHttpRequest();
articleRequest.open("POST", "http://localhost:3000/api/edit/article", false);
articleRequest.setRequestHeader("Content-type", "application/json");
articleRequest.send(data);
window.alert(articleRequest.responseText);
}
}

// This removes duplicates from arrays
function squash(arr) {
var tmp = [];
for (var i = 0; i < arr.length; i++) {
if (tmp.indexOf(arr[i]) == -1) {
tmp.push(arr[i]);
}
}
return tmp;
}

0 comments on commit 110c269

Please sign in to comment.