ok changing everything again, decided to simplify the Friendship.entity, so i made a class that returns the stuff we need from a Friendship

This commit is contained in:
Me
2022-12-25 05:53:29 +01:00
parent 2b5f8a9667
commit b7b9b3d645
10 changed files with 251 additions and 137 deletions

View File

@@ -1,7 +1,7 @@
<script lang="ts">
import { onMount } from 'svelte';
import GenerateUserDisplay from '../../pieces/GenerateUserDisplay.svelte';
import GenerateUserDisplay from '../../pieces/GenerateUserDisplay.svelte';
let user;
@@ -20,7 +20,7 @@
<div class="outer">
<!-- OHHHH i could use #await instead of if and have an nice loading page! -->
{#if user !== undefined}
<GenerateUserDisplay {user} primary={true}/>
<GenerateUserDisplay user={user} primary={true}/>
{:else}
<!-- might be unnecessary since you can't access the page without fetching the user -->
<h2>Sorry</h2>

View File

@@ -14,7 +14,7 @@
let myFriends;
let requestsMade, requestsRecieved;
let blockedUsers;
let usernameBeingViewed;
let userIdBeingViewed;
let friendshipStatusFull; // id, date, senderUsername, reveiverUsername, status
/**** Layout variables ****/
@@ -60,7 +60,7 @@
};
const fetchMyFriends = async() => {
myFriends = await fetch('http://transcendance:8080/api/v2/network/myfriends')
myFriends = await fetch('http://transcendance:8080/api/v2/network/relations')
.then( x => x.json() );
console.log('got my friends ')
console.log({...myFriends})
@@ -90,35 +90,36 @@
/**** END OF MAIN FETCH ****/
// returns everything but BLOCKED
const fetchFriendshipFull = async(aUsername) => {
const fetchFriendshipFull = async(aUserId) => {
console.log('fetch friendship from a username')
console.log(aUsername)
friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${aUsername}`)
console.log(aUserId)
friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/relations/${aUserId}`)
.then( x => x.json());
console.log({...friendshipStatusFull})
};
const sendFriendRequest = async(aUsername) => {
const resp = await fetch("http://transcendance:8080/api/v2/network/myfriends", {
const sendFriendRequest = async(aUser) => {
const resp = await fetch("http://transcendance:8080/api/v2/network/relations", {
method : "POST",
headers: { 'Content-Type': 'application/json'},
body: JSON.stringify({
"receiverUsername": aUsername,
"receiverUsername": aUser.username,
"receiverId": aUser.id,
"status": "R"
})
})
.then( x => x.json())
fetchFriendshipFull(aUsername);
fetchFriendshipFull(aUser.id);
};
const viewAUser = async(aUsername) => {
const viewAUser = async(aUserId) => {
console.log('Profile Friend updating userBeingViewed')
usernameBeingViewed = aUsername;
userIdBeingViewed = aUserId;
// friendshipStatusFull = undefined;
// id, date, senderUsername, reveiverUsername, status
await fetchFriendshipFull(aUsername);
await fetchFriendshipFull(aUserId);
console.log('Friendship Status Full')
console.log({...friendshipStatusFull})
@@ -128,13 +129,13 @@
const acceptFriendRequest = async(relationshipId) => {
console.log('accept friend request')
// PATCH http://transcendance:8080/api/v2/network/myfriends/:relationshipId/accept
const resp = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${relationshipId}/accept`, {
const resp = await fetch(`http://transcendance:8080/api/v2/network/relations/${relationshipId}/accept`, {
method: "PATCH"})
.then( x => x.json());
// maybe not the most robust things, not super reusable cuz it depends on outside vars but works for now...
console.log('accepted friend request, now response')
console.log({...resp})
await fetchFriendshipFull(usernameBeingViewed);
await fetchFriendshipFull(userIdBeingViewed);
// will this make it reload? C'est un peu bourain... do i even need it?
activeTabItem = activeTabItem;
@@ -143,24 +144,24 @@
const declineFriendRequest = async(relationshipId) => {
console.log('decline friend request')
// PATCH http://transcendance:8080/api/v2/network/myfriends/:relationshipId/decline
const resp = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${relationshipId}/decline`, {
const resp = await fetch(`http://transcendance:8080/api/v2/network/relations/${relationshipId}/decline`, {
method: "PATCH"})
.then( x => x.json());
// maybe not the most robust things, not super reusable cuz it depends on outside vars but works for now...
console.log('declined friend request, now response')
console.log({...resp})
await fetchFriendshipFull(usernameBeingViewed);
await fetchFriendshipFull(userIdBeingViewed);
};
const unfriend = async(relationshipId) => {
console.log('Unfriend')
const resp = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${relationshipId}`, {
const resp = await fetch(`http://transcendance:8080/api/v2/network/relations/${relationshipId}`, {
method: "DELETE"})
.then( x => x.json());
// friendshipStatusFull = undefined;
// OR
await fetchFriendshipFull(usernameBeingViewed);
await fetchFriendshipFull(userIdBeingViewed);
if (Object.keys(friendshipStatusFull).length === 0)
friendshipStatusFull = undefined;
@@ -168,22 +169,23 @@
activeTabItem = activeTabItem;
};
const blockANonFriendUser = async(aUsername) => {
const blockANonFriendUser = async(aUser) => {
console.log('Block a non friend user, their username')
console.log(aUsername)
console.log({...aUser})
const blockResp = await fetch("http://transcendance:8080/api/v2/network/myfriends", {
const blockResp = await fetch("http://transcendance:8080/api/v2/network/relations", {
method : "POST",
headers: { 'Content-Type': 'application/json'},
body: JSON.stringify({
"receiverUsername": aUsername,
"receiverUsername": aUser.username,
"receiverId": aUser.id,
"status": "B"
})
})
.then( x => x.json())
await fetchBlockedUsers();
await fetchAllUsers();
usernameBeingViewed = undefined;
userIdBeingViewed = undefined;
friendshipStatusFull = undefined;
// will this make it reload?
@@ -193,13 +195,13 @@
const blockAFriend = async(relationshipId) => {
console.log('blocking a friend, the relationshipID')
console.log(relationshipId)
const resp = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${relationshipId}/block`, {method: "PATCH"})
const resp = await fetch(`http://transcendance:8080/api/v2/network/relations/${relationshipId}/block`, {method: "PATCH"})
.then( x => x.json() );
console.log('blocked a user response')
console.log({...resp})
await fetchBlockedUsers();
await fetchAllUsers();
usernameBeingViewed = undefined;
userIdBeingViewed = undefined;
friendshipStatusFull = undefined;
// will this make it reload?
@@ -250,9 +252,9 @@
{/if}
<!-- does this work? -->
<!-- {#each allUsers as aUser (aUser.username)} -->
<!-- {#each allUsers as aUser (aUser.id)} -->
{#each allUsers as aUser}
<div class="sidebar-item" on:click={() => viewAUser(aUser.username)}>{aUser.username}</div>
<!-- {#each allUsers as aUser} -->
{#each allUsers as aUser (aUser.id)}
<div class="sidebar-item" on:click={() => viewAUser(aUser.id)}>{aUser.username}</div>
<!-- i could make an indicator component? like green for connected or something?
i could use words but color them?
i could make it so if they're in a game -->
@@ -266,7 +268,7 @@
{/if}
<!-- {#each myFriends as aUser (aUser.id)} -->
{#each myFriends as aUser}
<div class="sidebar-item" on:click={() => viewAUser(aUser.username)}>{aUser.username}</div>
<div class="sidebar-item" on:click={() => viewAUser(aUser.id)}>{aUser.username}</div>
<div class="status sidebar-item">{aUser.status}</div>
<br>
{/each}
@@ -298,8 +300,9 @@
<div class="main-display">
{#if usernameBeingViewed !== undefined}
<DisplayAUser aUsername={usernameBeingViewed}/>
{#if userIdBeingViewed !== undefined}
<!-- <DisplayAUser aUsername={usernameBeingViewed}/> -->
<DisplayAUser aUserId={userIdBeingViewed}/>
<div class="buttons-area">
{#if friendshipStatusFull && friendshipStatusFull.id}
@@ -330,8 +333,8 @@
{/if}
{/if}
{:else}
<Button type="secondary" on:click={() => sendFriendRequest(usernameBeingViewed)}>Add Friend</Button>
<Button on:click={() => blockANonFriendUser(usernameBeingViewed)}>Block User</Button>
<Button type="secondary" on:click={() => sendFriendRequest(userIdBeingViewed)}>Add Friend</Button>
<Button on:click={() => blockANonFriendUser(userIdBeingViewed)}>Block User</Button>
{/if}
</div>
{:else}

View File

@@ -3,13 +3,15 @@
import { onMount, afterUpdate } from 'svelte';
import GenerateUserDisplay from './GenerateUserDisplay.svelte';
export let aUsername;
// export let aUsername;
export let aUserId;
let user;
onMount( async() => {
console.log('Display aUser username: '+ aUsername)
// console.log('Display aUser username: '+ aUsername)
// http://transcendance:8080/api/v2/user?username=NomDuUserATrouver
user = await fetch(`http://transcendance:8080/api/v2/user?username=${aUsername}`)
// user = await fetch(`http://transcendance:8080/api/v2/user?username=${aUsername}`)
user = await fetch(`http://transcendance:8080/api/v2/user?id=${aUserId}`)
.then( (x) => x.json() );
// console.log('Display a user: ')
@@ -27,13 +29,15 @@
export const updateUser = async() => {
console.log('Display Update aUser username: '+ aUsername)
// console.log('Display Update aUser username: '+ aUsername)
// http://transcendance:8080/api/v2/user?username=NomDuUserATrouver
user = await fetch(`http://transcendance:8080/api/v2/user?username=${aUsername}`)
// user = await fetch(`http://transcendance:8080/api/v2/user?username=${aUsername}`)
user = await fetch(`http://transcendance:8080/api/v2/user?id=${aUserId}`)
.then( (x) => x.json() );
};
$: aUsername, updateUser();
// $: aUsername, updateUser();
$: aUserId, updateUser();
</script>
@@ -45,7 +49,7 @@
<!-- <GenerateUserDisplay user={user} primary={true}/> -->
{:else}
<h2>Sorry</h2>
<div>Failed to load user {aUsername}</div>
<div>Failed to load user {aUserId}</div>
{/if}