Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix album sharing toggle #409

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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