Skip to content

Commit

Permalink
Merge pull request #507 from AkshataKatwal16/reassign-cohorts
Browse files Browse the repository at this point in the history
Issue feat:update zustand center list upon create new center and for newly craeted center store in lowercasefor classname
  • Loading branch information
itsvick authored Dec 5, 2024
2 parents e585613 + 2fca263 commit edb6201
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/components/LearnersListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const LearnersListItem: React.FC<LearnerListProps> = ({

setCenters(centers);
setCentersName(centersName);
}, [reloadState, setReloadState]);
}, [reloadState, setReloadState, userStore.cohorts]);

const toggleDrawer =
(anchor: Anchor, open: boolean) =>
Expand Down Expand Up @@ -366,7 +366,7 @@ const LearnersListItem: React.FC<LearnerListProps> = ({
t('MANAGE_USERS.CENTERS_REASSIGNED_SUCCESSFULLY'),
'success'
);
setReloadState(true);
setReloadState(!reloadState);
} catch (error) {
console.error('Error creating cohort members', error);
showToastMessage(t('MANAGE_USERS.CENTERS_REQUEST_FAILED'), 'error');
Expand Down
4 changes: 2 additions & 2 deletions src/components/ManageCentersModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const ManageCentersModal: React.FC<ManageUsersModalProps> = ({
};

const handleRadioChange = (name: string, cohortId: string) => {
setSelectedValue(name);
setSelectedValue(name.toLowerCase());
setCohortId(cohortId);
};

Expand Down Expand Up @@ -192,7 +192,7 @@ const ManageCentersModal: React.FC<ManageUsersModalProps> = ({
{isForLearner ? (
<Radio
sx={{ pb: '20px' }}
checked={selectedValue === center?.name}
checked={selectedValue === center?.name?.toLowerCase()}
onChange={() =>
handleRadioChange(center?.name, center?.cohortId)
}
Expand Down
1 change: 1 addition & 0 deletions src/components/ReassignModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const ReassignModal: React.FC<ReassignModalProps> = ({
reloadState,
setReloadState,
}) => {
console.log("ReassignModal")
const theme = useTheme<any>();
const { t } = useTranslation();
const store = useStore();
Expand Down
53 changes: 53 additions & 0 deletions src/components/center/CreateCenterModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { showToastMessage } from '../Toastify';
import FrameworkCategories from './FrameworkCategories';
import { telemetryFactory } from '@/utils/telemetry';
import { useQueryClient } from '@tanstack/react-query';
import { getCohortList } from '@/services/CohortServices';
import useStore from '@/store/store';
interface CreateBlockModalProps {
open: boolean;
handleClose: () => void;
Expand All @@ -32,6 +34,21 @@ interface CohortDetails {
parentId: string | null;
customFields: CustomField[];
}
interface ChildData {
cohortId: string;
name: string;
parentId: string;
type: string;
customField: any[];
childData: ChildData[];
status?: string;
}
interface NameTypePair {
cohortId: string;
name: string;
cohortType: string;
status?: string;
}
const CreateCenterModal: React.FC<CreateBlockModalProps> = ({
open,
handleClose,
Expand All @@ -45,6 +62,7 @@ const CreateCenterModal: React.FC<CreateBlockModalProps> = ({
const [showForm, setShowForm] = useState(false);
const [isHiddenFieldPresent, setIsHiddenFieldPresent] = useState(false);
const queryClient = useQueryClient();
const setCohorts = useStore((state) => state.setCohorts);

const { data: formResponse, isPending } = useFormRead(
FormContext.COHORTS,
Expand Down Expand Up @@ -89,6 +107,33 @@ const CreateCenterModal: React.FC<CreateBlockModalProps> = ({
setFormData(data.formData);
});
};
const extractNamesAndCohortTypes = (
data: ChildData[]
): NameTypePair[] => {
const nameTypePairs: NameTypePair[] = [];
const recursiveExtract = (items: ChildData[]) => {
items.forEach((item) => {
const cohortType =
item?.customField?.find(
(field) => field?.label === 'TYPE_OF_COHORT'
)?.value || 'Unknown';
if (item?.cohortId && item?.name) {
nameTypePairs.push({
cohortId: item?.cohortId,
name: item?.name,
status: item?.status,
cohortType,
});
}
if (item?.childData && item?.childData?.length > 0) {
recursiveExtract(item?.childData);
}
});
};
recursiveExtract(data);
return nameTypePairs;
};

useEffect(() => {
if (formData) {
handleButtonClick();
Expand Down Expand Up @@ -160,6 +205,14 @@ const CreateCenterModal: React.FC<CreateBlockModalProps> = ({
queryClient.invalidateQueries({
queryKey: [QueryKeys.MY_COHORTS, userId],
});
const myCohortList = await queryClient.fetchQuery({
queryKey: [QueryKeys.MY_COHORTS, userId],
queryFn: () => getCohortList(userId as string, { customField: 'true' }),
});
if (myCohortList?.length > 0) {
const nameTypePairs = extractNamesAndCohortTypes(myCohortList);
setCohorts(nameTypePairs);
}
const telemetryInteract = {
context: {
env: 'teaching-center',
Expand Down
2 changes: 1 addition & 1 deletion src/components/center/centerList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const CenterList: React.FC<CenterListProps> = ({
onClick={() => {
router.push(`/centers/${center?.cohortId}`);
localStorage.setItem('classId', center?.cohortId);
localStorage.setItem('className', center?.cohortName);
localStorage.setItem('className', center?.cohortName?.toLowerCase());
}}
sx={{
cursor: 'pointer',
Expand Down

0 comments on commit edb6201

Please sign in to comment.