Skip to content
Merged
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
35 changes: 34 additions & 1 deletion src/frontend/src/apis/rules.api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
import { RulesetType, Ruleset } from 'shared';
/*
* This file is part of NER's FinishLine and licensed under GNU AGPLv3.
* See the LICENSE file in the repository root folder for details.
*/

import axios from '../utils/axios';
import { Rule, RulesetType, Ruleset } from 'shared';
import { apiUrls } from '../utils/urls';

/**
* Gets all top-level rules (rules with no parent) for a ruleset
*/
export const getTopLevelRules = (rulesetId: string) => {
return axios.get<Rule[]>(apiUrls.rulesTopLevel(rulesetId));
};

/**
* Gets all child rules of a specific rule
*/
export const getChildRules = (ruleId: string) => {
return axios.get<Rule[]>(apiUrls.rulesChildRules(ruleId));
};

/**
* Toggles team assignment for a rule
*/
export const toggleRuleTeam = (ruleId: string, teamId: string) => {
return axios.post<Rule>(apiUrls.rulesToggleTeam(ruleId), { teamId });
};

/**
* Gets all rules assigned to a team for a specific ruleset type
*/
export const getTeamRulesInRulesetType = (rulesetTypeId: string, teamId: string) => {
return axios.get<Rule[]>(apiUrls.rulesTeamRulesInRulesetType(rulesetTypeId, teamId));
};

/**
* Creates a new ruleset type
*
Expand Down
56 changes: 53 additions & 3 deletions src/frontend/src/hooks/rules.hooks.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,61 @@
import { useMutation, useQueryClient, useQuery } from 'react-query';
import { Ruleset, RulesetType } from 'shared';
import { createRulesetType, getAllRulesetTypes, getRulesetsByRulesetType } from '../apis/rules.api';
/*
* This file is part of NER's FinishLine and licensed under GNU AGPLv3.
* See the LICENSE file in the repository root folder for details.
*/

import { useQuery, useMutation, useQueryClient } from 'react-query';
import { Rule, Ruleset, RulesetType } from 'shared';
import {
getTopLevelRules,
getChildRules,
toggleRuleTeam,
getTeamRulesInRulesetType,
createRulesetType,
getAllRulesetTypes,
getRulesetsByRulesetType
} from '../apis/rules.api';

interface CreateRulesetTypePayload {
name: string;
}

export const useGetTopLevelRules = (rulesetId: string) => {
return useQuery<Rule[], Error>(['rules', 'top-level', rulesetId], async () => {
const { data } = await getTopLevelRules(rulesetId);
return data;
});
};

export const useGetChildRules = (ruleId: string) => {
return useQuery<Rule[], Error>(['rules', 'children', ruleId], async () => {
const { data } = await getChildRules(ruleId);
return data;
});
};

export const useToggleRuleTeam = () => {
const queryClient = useQueryClient();
return useMutation<Rule, Error, { ruleId: string; teamId: string }>(
['rules', 'toggle-team'],
async ({ ruleId, teamId }) => {
const { data } = await toggleRuleTeam(ruleId, teamId);
return data;
},
{
onSuccess: () => {
queryClient.invalidateQueries(['rules']);
}
}
);
};

export const useGetTeamRulesInRulesetType = (rulesetTypeId: string, teamId: string) => {
return useQuery<Rule[], Error>(['rules', 'team-rules', rulesetTypeId, teamId], async () => {
const { data } = await getTeamRulesInRulesetType(rulesetTypeId, teamId);
return data;
});
};

/**
* Custom React Hook to create a new ruleset type
*/
Expand Down
Loading