Skip to content
15 changes: 15 additions & 0 deletions backend/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ def send_bloom():
)


@jwt_required()
def rebloom(bloom_id):
current_user = get_current_user()
original_bloom = blooms.get_bloom(int(bloom_id))

if original_bloom is None:
return make_response(jsonify({"success": False, "message": "Bloom not found"}), 404)

blooms.add_bloom(bloom_id, current_user.username)
rebloom_count = len(original_bloom.get("reblooms", []))

return jsonify({"success": True, "rebloom_count": rebloom_count})



def get_bloom(id_str):
try:
id_int = int(id_str)
Expand Down
2 changes: 2 additions & 0 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
register,
self_profile,
send_bloom,
rebloom,
suggested_follows,
user_blooms,
)
Expand Down Expand Up @@ -57,6 +58,7 @@ def main():
app.add_url_rule("/suggested-follows/<limit_str>", view_func=suggested_follows)

app.add_url_rule("/bloom", methods=["POST"], view_func=send_bloom)
app.add_url_rule("/blooms/<bloom_id>/rebloom", methods=["POST"], view_func=rebloom)
app.add_url_rule("/bloom/<id_str>", methods=["GET"], view_func=get_bloom)
app.add_url_rule("/blooms/<profile_username>", view_func=user_blooms)
app.add_url_rule("/hashtag/<hashtag>", view_func=hashtag)
Expand Down
4 changes: 4 additions & 0 deletions front-end/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ <h2 id="bloom-form-title" class="bloom-form__title">Share a Bloom</h2>
<div class="bloom__header flex">
<a href="#" class="bloom__username" data-username>Username</a>
<a href="#" class="bloom__time"><time class="bloom__time" data-time>2m</time></a>
<button type="button" data-action="rebloom">
Re-bloom
<span data-rebloom-count></span>
</button>
</div>
<div class="bloom__content" data-content></div>
</article>
Expand Down
11 changes: 11 additions & 0 deletions front-end/lib/api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ async function postBloom(content) {
}
}

async function rebloom(bloomId) {
try {
const data = await __apiRequest(`/blooms/${bloomId}/rebloom`, { method: "POST",});

return data;
} catch (error) {
return { success: false };
}
}

// ======= USER methods
async function getProfile(username) {
const endpoint = username ? `/profile/${username}` : "/profile";
Expand Down Expand Up @@ -292,6 +302,7 @@ const apiService = {
getBlooms,
postBloom,
getBloomsByHashtag,
rebloom,

// User methods
getProfile,
Expand Down
20 changes: 20 additions & 0 deletions front-end/views/home.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {renderEach, renderOne, destroy} from "../lib/render.mjs";
import { apiService } from "../lib/api.mjs";
import {
state,
getLogoutContainer,
Expand Down Expand Up @@ -38,6 +39,25 @@ function homeView() {
"bloom-template",
createBloom
);

document.querySelectorAll("[data-action='rebloom']").forEach((button) => {
button.addEventListener("click", async () => {
const bloomArticle = button.closest("[data-bloom]");
const bloomId = bloomArticle.getAttribute("data-bloom-id");

try {
const data = await apiService.rebloom(bloomId);
const counter = button.querySelector("[data-rebloom-count]");
let count = parseInt(counter.textContent) || 0;
counter.textContent = data.rebloom_count ?? count + 1;
} catch (error) {
console.error("Unable to rebloom:", error);
}

});

});

renderOne(
state.isLoggedIn,
getBloomFormContainer(),
Expand Down