Skip to content

AJAX Front End

jha8 edited this page Mar 5, 2021 · 20 revisions

Getting Non-Followers

/service/author/{AUTHOR_ID}/nonfollowers/ to retrieve a json response containing users that the logged in user may follow/send a friend request to

  • Request method: GET

function fetchJSON(url) {
            var request = new Request(url);
            return fetch(request).then((response) => {
                if (response.status === 200 || response.status == 404) { 
                    return response.json(); // return a Promise
                } else {
                    alert("Something went wrong: " + response.status);
                }
            });
        }

Getting Followers

/service/author/{AUTHOR_ID}/followers/ to retrieve a json response containing users that are following the logged in user (sending a friend request to the logged in user)

  • Request method: GET

function fetchJSON(url) {
            var request = new Request(url);
            return fetch(request).then((response) => {
                if (response.status === 200 || response.status == 404) { 
                    return response.json(); // return a Promise
                } else {
                    alert("Something went wrong: " + response.status);
                }
            });
        }

Getting Friends

/service/author/{AUTHOR_ID}/friends/ to retrieve a json response containing users that are friends with the logged in user (both users are following each other)

  • Request method: GET

function fetchJSON(url) {
            var request = new Request(url);
            return fetch(request).then((response) => {
                if (response.status === 200 || response.status == 404) { 
                    return response.json(); // return a Promise
                } else {
                    alert("Something went wrong: " + response.status);
                }
            });
        }

Getting Friends

/service/author/{AUTHOR_ID}/friends/ to retrieve a list of users that are following the logged in user (that are friend requesting them)

  • Request method: GET

function fetchJSON(url) {
            var request = new Request(url);
            return fetch(request).then((response) => {
                if (response.status === 200 || response.status == 404) { 
                    return response.json(); // return a Promise
                } else {
                    alert("Something went wrong: " + response.status);
                }
            });
        }

Following user

/service/author/{AUTHOR_ID}/followers/{FOREIGN_AUTHOR_ID}/ to follow a user/friend request a user

  • Request method: PUT -!important: in this instance {AUTHOR_ID} is the author the logged in user is following and {FOREIGN_AUTHOR_ID} is the current logged in users id
follow(url).done(function(response) {
                console.log(response);
            });

function follow(url){
            return $.ajax({
                url: url,
                type: 'PUT',
                contentType: "application/json",
                success: function(result) {
                    console.log(result)
                   
                }
            })
        }

Following a user back

/service/author/{AUTHOR_ID}/followers/{FOREIGN_AUTHOR_ID}/ to follow a user/friend request a user back

  • Request method: PUT
  • !important: in this instance {AUTHOR_ID} is the author the logged in user is following and {FOREIGN_AUTHOR_ID} is the current logged in users id
AddFollowerBack(url).done(function(response) {
                console.log(response);
            });

function AddFollowerBack(url){
            return $.ajax({
                url: url,
                type: 'PUT',
                contentType: "application/json",
                success: function(result) {
                    console.log(result)
                }})
        }

**Deleting a friend <:-( **

/service/author/{AUTHOR_ID}/followers/{FOREIGN_AUTHOR_ID}/ to remove a friend from a friend list (also removes friends from followers)

/service/author/{FOREIGN_AUTHOR_ID}/followers/{AUTHOR_ID}/ to remove a friend from a friend list (also removes friends from followers)

  • Request method: PUT
  • !important: in this instance {AUTHOR_ID} is the author the logged in user is following and {FOREIGN_AUTHOR_ID} is the current logged in users id
  • !important: calls must be made to both url listed above

function remove(url){
            $.ajax({
                url: url,
                type: 'DELETE',
                contentType: "application/json",
                success: function(result) {
                    console.log(result)
                }
            })
        }

Getting All Friends a User Has

/service/author/{AUTHOR_ID}/friends/ to retrieve a list of users the author who is logged in may follow (requires authorization)

  • Request method: GET
function getFriends() {
    var uuid = "{{uuid}}";
    var pre_url = "/service/author/"
    var url = pre_url + uuid + "/friends/" 
    fetchJSON(url).then((json) => { //another callback
        try{
            //https://www.geeksforgeeks.org/how-to-combine-multiple-elements-and-append-the-result-into-a-div-using-javascript/
            for (var i = 0; i < json.items.length; i++) { 
                // console.log(json.items[i].displayName)
                // console.log(json.items[i].id)
                $("#friends-select")
                .append('<option value="'+ json.items[i].id +'">' + json.items[i].displayName + '</option>');
                } 
            }catch(err){
                // console.log(uuid)
                $("#friends-select") 
                .append('<option value="public">Public</option>');
            }
        });
}

Update an existing post

/service/author/{AUTHOR_ID}/posts/{POST_ID} will update the existing post that the author made.(requires authorization)

  • Request method: PUT
function updatePost() {
    var uuid = "{{ uuid }}";
    var pre_url = "/service/author/";
    var url = pre_url + uuid + "/posts/";
    url += "{{ post_id }}/";
    var title = document.getElementById("title-name").value;
    var content = document.getElementById("post-textarea").value;
    if (title === ""){
        alert("Don't submit an empty post");
        return false;
    }
    var visibility = document.getElementById("post-visibility").value;
    var markdown;
    if(document.getElementById('btnradio1').checked){
        markdown = document.getElementById('btnradio1').value;
    }
    if(document.getElementById('btnradio2').checked){
        markdown = document.getElementById('btnradio2').value;
    }
    markdown = "false";
    var shared = document.getElementById("friends-select").value;
    var categories_field = $("#categories");
    var categories;
    if (categories_field.is(":hidden")){
        categories = "";
    } else {
        categories = document.getElementById('categories').value;
    }

    var mJson = {
            title: title,
            description: "mock desc",
            categories: categories,
            content: content,
            origin: window.location.origin,
            visibility: visibility,
            shared_with: shared,
    };
    $.ajax({
        type: 'PUT',
        url: url,
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(mJson),
        success: function(result) {
            location.reload();
        }
    });
}

Delete existing post

/service/author/{AUTHOR_ID}/posts/{POST_ID} will delete the existing post that the author made.(requires authorization)

  • Request method: DELETE
function deletePost() {
    var uuid = "{{ uuid }}";
    var pre_url = "/service/author/";
    var url = pre_url + uuid + "/posts/";
    url += "{{ post_id }}/";
    $.ajax({
        type: 'DELETE',
        url: url,
        contentType: "application/json",
        success: function(result) {
            window.location.href = "{% url 'home_url'%}"
        }
    });
}

Clone this wiki locally