Editing a Reddit Post

Here we go - the biggy. The U in CRUD.😟😟


Update


Update Is the hardest part of CRUD for a couple reasons, but as I am an API and only focusing on client-side development things should be a little easier.

The main challenge is visually creating an application that previews the current status of an item to be edited, and then an updated version once you complete the request. 

For this, I have used a series of buttons, with some handy JQuery helpers to assist.


Once the dynamically generated button is pressed, then the button is located and replaced with the 'confirm' button using JQuery's '.replaceWith()'.

The body of the post (or 'selftext') is then found, and replaced with a form. This form contains an input box with the original body of the post as a value. 

From here, the user can change the text and press confirm, which will trigger the ajax request.


As shown above, the new user text is taken from the input box with the id of 'editText' and assigned to a variable. The id, or fullname, of the item is also taken from the button's id. 

The header's are set once again, and the request is made to api/editusertext. For this request to function, it is essential that the id of the post is sent, as well as the new text. 

Once this request is successful, the console will send a success message.

On top of this, the confirm button will be switched back to the edit button for future editing, as well as the replacement of the form back to a paragraph.

There you have it, full CRUD of a subreddit's posts using the Reddit API! Not so scary after all!  




Comments

Popular posts from this blog

Wordpress Intro & Child Themes

Introduction

Creating a Reddit post