Skip to content

Commit

Permalink
Add groups to sectionConfigs.ts and modify sections.ts with sectionCo…
Browse files Browse the repository at this point in the history
…nfigs.ts (#407)

Add groups to sectionConfigs.ts.
Modify sections.ts with sectionConfigs.ts.
Update sectionsTypes with better types.
Fix previousSection on generate_section_configs.py.
  • Loading branch information
samuel-duhaime authored Mar 21, 2024
1 parent 568dfea commit ff89a07
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def generate_section_configs(input_file: str):
ts_code += f"import {{ isSectionComplete }} from 'evolution-generator/lib/helpers/configsHelpers';\n"
ts_code += f"import {{ SectionConfig, SectionName }} from 'evolution-generator/lib/types/sectionsTypes';\n"
ts_code += f"import {{ widgetsNames }} from './widgetsNames';\n"
ts_code += f"import {{ preload }} from './preload';\n\n"
ts_code += f"import {{ preload }} from './preload';\n"

# Iterate through each row in the sheet, starting from the second row
for row_number, row in enumerate(rows[1:], start=2):
Expand All @@ -69,15 +69,24 @@ def generate_section_configs(input_file: str):

# Generate code for section
def generate_section_code(previousSection, nextSection):
ts_section_code = "" # TypeScript code for the section

# TODO: Do this with survey_folder_path
# Get section output file
section_output_file = (
f"../../../survey/src/survey/sections/{section}/sectionConfigs.ts"
)

# Check if the section has groups
has_groups = groups is not None and groups != ""

# Add imports for groups if the section has groups
if has_groups:
ts_section_code += f"import * as groups from './groups';\n"

# Generate currentSectionName
ts_section_code = (
f"export const currentSectionName: SectionName = '{section}';\n"
ts_section_code += (
f"\nexport const currentSectionName: SectionName = '{section}';\n"
)

# Generate previousSectionName
Expand Down Expand Up @@ -129,7 +138,6 @@ def generate_section_code(previousSection, nextSection):
f"{INDENT}// Do some actions before the section is loaded\n"
)
ts_section_code += f"{INDENT}preload: preload,\n"

ts_section_code += f"{INDENT}// Allow to click on the section menu\n"
if previousSection is None:
ts_section_code += f"{INDENT}enableConditional:true,\n"
Expand All @@ -144,8 +152,15 @@ def generate_section_code(previousSection, nextSection):
f"{INDENT}completionConditional: function (interview) {{\n"
)
ts_section_code += f"{INDENT}{INDENT}return isSectionComplete({{ interview, sectionName: currentSectionName }});\n"
ts_section_code += f"{INDENT}}}\n"
ts_section_code += f"}};\n\n"
ts_section_code += f"{INDENT}}}"
if has_groups:
# Split the groups string into a list of group names
group_names = groups.split(",")
ts_section_code += f",\n{INDENT}groups: {{\n"
for group_name in group_names:
ts_section_code += f"{INDENT}{INDENT}{group_name}: groups.{group_name},\n"
ts_section_code += f"{INDENT}}},\n"
ts_section_code += f"\n}};\n\n"
ts_section_code += f"export default sectionConfig;\n"

# Write TypeScript code to a file
Expand All @@ -159,13 +174,13 @@ def generate_section_code(previousSection, nextSection):

print(f"Generate {section_output_file} successfully")

previousSection = (
section # Update previousSection with the current section
)

# Generate section code
generate_section_code(previousSection, nextSection)

# Update previousSection with the current section
previousSection = section
print(previousSection)

except Exception as e:
# Handle any other exceptions that might occur during script execution
print(f"Error with sectionConfigs.ts: {e}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def generate_sections(output_file: str, sections: List[str]):
ts_code += "import { SectionsConfigs } from 'evolution-generator/lib/types/sectionsTypes';\n"
# Loop through each section and generate an import statement
for section in sections:
ts_code += f"import {section}Configs from './sections/{section}/configs';\n"
ts_code += f"import {section}Configs from './sections/{section}/sectionConfigs';\n"

# Generate the export statement
ts_code += "\n// Export all the sections configs\n"
Expand Down
40 changes: 25 additions & 15 deletions packages/evolution-generator/src/types/sectionsTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,30 @@

// Note: This file includes types for all the different sections types used in the evolution-generator


export type SectionName = string | null;
export type WidgetsNames = string[];
export type InterviewPathFunction = (interview: any, path: string) => boolean;
export type Group = {
showTitle?: boolean;
showGroupedObjectDeleteButton: InterviewPathFunction | boolean;
deleteConfirmPopup?: {
content: {
fr: ((interview?, path?) => string) | string;
en: ((interview?, path?) => string) | string;
};
};
showGroupedObjectAddButton: InterviewPathFunction | boolean;
groupedObjectAddButtonLabel?: {
fr: ((interview?, path?) => string) | string;
en: ((interview?, path?) => string) | string;
};
addButtonLocation?: 'bottom' | 'both';
addButtonSize?: string;
widgets: string[];
};
export type Groups = {
[groupName: string]: Group;
};
// TODO: Add better types for Preload
export type Preload = (
interview: any,
Expand All @@ -34,21 +55,10 @@ export type SectionConfig = {
};
parentSection?: string;
widgets: WidgetsNames;
groups?: {
[groupName: string]: {
showGroupedObjectDeleteButton: (interview: any, path: string) => boolean;
showGroupedObjectAddButton: (interview: any, path: string) => boolean;
groupedObjectAddButtonLabel: {
fr: string;
en: string;
};
addButtonSize: string;
widgets: string[];
};
};
groups?: Groups;
preload?: Preload;
enableConditional: ((interview: any) => boolean) | boolean;
completionConditional: ((interview: any) => boolean) | boolean;
enableConditional: InterviewPathFunction | boolean;
completionConditional: InterviewPathFunction | boolean;
};

// Configs for the sections configs
Expand Down

0 comments on commit ff89a07

Please sign in to comment.