Skip to content

Commit

Permalink
Merge pull request #16 from MLH-Fellowship/development
Browse files Browse the repository at this point in the history
ADD CMS content regex validation and server-side error handling
  • Loading branch information
MAMV3x3 authored Aug 8, 2023
2 parents f4f542c + f73fd2b commit b18f19a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
31 changes: 27 additions & 4 deletions app/static/scripts/addblogpost.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const titleField = document.getElementById('title');
const imageUrlField = document.getElementById('image_url');
const contentField = document.getElementById('myMarkdownEditor');
const categoryField = document.getElementById('category');
const requestError = document.getElementById('blog-request-error');
const form = document.getElementById('blogPostForm');

function validateFormInput(input, errorElement, validationRegex, requiredMessage, invalidMessage) {
Expand All @@ -20,11 +21,19 @@ function validateFormInput(input, errorElement, validationRegex, requiredMessage
return true;
}

function validateMarkdownContent(input, errorElement, requiredMessage) {
errorElement.innerHTML = '';
if (!input.value.trim()) {
errorElement.innerHTML = requiredMessage;
return false;
}
return true;
}

function checkFormValidity() {
var valid = true;
const titleRegex = /^.+$/; // Adjust as needed
const urlRegex = /^https?:\/\/[^ \n]+$/i;
const contentRegex = /^.+$/;
const categoryRegex = /^.+$/;

valid &= validateFormInput(
Expand All @@ -51,6 +60,12 @@ function checkFormValidity() {
'Category is invalid'
);

valid &= validateMarkdownContent(
contentField,
document.getElementById('blog-content-error'),
'Content is required'
);

return valid;
}

Expand All @@ -64,8 +79,14 @@ function createPost(title, imageUrl, category) {
})
.then(response => {
if (!response.ok) {
console.log(response);
return response.text();
switch (response.status) {
case 429:
throw new Error('Too many requests');
case 500:
throw new Error('Internal server error');
default:
throw new Error('Unknown error');
}
}
return response.json();
})
Expand All @@ -76,11 +97,13 @@ function createPost(title, imageUrl, category) {
titleField.value = '';
imageUrlField.value = '';
categoryField.value = '';
requestError.innerHTML = '';
simplemde.value('');
alert('Post created successfully!');
})
.catch(e => {
console.log('There was a problem with the request:', e.message);
console.error(error);
requestError.innerHTML = error.message;
});
}

Expand Down
1 change: 1 addition & 0 deletions app/templates/cms.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ <h2 class="section-title">Create New Post</h2>
<textarea id="myMarkdownEditor" name="content" rows="5" cols="33"></textarea>
<span id="blog-content-error" class="form-error-message"></span>
</div>
<span id="blog-request-error" class="form-error-message request"></span>
<button id="addBlogPost" class="send-form-button">Add Post</button>
</form>
</div>
Expand Down

0 comments on commit b18f19a

Please sign in to comment.