All files / src/utils/hooks useTeacherMadeAssignmentQuota.js

95% Statements 19/20
87.5% Branches 7/8
100% Functions 6/6
95% Lines 19/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68                                3x 17x 17x 17x 33x     17x   17x 16x 7x   9x     17x 17x   17x 1x     17x   2x             17x   3x         17x                        
import { useCallback, useMemo } from "react";
import { useDispatch, useSelector } from "react-redux";
import {
    fetchTeacherMadeAssignmentQuota,
    setTeacherMadeAssignmentQuota,
} from "@/store/slices/teacher/TeacherFeatureFlags";
import {
    getTeacherMadeAssignmentCreateUi,
    getUpgradeDialogOptionsForQuota,
    resolveClassTeacherMadeAssignmentQuota,
} from "@/utils/features/teacherMadeAssignmentQuota";
import { useUpgradeDialog } from "@/utils/components/feature-access/UpgradeDialogProvider";
 
/**
 * @param {number|null|undefined} classAssignmentCount When set, quota usage is scoped to this class (10 per class per teacher).
 */
const useTeacherMadeAssignmentQuota = (classAssignmentCount = null) => {
    const dispatch = useDispatch();
    const { openUpgradeDialog } = useUpgradeDialog();
    const { teacherMadeAssignments, districtFeatures, isLoading, quotaLoading } = useSelector(
        (state) => state.TeacherFeatureFlags
    );
 
    const planQuota = teacherMadeAssignments;
 
    const quota = useMemo(() => {
        if (classAssignmentCount == null) {
            return planQuota;
        }
        return resolveClassTeacherMadeAssignmentQuota(planQuota, classAssignmentCount);
    }, [planQuota, classAssignmentCount]);
 
    const ui = getTeacherMadeAssignmentCreateUi(quota);
    const loading = isLoading || quotaLoading;
 
    const refreshQuota = useCallback(() => {
        dispatch(fetchTeacherMadeAssignmentQuota());
    }, [dispatch]);
 
    const syncQuota = useCallback(
        (nextQuota) => {
            Iif (nextQuota) {
                dispatch(setTeacherMadeAssignmentQuota(nextQuota));
            }
        },
        [dispatch]
    );
 
    const openUpgradeOnBlock = useCallback(
        (options = {}) => {
            openUpgradeDialog(getUpgradeDialogOptionsForQuota(quota, districtFeatures, options));
        },
        [openUpgradeDialog, quota, districtFeatures]
    );
 
    return {
        planQuota,
        quota,
        ui,
        loading,
        refreshQuota,
        syncQuota,
        openUpgradeOnBlock,
    };
};
 
export default useTeacherMadeAssignmentQuota;