Skip to content

Commit

Permalink
feat(api): introduce rule_group field to scenario iteration rule (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
balzdur authored Jun 7, 2024
1 parent 13dbe39 commit 0b72480
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 34 deletions.
8 changes: 6 additions & 2 deletions packages/app-builder/src/models/scenario-iteration-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface ScenarioIterationRule {
displayOrder: number;
name: string;
description: string;
ruleGroup: string | null;
ruleGroup: string;
formula: AstNode | null;
scoreModifier: number;
createdAt: string;
Expand All @@ -27,7 +27,7 @@ export function adaptScenarioIterationRule(
displayOrder: dto.displayOrder,
name: dto.name,
description: dto.description,
ruleGroup: null,
ruleGroup: dto.rule_group,
formula: dto.formula_ast_expression
? adaptAstNode(dto.formula_ast_expression)
: null,
Expand All @@ -41,6 +41,7 @@ export interface CreateScenarioIterationRuleInput {
displayOrder: number;
name: string;
description: string;
ruleGroup: string;
formula: AstNode | null;
scoreModifier: number;
}
Expand All @@ -53,6 +54,7 @@ export function adaptCreateScenarioIterationRuleBodyDto(
displayOrder: input.displayOrder,
name: input.name,
description: input.description,
rule_group: input.ruleGroup,
formula_ast_expression: input.formula ? adaptNodeDto(input.formula) : null,
scoreModifier: input.scoreModifier,
};
Expand All @@ -63,6 +65,7 @@ export interface UpdateScenarioIterationRuleInput {
displayOrder?: number;
name?: string;
description?: string;
ruleGroup?: string;
formula?: AstNode | null;
scoreModifier?: number;
}
Expand All @@ -74,6 +77,7 @@ export function adaptUpdateScenarioIterationRuleBodyDto(
displayOrder: input.displayOrder,
name: input.name,
description: input.description,
rule_group: input.ruleGroup,
formula_ast_expression: input.formula
? adaptNodeDto(input.formula)
: input.formula,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
const ruleGroups = R.pipe(
rules,
R.map((rule) => rule.ruleGroup),
R.filter(R.isNonNullish),
R.filter((val) => !R.isEmpty(val)),
R.unique(),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
const editRuleFormSchema = z.object({
name: z.string().min(1),
description: z.string(),
ruleGroup: z.string().nullable(),
ruleGroup: z.string(),
scoreModifier: z.coerce.number().int().min(-1000).max(1000),
});
type EditRuleFormValues = z.infer<typeof editRuleFormSchema>;
Expand Down Expand Up @@ -150,6 +150,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
formula: astNode,
name: formValues.name,
description: formValues.description,
ruleGroup: formValues.ruleGroup,
scoreModifier: formValues.scoreModifier,
});

Expand Down Expand Up @@ -539,17 +540,9 @@ function RuleGroup({
field: ControllerRenderProps<EditRuleFormValues, 'ruleGroup'>;
}) {
const { t } = useTranslation(handle.i18n);
const value = field.value ?? '';
const searchValue = React.useDeferredValue(value);
const searchValue = React.useDeferredValue(field.value);
const ruleGroups = useRuleGroups();

const onChange = React.useCallback(
(value: string) => {
field.onChange(value || null);
},
[field],
);

const matches = React.useMemo(
() => matchSorter(ruleGroups, searchValue),
[searchValue, ruleGroups],
Expand All @@ -558,10 +551,10 @@ function RuleGroup({
return (
<ComboboxRoot
open={ruleGroups.length === 0 ? false : undefined}
value={value}
setValue={onChange}
selectedValue={value}
setSelectedValue={onChange}
value={field.value}
setValue={field.onChange}
selectedValue={field.value}
setSelectedValue={field.onChange}
>
<FormItem className="flex flex-col gap-2">
<ComboboxLabel render={<FormLabel />}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
formula: null,
name: t('create_rule.default_name'),
description: '',
ruleGroup: '',
scoreModifier: 0,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const useRuleGroups = () => {
R.pipe(
rules,
R.map((rule) => rule.ruleGroup),
R.filter(R.isNonNullish),
R.filter((val) => !R.isEmpty(val)),
R.unique(),
),
[rules],
Expand Down
40 changes: 24 additions & 16 deletions packages/marble-api/scripts/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4368,15 +4368,6 @@ components:
type: string
ScenarioIterationRuleDto:
type: object
required:
- id
- scenarioIterationId
- displayOrder
- name
- description
- formula_ast_expression
- scoreModifier
- createdAt
properties:
id:
type: string
Expand All @@ -4390,6 +4381,8 @@ components:
type: string
description:
type: string
rule_group:
type: string
formula_ast_expression:
nullable: true
oneOf:
Expand All @@ -4399,6 +4392,16 @@ components:
createdAt:
type: string
format: date-time
required:
- id
- scenarioIterationId
- displayOrder
- name
- description
- rule_group
- formula_ast_expression
- scoreModifier
- createdAt
ScenarioValidationDto:
type: object
required:
Expand Down Expand Up @@ -4452,13 +4455,6 @@ components:
$ref: '#/components/schemas/ScenarioValidationErrorDto'
CreateScenarioIterationRuleBodyDto:
type: object
required:
- scenarioIterationId
- displayOrder
- name
- description
- formula_ast_expression
- scoreModifier
properties:
scenarioIterationId:
type: string
Expand All @@ -4469,12 +4465,22 @@ components:
type: string
description:
type: string
rule_group:
type: string
formula_ast_expression:
nullable: true
oneOf:
- $ref: '#/components/schemas/NodeDto'
scoreModifier:
type: integer
required:
- scenarioIterationId
- displayOrder
- name
- description
- rule_group
- formula_ast_expression
- scoreModifier
UpdateScenarioIterationRuleBodyDto:
type: object
properties:
Expand All @@ -4484,6 +4490,8 @@ components:
type: string
description:
type: string
rule_group:
type: string
formula_ast_expression:
nullable: true
oneOf:
Expand Down
3 changes: 3 additions & 0 deletions packages/marble-api/src/generated/marble-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ export type CreateScenarioIterationRuleBodyDto = {
displayOrder: number;
name: string;
description: string;
rule_group: string;
formula_ast_expression: (NodeDto) | null;
scoreModifier: number;
};
Expand All @@ -340,6 +341,7 @@ export type ScenarioIterationRuleDto = {
displayOrder: number;
name: string;
description: string;
rule_group: string;
formula_ast_expression: (NodeDto) | null;
scoreModifier: number;
createdAt: string;
Expand Down Expand Up @@ -388,6 +390,7 @@ export type UpdateScenarioIterationRuleBodyDto = {
displayOrder?: number;
name?: string;
description?: string;
rule_group?: string;
formula_ast_expression?: (NodeDto) | null;
scoreModifier?: number;
};
Expand Down

0 comments on commit 0b72480

Please sign in to comment.