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

Deleting and Updating Announcements #84

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions backend/graphql/resolvers/notificationResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@ const notificationResolvers = {
);
return newAnnouncement;
},
updateAnnouncement: async (
_parent: undefined,
{
announcementId,
title,
message,
createdAt,
}: {
announcementId: number;
title: string;
message: string;
createdAt: Date;
},
): Promise<NotificationDTO> => {
const updatedAnnouncement = await notificationService.updateAnnouncement(
Number(announcementId),
title,
message,
createdAt,
);
return updatedAnnouncement;
},
},
};

Expand Down
6 changes: 6 additions & 0 deletions backend/graphql/types/notificationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ const notificationType = gql`
message: String
userId: ID
): NotificationDTO!
updateAnnouncement(
announcementId: ID!
title: String
message: String
createdAt: String
): NotificationDTO!
}
`;

Expand Down
35 changes: 35 additions & 0 deletions backend/services/implementations/notificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,41 @@ class NotificationService implements INotificationService {
throw error;
}
}

async updateAnnouncement(
announcementId: number,
title: string,
message: string,
createdAt: Date,
): Promise<NotificationDTO> {
try {
const updatedAnnouncement = await prisma.notification.update({
where: { id: announcementId },
data: {
title,
message,
createdAt: new Date(createdAt),
},
include: { recipients: true },
});

return {
id: updatedAnnouncement.id,
authorId: updatedAnnouncement.authorId,
title: updatedAnnouncement.title,
message: updatedAnnouncement.message,
createdAt: updatedAnnouncement.createdAt,
recipients: updatedAnnouncement.recipients,
};
} catch (error) {
Logger.error(
`Failed to update announcement #${announcementId} because ${getErrorMessage(
error,
)}`,
);
throw error;
}
}
}

export default NotificationService;
7 changes: 7 additions & 0 deletions backend/services/interfaces/notificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ interface INotificationService {
message: string,
userId: number,
): Promise<NotificationDTO>;

updateAnnouncement(
announcementId: number,
title: string,
message: string,
createdAt: Date,
): Promise<NotificationDTO>;
}

export default INotificationService;
6 changes: 3 additions & 3 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ const App = (): React.ReactElement => {
<Route
path={Routes.HOME_PAGE}
element={
<PrivateRoute>
<AnnouncementsPage />
</PrivateRoute>
// <PrivateRoute>
<AnnouncementsPage />
// </PrivateRoute>
}
/>
<Route
Expand Down
72 changes: 37 additions & 35 deletions frontend/src/components/pages/announcements/AnnouncementsGroups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,43 +50,45 @@ const GroupTab = ({
}) => {
const rooms = roomKey.split(",").map(Number);
return (
<Box
onClick={() => setSelectedGroup(roomKey)}
w="100%"
p={3}
borderBottom="solid"
borderBottomColor="gray.300"
_hover={{ bg: "purple.100", cursor: "pointer" }}
>
<Flex alignItems="center">
<Box
borderRadius="full"
border="1px solid"
borderColor="purple.300"
p={1}
mr={3}
>
<Icon
as={
rooms.length === 1
? PersonOutlineOutlinedIcon
: PeopleAltOutlinedIcon
}
boxSize={10}
color="purple.main"
/>
</Box>
<Flex flexDir="column">
<Flex justifyContent="space-between" alignItems="center" w="100%">
<Text as="b">{formatRooms(rooms)}</Text>
<Text color="gray.500">
{moment(firstAnnouncement.createdAt).fromNow()}
</Text>
firstAnnouncement && (
<Box
onClick={() => setSelectedGroup(roomKey)}
w="100%"
p={3}
borderBottom="solid"
borderBottomColor="gray.300"
_hover={{ bg: "purple.100", cursor: "pointer" }}
>
<Flex alignItems="center">
<Box
borderRadius="full"
border="1px solid"
borderColor="purple.300"
p={1}
mr={3}
>
<Icon
as={
rooms.length === 1
? PersonOutlineOutlinedIcon
: PeopleAltOutlinedIcon
}
boxSize={10}
color="purple.main"
/>
</Box>
<Flex flexDir="column">
<Flex justifyContent="space-between" alignItems="center" w="100%">
<Text as="b">{formatRooms(rooms)}</Text>
<Text color="gray.500">
{moment(firstAnnouncement.createdAt).fromNow()}
</Text>
</Flex>
<Text>{truncateMessage(firstAnnouncement.message, 60)}</Text>
</Flex>
<Text>{truncateMessage(firstAnnouncement.message, 60)}</Text>
</Flex>
</Flex>
</Box>
</Box>
)
);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import React, { useEffect, useState } from "react";
import { Flex } from "@chakra-ui/react";

import { GroupAnnouncements } from "../../../types/NotificationTypes";
import {
GroupAnnouncements,
Announcement,
} from "../../../types/NotificationTypes";
import AnnouncementsGroups from "./AnnouncementsGroups";
import AnnouncementsView from "./AnnouncementsView";
import { announcementsMockData } from "../../../mocks/notifications";
Expand All @@ -15,6 +18,26 @@ const AnnouncementsPage = (): React.ReactElement => {
setAnnouncements(announcementsMockData);
}, []);

const deleteAnnouncement = (room: string, id: number): void => {
const newAnnouncements = { ...announcements };
newAnnouncements[room] = newAnnouncements[room].filter(
(announcement: Announcement) => announcement.id !== id,
);
setAnnouncements(newAnnouncements);
};

const updateAnnouncement = (
room: string,
id: number,
message: string,
): void => {
const newAnnouncements = { ...announcements };
newAnnouncements[room] = newAnnouncements[room].map((announcement) =>
announcement.id === id ? { ...announcement, message } : announcement,
);
setAnnouncements(newAnnouncements);
};

return (
<Flex flexDir="row" alignItems="flex-start" w="100%">
<AnnouncementsGroups
Expand All @@ -24,6 +47,8 @@ const AnnouncementsPage = (): React.ReactElement => {
<AnnouncementsView
announcements={announcements}
selectedGroup={selectedGroup}
deleteAnnouncement={deleteAnnouncement}
updateAnnouncement={updateAnnouncement}
/>
</Flex>
);
Expand Down
Loading