Skip to content

Commit

Permalink
Add enhancement features
Browse files Browse the repository at this point in the history
  • Loading branch information
barshathakuri committed Oct 2, 2024
1 parent 0c1a681 commit 5e71084
Show file tree
Hide file tree
Showing 14 changed files with 237 additions and 213 deletions.
2 changes: 1 addition & 1 deletion app/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const PROJECT_STATUS_PLANNED = 0 satisfies StatusTypeEnum;

// FIXME: fix typing in server (medium priority)
// This should not be the same as OperationType.
type DrefStatus = components<'read'>['schemas']['DrefDrefStatusEnumKey'];
export type DrefStatus = components<'read'>['schemas']['DrefDrefStatusEnumKey'];
export const DREF_STATUS_COMPLETED = 1 satisfies DrefStatus;
export const DREF_STATUS_IN_PROGRESS = 0 satisfies DrefStatus;

Expand Down
4 changes: 2 additions & 2 deletions app/src/utils/domain/dref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ export const nsActionsOrder: Record<NsActions['title'], number> = {
other: 18,
};

export type DrefSheetName = 'Operation Overview' | 'Event Detail' | 'Actions-Needs' | 'Operation' | 'Timeframes and Contacts';
export type DrefSheetName = 'Operation Overview' | 'Event Detail' | 'Actions Needs' | 'Operation' | 'Timeframes and Contacts';
export const SHEET_OPERATION_OVERVIEW = 'Operation Overview' satisfies DrefSheetName;
export const SHEET_EVENT_DETAIL = 'Event Detail' satisfies DrefSheetName;
export const SHEET_ACTIONS_NEEDS = 'Actions-Needs' satisfies DrefSheetName;
export const SHEET_ACTIONS_NEEDS = 'Actions Needs' satisfies DrefSheetName;
export const SHEET_OPERATION = 'Operation' satisfies DrefSheetName;
export const SHEET_TIMEFRAMES_AND_CONTACTS = 'Timeframes and Contacts' satisfies DrefSheetName;
69 changes: 46 additions & 23 deletions app/src/utils/importTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@ import {
isObject,
listToMap,
mapToMap,
randomString,
} from '@togglecorp/fujs';

type ValidationType = string | number | boolean;
type ValidationType = string | number | boolean | 'textArea';
type TypeToLiteral<T extends ValidationType> = T extends string
? 'string' | 'date'
? 'string' | 'date' | 'textArea'
: T extends number
? 'integer' | 'number'
: T extends boolean
? 'boolean'
: never;
? 'integer' | 'number'
: T extends boolean
? 'boolean'
: never;

type ExtractValidation<T> = T extends ValidationType
? T
: never;

interface BaseField {
label: string;
description?: string;
headingBefore?: string;
}

interface InputField<
Expand All @@ -40,8 +43,8 @@ interface SelectField<
// Make this more strict
optionsKey: {
[key in keyof OPTIONS_MAPPING]: VALIDATION extends OPTIONS_MAPPING[key][number]['key']
? key
: never
? key
: never
}[keyof OPTIONS_MAPPING]
}

Expand Down Expand Up @@ -76,8 +79,8 @@ export interface TemplateOptionItem<T extends ValidationType> {

export interface TemplateFieldOptionsMapping {
[key: string]: TemplateOptionItem<string>[]
| TemplateOptionItem<number>[]
| TemplateOptionItem<boolean>[]
| TemplateOptionItem<number>[]
| TemplateOptionItem<boolean>[]
}

export type TemplateSchema<
Expand All @@ -86,13 +89,13 @@ export type TemplateSchema<
> = VALUE extends (infer LIST_ITEM)[]
? (
ListField<LIST_ITEM, OPTIONS_MAPPING>
| InputField<ExtractValidation<LIST_ITEM>>
| SelectField<ExtractValidation<LIST_ITEM>, OPTIONS_MAPPING>
| InputField<ExtractValidation<LIST_ITEM>>
| SelectField<ExtractValidation<LIST_ITEM>, OPTIONS_MAPPING>
) : (
VALUE extends object
? ObjectField<VALUE, OPTIONS_MAPPING>
: (InputField<ExtractValidation<VALUE>>
| SelectField<ExtractValidation<VALUE>, OPTIONS_MAPPING>)
? ObjectField<VALUE, OPTIONS_MAPPING>
: (InputField<ExtractValidation<VALUE>>
| SelectField<ExtractValidation<VALUE>, OPTIONS_MAPPING>)
);

interface HeadingTemplateField {
Expand All @@ -111,11 +114,12 @@ type InputTemplateField = {
label: string;
outlineLevel: number;
description?: string;
headingBefore?: string;
} & ({
dataValidation: 'list';
optionsKey: ObjectKey;
} | {
dataValidation?: 'number' | 'integer' | 'date';
dataValidation?: 'number' | 'integer' | 'date' | 'textArea';
optionsKey?: never;
})

Expand Down Expand Up @@ -168,19 +172,31 @@ export function createImportTemplate<
return [];
}

const fields: TemplateField[] = [];

if (isDefined(schema.headingBefore)) {
fields.push({
type: 'heading',
name: `${fieldName}__headingBefore`,
label: schema.headingBefore,
outlineLevel,
} satisfies HeadingTemplateField);
}

if (schema.type === 'input') {
const field = {
type: 'input',
name: fieldName,
label: schema.label,
description: schema.description,
dataValidation: (schema.validation === 'number' || schema.validation === 'date' || schema.validation === 'integer')
dataValidation: (schema.validation === 'number' || schema.validation === 'date' || schema.validation === 'integer' || schema.validation === 'textArea')
? schema.validation
: undefined,
outlineLevel,
} satisfies InputTemplateField;

return [field];
fields.push(field);
return fields;
}

if (schema.type === 'select') {
Expand All @@ -194,7 +210,8 @@ export function createImportTemplate<
optionsKey: schema.optionsKey,
} satisfies InputTemplateField;

return [field];
fields.push(field);
return fields;
}

const headingField = {
Expand Down Expand Up @@ -234,6 +251,7 @@ export function createImportTemplate<
});

return [
...fields,
headingField,
...optionFields,
];
Expand All @@ -246,7 +264,7 @@ export function getValueFromImportTemplate<
>(
schema: TemplateSchema<TEMPLATE_SCHEMA, OPTIONS_MAPPING>,
optionsMap: OPTIONS_MAPPING,
formValues: Record<string, string>,
formValues: Record<string, string | number | boolean>,
fieldName: string | undefined = undefined,
): unknown {
const optionsReverseMap = mapToMap(
Expand Down Expand Up @@ -291,7 +309,7 @@ export function getValueFromImportTemplate<
const value = formValues[fieldName];
const valueKey = optionsReverseMap[
schema.optionsKey as string
]?.[value];
]?.[String(value)];
// TODO: add validation from schema.validation
return valueKey;
}
Expand All @@ -308,12 +326,17 @@ export function getValueFromImportTemplate<
);

if (isObject(value) && hasSomeDefinedValue(value)) {
if (schema.keyFieldName) {
return {
[schema.keyFieldName]: option.key,
...value,
};
}
return {
[schema.keyFieldName ?? 'client_id']: option.key,
client_id: randomString(),
...value,
};
}

return undefined;
}).filter(isDefined);

Expand Down
Loading

0 comments on commit 5e71084

Please sign in to comment.