mirror of
https://github.com/versia-pub/frontend.git
synced 2026-03-13 03:29:16 +01:00
feat: ✨ Add ability to follow and unfollow users
This commit is contained in:
parent
8c68957df8
commit
68e5ede6c6
3 changed files with 86 additions and 1 deletions
47
composables/Relationship.ts
Normal file
47
composables/Relationship.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import type { Mastodon } from "megalodon";
|
||||
import type { Relationship } from "~/types/mastodon/relationship";
|
||||
|
||||
export const useRelationship = (
|
||||
client: MaybeRef<Mastodon | null>,
|
||||
accountId: MaybeRef<string | null>,
|
||||
) => {
|
||||
const relationship = ref(null as Relationship | null);
|
||||
const isLoading = ref(false);
|
||||
|
||||
if (!useSignedIn().value) {
|
||||
return { relationship, isLoading };
|
||||
}
|
||||
|
||||
watchEffect(() => {
|
||||
if (toValue(accountId))
|
||||
toValue(client)
|
||||
?.getRelationship(toValue(accountId) ?? "")
|
||||
.then((res) => {
|
||||
relationship.value = res.data;
|
||||
});
|
||||
});
|
||||
|
||||
watch(relationship, (newOutput, oldOutput) => {
|
||||
if (newOutput !== oldOutput && newOutput && oldOutput) {
|
||||
if (newOutput?.following !== oldOutput?.following) {
|
||||
isLoading.value = true;
|
||||
if (newOutput?.following) {
|
||||
toValue(client)
|
||||
?.followAccount(toValue(accountId) ?? "")
|
||||
.finally(() => {
|
||||
isLoading.value = false;
|
||||
});
|
||||
} else {
|
||||
toValue(client)
|
||||
?.unfollowAccount(toValue(accountId) ?? "")
|
||||
.finally(() => {
|
||||
isLoading.value = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
// FIXME: Add more relationship changes
|
||||
}
|
||||
});
|
||||
|
||||
return { relationship, isLoading };
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue