@oxyhq/services
Version:
Reusable OxyHQ module to handle authentication, user management, karma system, device-based session management and more 🚀
225 lines (217 loc) • 5.69 kB
JavaScript
"use strict";
import { create } from 'zustand';
export const useFollowStore = create((set, get) => ({
followingUsers: {},
loadingUsers: {},
fetchingUsers: {},
errors: {},
followerCounts: {},
followingCounts: {},
loadingCounts: {},
setFollowingStatus: (userId, isFollowing) => set(state => ({
followingUsers: {
...state.followingUsers,
[]: isFollowing
},
errors: {
...state.errors,
[]: null
}
})),
clearFollowError: userId => set(state => ({
errors: {
...state.errors,
[]: null
}
})),
resetFollowState: () => set({
followingUsers: {},
loadingUsers: {},
fetchingUsers: {},
errors: {},
followerCounts: {},
followingCounts: {},
loadingCounts: {}
}),
fetchFollowStatus: async (userId, oxyServices) => {
set(state => ({
fetchingUsers: {
...state.fetchingUsers,
[]: true
},
errors: {
...state.errors,
[]: null
}
}));
try {
const response = await oxyServices.getFollowStatus(userId);
set(state => ({
followingUsers: {
...state.followingUsers,
[]: response.isFollowing
},
fetchingUsers: {
...state.fetchingUsers,
[]: false
},
errors: {
...state.errors,
[]: null
}
}));
} catch (error) {
set(state => ({
fetchingUsers: {
...state.fetchingUsers,
[]: false
},
errors: {
...state.errors,
[]: error?.message || 'Failed to fetch follow status'
}
}));
}
},
toggleFollowUser: async (userId, oxyServices, isCurrentlyFollowing) => {
set(state => ({
loadingUsers: {
...state.loadingUsers,
[]: true
},
errors: {
...state.errors,
[]: null
}
}));
try {
let response;
let newFollowState;
if (isCurrentlyFollowing) {
response = await oxyServices.unfollowUser(userId);
newFollowState = false;
} else {
response = await oxyServices.followUser(userId);
newFollowState = true;
}
// Update follow status
set(state => ({
followingUsers: {
...state.followingUsers,
[]: newFollowState
},
loadingUsers: {
...state.loadingUsers,
[]: false
},
errors: {
...state.errors,
[]: null
}
}));
// Update counts if the response includes them
// The API returns counts for both users:
// - followers: target user's follower count (the user being followed)
// - following: current user's following count (the user doing the following)
if (response && response.counts) {
const {
counts
} = response;
// Get current user ID from oxyServices
const currentUserId = oxyServices.getCurrentUserId();
set(state => {
const updates = {};
// Update target user's follower count (the user being followed)
updates.followerCounts = {
...state.followerCounts,
[]: counts.followers
};
// Update current user's following count (the user doing the following)
if (currentUserId) {
updates.followingCounts = {
...state.followingCounts,
[]: counts.following
};
}
return updates;
});
}
} catch (error) {
set(state => ({
loadingUsers: {
...state.loadingUsers,
[]: false
},
errors: {
...state.errors,
[]: error?.message || 'Failed to update follow status'
}
}));
}
},
setFollowerCount: (userId, count) => set(state => ({
followerCounts: {
...state.followerCounts,
[]: count
}
})),
setFollowingCount: (userId, count) => set(state => ({
followingCounts: {
...state.followingCounts,
[]: count
}
})),
updateCountsFromFollowAction: (targetUserId, action, counts, currentUserId) => {
set(state => {
const updates = {};
// Update target user's follower count (the user being followed)
updates.followerCounts = {
...state.followerCounts,
[]: counts.followers
};
// Update current user's following count (the user doing the following)
if (currentUserId) {
updates.followingCounts = {
...state.followingCounts,
[]: counts.following
};
}
return updates;
});
},
fetchUserCounts: async (userId, oxyServices) => {
set(state => ({
loadingCounts: {
...state.loadingCounts,
[]: true
}
}));
try {
const user = await oxyServices.getUserById(userId);
if (user && user._count) {
set(state => ({
followerCounts: {
...state.followerCounts,
[]: user._count?.followers || 0
},
followingCounts: {
...state.followingCounts,
[]: user._count?.following || 0
},
loadingCounts: {
...state.loadingCounts,
[]: false
}
}));
}
} catch (error) {
set(state => ({
loadingCounts: {
...state.loadingCounts,
[]: false
}
}));
}
}
}));
//# sourceMappingURL=followStore.js.map