diff options
-rw-r--r-- | api/social/create_relation.py | 2 | ||||
-rw-r--r-- | api/social/destroy_relation.py | 31 | ||||
-rw-r--r-- | pages/user.tsx | 69 |
3 files changed, 79 insertions, 23 deletions
diff --git a/api/social/create_relation.py b/api/social/create_relation.py index 7451b6d..eded25d 100644 --- a/api/social/create_relation.py +++ b/api/social/create_relation.py @@ -14,7 +14,7 @@ def create_relation(user_1_id, user_2_id, relation_type): def remove_relation(user_1_id, user_2_id): cursor.execute("delete from social where user_1_id = ? and user_2_id = ?", [user_1_id, user_2_id]) - connection.commit + connection.commit() def create_relation_route(relation_type): def route(): diff --git a/api/social/destroy_relation.py b/api/social/destroy_relation.py new file mode 100644 index 0000000..1107024 --- /dev/null +++ b/api/social/destroy_relation.py @@ -0,0 +1,31 @@ +from flask import Blueprint, request +from db import cursor +from auth.login_token import token_login +from social.create_relation import remove_relation +from user.info import get_relation_to +import time + +remove = Blueprint('remove', __name__) + +@remove.route('/remove', methods = ['POST']) +def index(): + data = request.get_json() + + user_2_id = data.get("id") or "" + token = request.cookies.get("token") or "" + + if not token: return "", 401 + user_1_id = token_login(token) or "" + + if not user_1_id or \ + not user_2_id: + return "", 403 + + relation = get_relation_to(user_1_id, user_2_id) + if relation == "none": return 403 + + remove_relation(user_1_id, user_2_id) + return "", 200 + +dynamic_route = ["/social", remove] + diff --git a/pages/user.tsx b/pages/user.tsx index a33f58b..8d4fb8d 100644 --- a/pages/user.tsx +++ b/pages/user.tsx @@ -25,7 +25,8 @@ import { mdiClipboardTextOutline, mdiGamepadSquareOutline, mdiEarth, - mdiAccountMinusOutline } from '@mdi/js'; + mdiAccountMinusOutline, + mdiAccountRemoveOutline } from '@mdi/js'; function InfoModule(props: { label: string; @@ -217,36 +218,60 @@ export default function AccountPage() { }); }}/> })()} - { relation == "friends" ? - <IconLabelButton icon={<Icon size={1} path={mdiAccountMinusOutline}/>} text="Vriend verwijderen" onclick={() => { - /* axios.request({ */ - /* method: "post", */ - /* url: `/api/social/request`, */ - /* headers: {"content-type": "application/json"}, */ - /* data: { "id": user?.id } */ - /* }) */ - /* .then(() => { */ - /* toast("Vriendschapsverzoek gestuurd", */ - /* "confirmation", */ - /* <PersonAddOutlinedIcon style={{ fontSize: 32 }}/>); */ - /* setIsFriends(true); */ - /* }); */ - }}/> : - <IconLabelButton icon={<PersonAddOutlinedIcon/>} text="Vriendschapsverzoek" onclick={() => { + {(() => { + var icon = { + "friends": <Icon size={1} path={mdiAccountMinusOutline}/>, + "outgoing": <Icon size={1} path={mdiAccountRemoveOutline}/>, + "incoming": <PersonAddOutlinedIcon/> + }[relation] || <PersonAddOutlinedIcon/> + + var text = { + "friends": "Vriend verwijderen", + "outgoing": "Vriendschapsverzoek annuleren", + "incoming": "Vriendschapsverzoek accepteren" + }[relation] || "Vriendschapsverzoek sturen" + + return <IconLabelButton icon={icon} text={text} onclick={() => { + var nextRelation = { + "friends": { + "endpoint": "/api/social/remove", + "action": `${user.username} succesvol verwijderd als vriend`, + "relation": "none", + "icon": <Icon size={32 / 24} path={mdiAccountMinusOutline}/>, + }, + "outgoing": { + "endpoint": "/api/social/remove", + "action": `Vriendschapsverzoek naar ${user.username} geannuleerd`, + "relation": "none", + "icon": <Icon size={32 / 24} path={mdiAccountMinusOutline}/>, + }, + "incoming": { + "endpoint": "/api/social/accept", + "action": `Vriendschapsverzoek van ${user.username} geaccepteerd`, + "relation": "friends", + "icon": <PersonAddOutlinedIcon/>, + }, + }[relation] || { + "endpoint": "/api/social/request", + "action": `Vriendschapsverzoek gestuurd naar ${user.username}`, + "relation": "outgoing", + "icon": <PersonAddOutlinedIcon/>, + } + axios.request({ method: "post", - url: `/api/social/request`, + url: nextRelation.endpoint, headers: {"content-type": "application/json"}, data: { "id": user?.id } }) .then(() => { - toast("Vriendschapsverzoek gestuurd", + toast(nextRelation.action, "confirmation", - <PersonAddOutlinedIcon style={{ fontSize: 32 }}/>); - setRelation("outgoing"); + nextRelation.icon); + setRelation(nextRelation.relation); }); }}/> - } + })()} </div> }</div>} </div> |