Skip to content

Commit 022a0d6

Browse files
Update EditableSubscriptionsCell.tsx
1 parent 1e7a2ae commit 022a0d6

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

src/components/dashboard/user_management/EditableSubscriptionsCell.tsx

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
WrapItem,
1515
} from '@chakra-ui/react';
1616
import { MailingList, SubjectAPI } from '@utils/types';
17-
import React, { useEffect, useMemo, useRef, useState } from 'react';
17+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
1818
import { FiChevronDown, FiChevronUp, FiEdit2 } from 'react-icons/fi';
1919

2020
interface SubjectTagProps {
@@ -56,7 +56,9 @@ const EditableSubscriptionsCell: React.FC<EditableSubscriptionsCellProps> = ({
5656
const [isEditing, setIsEditing] = useState(false);
5757
const [isPopoverOpen, setIsPopoverOpen] = useState(false);
5858
const [isHovered, setIsHovered] = useState(false);
59-
const [selectedSubjectIds, setSelectedSubjectIds] = useState<number[]>([]);
59+
const [selectedSubjectIds, setSelectedSubjectIds] = useState<number[]>(
60+
mailingLists.map((list) => list.subjectId)
61+
);
6062
const [localMailingLists, setLocalMailingLists] =
6163
useState<MailingList[]>(mailingLists);
6264
const containerRef = useRef<HTMLDivElement | null>(null);
@@ -79,18 +81,13 @@ const EditableSubscriptionsCell: React.FC<EditableSubscriptionsCellProps> = ({
7981

8082
// Get sorted subjects for dropdown display
8183
const sortedSubjects = useMemo(() => {
82-
return [...allSubjects].sort((a, b) => {
83-
// First sort by archived status (unarchived first)
84-
if (a.archived !== b.archived) {
85-
return a.archived ? 1 : -1;
86-
}
87-
// Then sort by ID
88-
return a.id - b.id;
89-
});
84+
return [...allSubjects]
85+
.filter((subject) => !subject.archived) // Filter out archived subjects
86+
.sort((a, b) => a.id - b.id); // Now we only need to sort by ID since all subjects are unarchived
9087
}, [allSubjects]);
9188

9289
// Define saveSubscriptions with useCallback so it can be used in dependency arrays
93-
const saveSubscriptions = React.useCallback(() => {
90+
const saveSubscriptions = useCallback(() => {
9491
// Don't save if nothing has changed
9592
const currentIds = new Set(mailingLists.map((list) => list.subjectId));
9693
const selectedIds = new Set(selectedSubjectIds);
@@ -111,11 +108,17 @@ const EditableSubscriptionsCell: React.FC<EditableSubscriptionsCellProps> = ({
111108
});
112109
}, [mailingLists, selectedSubjectIds, onSubscriptionsChange]);
113110

114-
// Initialize selected subjects from current mailing lists
111+
// Update the state when props change
115112
useEffect(() => {
116113
if (!isSaving) {
117-
setSelectedSubjectIds(mailingLists.map((list) => list.subjectId));
118-
setLocalMailingLists(mailingLists);
114+
setSelectedSubjectIds(
115+
mailingLists
116+
.filter((list) => !list.subject.archived)
117+
.map((list) => list.subjectId)
118+
);
119+
setLocalMailingLists(
120+
mailingLists.filter((list) => !list.subject.archived)
121+
);
119122
}
120123
}, [mailingLists, isSaving]);
121124

@@ -126,12 +129,12 @@ const EditableSubscriptionsCell: React.FC<EditableSubscriptionsCellProps> = ({
126129
.map((id) => {
127130
// Try to find existing mailing list first
128131
const existingList = mailingLists.find((list) => list.subjectId === id);
129-
if (existingList) return existingList;
132+
if (existingList && !existingList.subject.archived) return existingList;
130133

131134
// If not, create a new one based on the subject from our full subjects list
132135
const subject = subjectsById[id];
133136

134-
if (!subject) return null;
137+
if (!subject || subject.archived) return null;
135138

136139
return {
137140
subjectId: subject.id,
@@ -140,15 +143,10 @@ const EditableSubscriptionsCell: React.FC<EditableSubscriptionsCellProps> = ({
140143
})
141144
.filter(Boolean) as MailingList[];
142145

143-
// Sort the new mailing lists by archived status and ID
144-
const sortedMailingLists = [...newMailingListsUnsorted].sort((a, b) => {
145-
// First sort by archived status (unarchived first)
146-
if (a.subject.archived !== b.subject.archived) {
147-
return a.subject.archived ? 1 : -1;
148-
}
149-
// Then sort by ID
150-
return a.subjectId - b.subjectId;
151-
});
146+
// Sort the new mailing lists by ID
147+
const sortedMailingLists = [...newMailingListsUnsorted].sort(
148+
(a, b) => a.subjectId - b.subjectId
149+
);
152150

153151
setLocalMailingLists(sortedMailingLists);
154152
}, [selectedSubjectIds, mailingLists, subjectsById]);

0 commit comments

Comments
 (0)