Skip to content

Commit

Permalink
fix album sharing toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
sickelap committed Nov 13, 2023
1 parent e9f7da9 commit 52fbd09
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions src/components/sharing/UserEntry.tsx
Original file line number Diff line number Diff line change
@@ -1,59 +1,58 @@
import { Avatar, Group, Switch, Text, Title } from "@mantine/core";
import { DateTime } from "luxon";
import React, { useEffect, useState } from "react";
import React from "react";
import { useTranslation } from "react-i18next";

import { setUserAlbumShared } from "../../actions/albumsActions";
import { UserAlbum, useFetchUserAlbumQuery, useShareUserAlbumMutation } from "../../api_client/albums/user";
import { i18nResolvedLanguage } from "../../i18n";
import { useAppDispatch, useAppSelector } from "../../store/store";
import { IUser } from "../../store/user/user.zod";

type UserEntryProps = {
item: any;
item: IUser;
albumID: string;
};

export function UserEntry(props: UserEntryProps) {
const { item, albumID } = props;
const { albumDetails } = useAppSelector(store => store.albums);
function getDisplayName(item: IUser) {
return item.first_name.length > 0 && item.last_name.length > 0
? `${item.first_name} ${item.last_name}`
: item.username;
}

const [isShared, setIsShared] = useState(
albumDetails.shared_to && albumDetails.shared_to.map(e => e.id).includes(item.id)
);
const dispatch = useAppDispatch();
const { t } = useTranslation();
function getAvatar(item: IUser) {
return item.avatar ? item.avatar_url : "/unknown_user.jpg";
}

useEffect(() => {
setIsShared(albumDetails.shared_to && albumDetails.shared_to.map(e => e.id).includes(item.id));
}, [albumDetails]);
function isShared(album: UserAlbum, user: IUser) {
return album?.shared_to.map(sUser => sUser.id).includes(user.id);
}

const displayName =
item.first_name.length > 0 && item.last_name.length > 0 ? `${item.first_name} ${item.last_name}` : item.username;
const avatar = item.avatar ? item.avatar_url : "/unknown_user.jpg";
export function UserEntry(props: UserEntryProps) {
const { item: user, albumID } = props;
const { t } = useTranslation();
const { data: albumDetails } = useFetchUserAlbumQuery(albumID);
const [shareUserAlbum] = useShareUserAlbumMutation();

return (
<Group position="apart" key={item.id}>
<Group position="apart" key={user.id}>
<Group>
<Avatar radius="xl" size={50} src={avatar} />
<Avatar radius="xl" size={50} src={getAvatar(user)} />
<div>
<Title order={4}>{displayName}</Title>
<Title order={4}>{getDisplayName(user)}</Title>
<Text size="sm" color="dimmed">
{t("modalphotosshare.joined")}{" "}
{DateTime.fromISO(item.date_joined).setLocale(i18nResolvedLanguage).toRelative()}
{DateTime.fromISO(user.date_joined).setLocale(i18nResolvedLanguage).toRelative()}
</Text>
</div>
</Group>
<Group>
<Switch
checked={isShared}
checked={isShared(albumDetails!, user)}
onChange={() => {
dispatch(
setUserAlbumShared(
parseInt(albumID, 10),
item.id,
!albumDetails.shared_to.map(e => e.id).includes(item.id)
)
);
setIsShared(!isShared);
shareUserAlbum({
albumId: albumID,
userId: `${user.id}`,
share: !albumDetails?.shared_to.map(e => e.id).includes(user.id),
});
}}
/>
</Group>
Expand Down

0 comments on commit 52fbd09

Please sign in to comment.