Skip to content

Commit

Permalink
Merge pull request #268 from AkshataKatwal16/reassign-cohorts
Browse files Browse the repository at this point in the history
Issue feat: PS-1502 updated user cohorts values from zustand and loca…
  • Loading branch information
itsvick authored Sep 18, 2024
2 parents 158b45a + 9304241 commit 9aac37c
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 12 deletions.
20 changes: 14 additions & 6 deletions src/components/CohortSelectionSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { ArrowDropDownIcon } from '@mui/x-date-pickers/icons';
import { telemetryFactory } from '@/utils/telemetry';
import { toPascalCase } from '@/utils/Helper';
import { useQueryClient } from '@tanstack/react-query';
import { getUserDetails } from '@/services/ProfileService';

interface CohortSelectionSectionProps {
classId: string;
Expand Down Expand Up @@ -93,6 +94,7 @@ const CohortSelectionSection: React.FC<CohortSelectionSectionProps> = ({
showFloatingLabel = false,
showDisabledDropDown = false,
}) => {
console.log("cohortsData",classId, cohortsData[0]?.cohortId)
const router = useRouter();
const theme = useTheme<any>();
const queryClient = useQueryClient();
Expand Down Expand Up @@ -147,8 +149,14 @@ const CohortSelectionSection: React.FC<CohortSelectionSectionProps> = ({
queryFn: () => getCohortList(userId, { customField: 'true' }),
});

console.log('Response:', response);
const cohortData = response[0];
let userDetailsResponse;
if(userId)
{
userDetailsResponse = await getUserDetails(userId, true);
}
const blockObject = userDetailsResponse?.result?.userData?.customFields.find((item:any) => item?.label === 'BLOCKS');

if (cohortData?.customField?.length) {
const district = cohortData?.customField?.find(
(item: CustomField) => item?.label === 'DISTRICTS'
Expand All @@ -172,12 +180,12 @@ const CohortSelectionSection: React.FC<CohortSelectionSectionProps> = ({
(field: any) => field?.label === 'BLOCKS'
);

if (blockField) {
setBlockCode(blockField?.code);
setBlockId(blockField?.fieldId);
if (blockObject) {
setBlockCode(blockObject?.code);
setBlockId(blockObject?.fieldId);
}
}

if (response && response?.length > 0) {
const extractNamesAndCohortTypes = (
data: ChildData[]
Expand Down Expand Up @@ -446,7 +454,7 @@ const CohortSelectionSection: React.FC<CohortSelectionSectionProps> = ({
<Select
labelId="center-select-label"
label={showFloatingLabel ? t('COMMON.CENTER') : ''}
value={classId}
value={classId? classId: cohortsData[0]?.cohortId}
onChange={handleCohortSelection}
// displayEmpty
// style={{ borderRadius: '4px' }}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ManageUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ const ManageUser: React.FC<ManageUsersProps> = ({
queryKey: [QueryKeys.GET_ACTIVE_FACILITATOR, filters],
queryFn: () => getMyUserList({ limit, page, filters, fields }),
});

console.log("facilitator")
const facilitatorList = resp.result?.getUserDetails;

if (!facilitatorList || facilitatorList?.length === 0) {
Expand Down
37 changes: 33 additions & 4 deletions src/pages/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ import CalendarMonthIcon from '@mui/icons-material/CalendarMonth';
import { useQueryClient } from '@tanstack/react-query';
import { getCohortList } from '@/services/CohortServices';
import CentralizedModal from '@/components/CentralizedModal';

import manageUserStore from '@/store/manageUserStore';
import { getUserDetails } from '@/services/ProfileService';
import {
updateStoreFromCohorts
} from '@/utils/Helper';
interface DashboardProps {}

const Dashboard: React.FC<DashboardProps> = () => {
Expand Down Expand Up @@ -114,6 +118,7 @@ const Dashboard: React.FC<DashboardProps> = () => {
const [myCohortList, setMyCohortList] = React.useState<any>();
const [centralizedModal, setCentralizedModal] = useState(false);


const toggleDrawer = (newOpen: boolean) => () => {
setOpenDrawer(newOpen);
};
Expand Down Expand Up @@ -147,8 +152,19 @@ const Dashboard: React.FC<DashboardProps> = () => {

calculateDateRange();
}, []);

useEffect(() => {
if(cohortsData[0]?.cohortId)
{

localStorage.setItem('classId', cohortsData[0]?.cohortId)

}
else
{
localStorage.setItem('classId', "")

}
if (typeof window !== 'undefined' && window.localStorage) {
const token = localStorage.getItem('token');
const role = localStorage.getItem('role');
Expand All @@ -162,16 +178,29 @@ const Dashboard: React.FC<DashboardProps> = () => {
}
setUserId(storedUserId);
}
}, []);
}, [cohortsData]);

useEffect(() => {

const getMyCohortList = async () => {
const myCohortList = await queryClient.fetchQuery({
queryKey: [QueryKeys.MY_COHORTS, userId],
queryFn: () => getCohortList(userId as string, { customField: 'true' }),
});
let response;
if(userId)
{
response = await getUserDetails(userId, true);
}
const blockObject = response?.result?.userData?.customFields.find((item:any) => item?.label === 'BLOCKS');


let isCustomFields=true;
const result=await getCohortList(userId as string, { customField: 'true' }, isCustomFields)
const activeCohorts = result?.filter((cohort: any) => cohort.cohortMemberStatus === "active");
updateStoreFromCohorts(activeCohorts, blockObject );

setMyCohortList(myCohortList);
setMyCohortList(myCohortList);
};
if (userId) {
getMyCohortList();
Expand Down
9 changes: 8 additions & 1 deletion src/services/CohortServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export const getCohortDetails = async (cohortId: string): Promise<any> => {

export const getCohortList = async (
userId: string,
filters: { [key: string]: string } = {}
filters: { [key: string]: string } = {},
isCustomFields: boolean = false
): Promise<any> => {
let apiUrl: string = `${process.env.NEXT_PUBLIC_BASE_URL}/cohort/mycohorts/${userId}?children=true`;
const filterParams = new URLSearchParams(filters).toString();
Expand All @@ -40,6 +41,11 @@ export const getCohortList = async (
}
try {
const response = await get(apiUrl);
if(isCustomFields)
{
return response?.data?.result;

}
if (response?.data?.result?.length) {
let res = response?.data?.result;
res = res.filter((block: any) => {
Expand All @@ -53,6 +59,7 @@ export const getCohortList = async (
return res;
}
return response?.data?.result;

} catch (error) {
console.error('Error in getting cohort details', error);
// throw error;
Expand Down
43 changes: 43 additions & 0 deletions src/utils/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import FingerprintJS from 'fingerprintjs2';
import { CustomField, UpdateCustomField } from './Interfaces';
dayjs.extend(utc);
import { format, parseISO } from 'date-fns';
import manageUserStore from '@/store/manageUserStore';

export const ATTENDANCE_ENUM = {
PRESENT: 'present',
Expand Down Expand Up @@ -554,3 +555,45 @@ export const filterAndMapAssociationsNew = (
associations: option.associations || [],
}));
};


export const updateStoreFromCohorts = (activeCohorts: any, blockObject: any) => {
const setDistrictCode = manageUserStore.getState().setDistrictCode;
const setDistrictId = manageUserStore.getState().setDistrictId;
const setStateCode = manageUserStore.getState().setStateCode;
const setStateId = manageUserStore.getState().setStateId;
const setBlockCode = manageUserStore.getState().setBlockCode;
const setBlockId = manageUserStore.getState().setBlockId;
const setBlockName = manageUserStore.getState().setBlockName;
const setDistrictName = manageUserStore.getState().setDistrictName;
const setStateName = manageUserStore.getState().setStateName;


const district = activeCohorts[0]?.customField?.find(
(item: any) => item?.label === 'DISTRICTS'
);
if (district) {
setDistrictCode(district?.code);
setDistrictId(district?.fieldId);
setDistrictName(district?.value)
}

const state = activeCohorts[0]?.customField?.find(
(item: any) => item?.label === 'STATES'
);

if (state) {
setStateCode(state?.code);
setStateId(state?.fieldId);
setStateName(state?.value);

}


if (blockObject) {
setBlockCode(blockObject?.code);
setBlockId(blockObject?.fieldId);
setBlockName(blockObject?.value);

}
};

0 comments on commit 9aac37c

Please sign in to comment.