Skip to content

Commit

Permalink
feat: actual menu for deleting badges gh issue bitraf#13
Browse files Browse the repository at this point in the history
  • Loading branch information
clehre committed Jul 3, 2024
1 parent 8f047f8 commit fc2d29e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 33 deletions.
15 changes: 8 additions & 7 deletions web/src/p2k16/core/badge_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,26 @@ def badges_for_account(account_id: int) -> List[AccountBadge]:
return AccountBadge.query.join(Account, Account.id == AccountBadge.account_id).filter(Account.id == account_id).all()


def badge_is_user_made(title: str) -> bool:
super_badges = ["first_door_opening", "laser-certified"]
return title.lower() not in super_badges
def badge_is_user_made(badge: AccountBadge) -> bool:
return not badge.awarded_by.name.lower() == "system"


def remove_badge(account: Account, title: str) -> bool:
if not badge_is_user_made(title):
logger.error(f"Cannot remove non-user-made badge: {title}")
return False

badge_desc = _load_description(title)
if not badge_desc:
logger.error(f"Badge description not found for title: {title}")
return False

account_badge = AccountBadge.query.filter_by(account=account, description=badge_desc).first()
account_badge = AccountBadge.query.filter_by(account=account, description=badge_desc).all()
if not account_badge:
logger.error(f"Badge not found for account: {account.username} with title: {title}")
return False
badges = [badge for badge in account_badge if badge_is_user_made(badge)]
if len(badges) < 1:
logger.error(f"Cannot remove non-user-made badge: {title}")
return False
account_badge = badges[0]
events = Event.query.filter(Event.int1 == account_badge.id).all()
for event in events:
db.session.delete(event)
Expand Down
1 change: 0 additions & 1 deletion web/src/p2k16/web/core_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,6 @@ def _data_company_save():
###############################################################################
# HTML Pages


@core.route('/')
def index():
from .badge_blueprint import badge_description_to_json
Expand Down
65 changes: 45 additions & 20 deletions web/src/p2k16/web/static/my-profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,33 +99,58 @@ <h4 class="panel-title">Instructions</h4>

<h2 class="text-center">Awarded badges</h2>
<br>
<br>
<div class="col-sm-8 col-sm-offset-2">
<ul ng-show="ctrl.badges.length" class="list-inline list-inline-badges">
<li ng-repeat="b in ctrl.badges" ng-init="bd=ctrl.descriptions[b.description_id]">
<span ng-click="ctrl.delete_badge(bd.title)" class="label label-default">{{ bd.title }}</span>
<li ng-repeat="b in ctrl.badges" ng-init="bd=ctrl.descriptions[b.description_id]; b.showBadge=true"
style="position: relative;">
<div style="display: flex; align-items: center; width: 100%; ">
<span ng-show="b.showBadge" class="label label-default"
style="flex-grow: 1; display: flex; padding: 0rem; overflow: hidden;">
<div style="padding: .15rem;">
{{ bd.title }}
</div>
<div ng-click="b.showMenu = !b.showMenu" ng-show="b.editable && !b.showMenu"
style="margin-left: .2rem; cursor: pointer; align-items: center; margin-right: .2rem;">...</div>
<div ng-show="b.showMenu" style="display: grid; grid-template-columns: 1fr 1fr;;
padding-left: .3em; height: 100%;">
<div ng-click="b.showMenu = false; ctrl.delete_badge(bd.title); b.showBadge=false;"
style="background-color: red; border: none; width: 100%; height: 110%; cursor: pointer; padding: .1rem;">
delete</div>
<div ng-click="b.showMenu = false"
style="background-color: green; border: none; width: 100%; height: 110%; cursor: pointer;padding: .1rem;">
keep</div>
</div>
</span>
</div>
</li>
</ul>
<p ng-hide="ctrl.badges.length" class="text-center">
Aaw, you don't have any badges yet.
</p>
</div>

<div class="col-sm-12">
<hr>
</div>
</li>
</ul>
<p ng-hide=" ctrl.badges.length" class="text-center">
Aaw, you don't have any badges yet.
</p>
</div>
<div class="col-sm-12"></div>

<h2 class="text-center">Joined circles</h2>
<br>
<div class="col-sm-8 col-sm-offset-2">
<ul ng-if="ctrl.circles.length" class="list-inline list-inline-badges">
<li ng-repeat="c in ctrl.circles">
<a href="#!/admin/circle/{{c.id}}"><span class="label label-default">{{ c.name }}</span></a>
</li>
</ul>
</div>
<div class="col-sm-12">
<hr>
</div>

<div class="col-sm-12">
<hr>
</div>
<h2 class="text-center">Joined circles</h2>
<br>
<div class="col-sm-8 col-sm-offset-2">
<ul ng-if="ctrl.circles.length" class="list-inline list-inline-badges">
<li ng-repeat="c in ctrl.circles">
<a href="#!/admin/circle/{{c.id}}"><span class="label label-default">{{ c.name }}</span></a>
</li>
</ul>
</div>

<div class="col-sm-12">
<hr>
</div>

</div>
19 changes: 14 additions & 5 deletions web/src/p2k16/web/static/p2k16/p2k16.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,19 +661,20 @@
CoreDataService,
badgeDescriptions,
LabelService,
BadgeDataService
BadgeDataService,
$scope,
) {
const self = this;

P2k16.accountListeners.add($scope, (newValue) => {
updateBadges(newValue);
updateCircles(newValue);
self.badges(newValue);
});

const updateCircles = (account) =>
(self.circles = Object.values(account.circles || []));
const updateBadges = (account) =>
(self.badges = Object.values(account.badges || []));
const updateBadges = (account) => this.badge_service.badges_for_user(account.account.id).then((badges) => { this.badges = badges.data; Object.values(this.badges).forEach((badge) => badge.editable = this.isBadgeEditable(badge)) })

self.changePassword = () => {
CoreDataService.service_set_password(self.changePasswordForm).then(
Expand Down Expand Up @@ -725,14 +726,20 @@
self.badge_service.delete_badge(JSON.stringify({ title: desc })).then((res) => {
const msg = res.message || "Badge deleted";
P2k16.addInfos(msg);
// Optionally, update the badge list
const index = self.badges.findIndex(badge => badge.title === desc);
if (index !== -1) {
self.badges.splice(index, 1);
$scope.$apply();
}

updateBadges(P2k16.currentProfile());
}).catch((error) => {
console.error("Error deleting badge:", error);
P2k16.addErrors("Error deleting badge");
P2k16.addErrors(error.data.error);
});
};

self.isBadgeEditable = (desc) => { console.log(desc); return desc.awarded_by_username !== "system"; }
self.badges = [];
self.circles = [];
self.newBadge = {};
Expand All @@ -743,8 +750,10 @@
self.currentProfile = P2k16.currentProfile().account;
self.profileForm = { phone: self.currentProfile.phone, username: self.currentProfile.username };
self.isLabelActive = false;

updateBadges(P2k16.currentProfile());
updateCircles(P2k16.currentProfile());
console.log(self.badges)
self.getCurrentStatus();
}

Expand Down

0 comments on commit fc2d29e

Please sign in to comment.