Skip to content

Commit

Permalink
Merge pull request #4287 from activepieces/fix/default-value-on-choos…
Browse files Browse the repository at this point in the history
…ing-trigger

fix: steps being shown as invalid when they are not after refreshing the browser
  • Loading branch information
AbdulTheActivePiecer committed Mar 27, 2024
2 parents 1612daf + 6091fa9 commit fbf1c7b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 225 deletions.
55 changes: 54 additions & 1 deletion packages/ui/common/src/lib/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ActionType, TriggerType } from '@activepieces/shared';
import { ActionType, TriggerType, isNil } from '@activepieces/shared';
import { PieceProperty, PropertyType } from '@activepieces/pieces-framework';

export function getDisplayNameForTrigger(triggerType: TriggerType) {
switch (triggerType) {
Expand Down Expand Up @@ -35,3 +36,55 @@ export function isOverflown(element: HTMLElement) {
element.scrollWidth > element.clientWidth
);
}

export const getPropertyInitialValue = (
property: PieceProperty,
currentValue: unknown
) => {
if (isNil(currentValue)) {
//used for default values for dynamic property inputs like in custom api calls
return parseControlValue(property, property.defaultValue);
}
return parseControlValue(property, currentValue);
};

const parseControlValue = (property: PieceProperty, value: unknown) => {
switch (property.type) {
case PropertyType.SHORT_TEXT:
case PropertyType.LONG_TEXT:
case PropertyType.NUMBER:
case PropertyType.DATE_TIME:
case PropertyType.FILE:
return isNil(value)
? ''
: typeof value === 'string'
? value
: JSON.stringify(value);
case PropertyType.ARRAY:
return isNil(value) ? [] : value;
case PropertyType.OBJECT:
case PropertyType.DYNAMIC:
return isNil(value) ? {} : value;
case PropertyType.CHECKBOX:
return isNil(value) ? false : value;
case PropertyType.BASIC_AUTH:
case PropertyType.CUSTOM_AUTH:
case PropertyType.OAUTH2:
case PropertyType.SECRET_TEXT:
case PropertyType.MARKDOWN:
case PropertyType.DROPDOWN:
case PropertyType.STATIC_DROPDOWN:
case PropertyType.MULTI_SELECT_DROPDOWN:
case PropertyType.STATIC_MULTI_SELECT_DROPDOWN:
return isNil(value) ? '' : value;
//json value is returned as either an object or string from the server
case PropertyType.JSON:
return isNil(value)
? property.required
? '{}'
: ''
: typeof value === 'string'
? value
: JSON.stringify(value);
}
};
1 change: 1 addition & 0 deletions packages/ui/feature-builder-form-controls/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export * from './lib/components/new-piece-properties-form/piece-properties-form.
export * from './lib/components/action-or-trigger-dropdown/action-or-trigger-dropdown.component';
import '@angular/localize/init';
export * from './lib/components/dynamic-property-control/dynamic-property-control.component';
export * from './lib/components/new-piece-properties-form/properties-controls-helper';
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import {
PiecePropertyMap,
PropertyType,
} from '@activepieces/pieces-framework';
import { jsonValidator } from '@activepieces/ui/common';
import { isNil } from '@activepieces/shared';
import {
getPropertyInitialValue,
jsonValidator,
} from '@activepieces/ui/common';

export const createFormControlsWithTheirValidators = (
fb: UntypedFormBuilder,
Expand Down Expand Up @@ -48,54 +50,6 @@ const removeAllFormControls = (form: UntypedFormGroup) => {
form.removeControl(ctrlName, { emitEvent: false });
});
};
const getControlValue = (property: PieceProperty, value: unknown) => {
if (isNil(value)) {
//used for default values for dynamic property inputs like in custom api calls
return parseControlValue(property, property.defaultValue);
}
return parseControlValue(property, value);
};

const parseControlValue = (property: PieceProperty, value: unknown) => {
switch (property.type) {
case PropertyType.SHORT_TEXT:
case PropertyType.LONG_TEXT:
case PropertyType.NUMBER:
case PropertyType.DATE_TIME:
case PropertyType.FILE:
return isNil(value)
? ''
: typeof value === 'string'
? value
: JSON.stringify(value);
case PropertyType.ARRAY:
return isNil(value) ? [] : value;
case PropertyType.OBJECT:
case PropertyType.DYNAMIC:
return isNil(value) ? {} : value;
case PropertyType.CHECKBOX:
return isNil(value) ? false : value;
case PropertyType.BASIC_AUTH:
case PropertyType.CUSTOM_AUTH:
case PropertyType.OAUTH2:
case PropertyType.SECRET_TEXT:
case PropertyType.MARKDOWN:
case PropertyType.DROPDOWN:
case PropertyType.STATIC_DROPDOWN:
case PropertyType.MULTI_SELECT_DROPDOWN:
case PropertyType.STATIC_MULTI_SELECT_DROPDOWN:
return isNil(value) ? '' : value;
//json value is returned as either an object or string from the server
case PropertyType.JSON:
return isNil(value)
? property.required
? '{}'
: ''
: typeof value === 'string'
? value
: JSON.stringify(value);
}
};

function createControl(
fb: UntypedFormBuilder,
Expand All @@ -106,7 +60,7 @@ function createControl(
if (property.type === PropertyType.DYNAMIC) {
return fb.group({});
}
return new FormControl(getControlValue(property, value), {
return new FormControl(getPropertyInitialValue(property, value), {
validators: validators,
});
}
Expand Down
159 changes: 0 additions & 159 deletions packages/ui/feature-builder-form-controls/src/lib/shared.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ export class PieceInputFormComponent extends InputFormCore {
(x) => x.name === triggerOrActionName
);
if (selectedTriggerOrAction) {
console.log(selectedTriggerOrAction.props);
this.store.dispatch(
FlowsActions.newTriggerOrActionSelected({
displayName: selectedTriggerOrAction.displayName,
Expand Down Expand Up @@ -393,13 +392,11 @@ export class PieceInputFormComponent extends InputFormCore {
Array.isArray(input[key])
) {
cleanedInput[key] = input[key];
} else if (typeof input[key] === 'object' && !Array.isArray(input[key])) {
} else if (typeof input[key] === 'object') {
const cleanedObject = this.removeEmptyValuesFromInput(
input[key] as Record<string, unknown>
);
if (Object.keys(cleanedObject).length > 0) {
cleanedInput[key] = cleanedObject;
}
cleanedInput[key] = cleanedObject;
}
});
return cleanedInput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
environment,
FlowBuilderService,
appConnectionsActions,
getPropertyInitialValue,
} from '@activepieces/ui/common';
import { canvasActions } from '../builder/canvas/canvas.action';
import { ViewModeActions } from '../builder/viewmode/view-mode.action';
Expand Down Expand Up @@ -90,17 +91,16 @@ export class FlowsEffects {
),
switchMap(([{ displayName, name, properties }, step]) => {
if (step) {
const initialValues = Object.keys(properties).reduce((acc, key) => {
acc[key] = getPropertyInitialValue(properties[key], undefined);
return acc;
}, {} as Record<string, unknown>);
const valid = Object.keys(properties).reduce((acc, key) => {
return (
acc &&
(!properties[key]?.required ||
!isNil(properties[key]?.defaultValue))
acc && (!properties[key]?.required || !isNil(initialValues[key]))
);
}, true);
const defaultValues = Object.keys(properties).reduce((acc, key) => {
acc[key] = properties[key]?.defaultValue;
return acc;
}, {} as Record<string, unknown>);

if (step.type === TriggerType.PIECE) {
// TODO fix this for piece schedule
const sampleData =
Expand All @@ -115,7 +115,7 @@ export class FlowsEffects {
settings: {
...step.settings,
triggerName: name,
input: defaultValues,
input: initialValues,
inputUiInfo: {
currentSelectedData: sampleData,
customizedInputs: {},
Expand All @@ -134,7 +134,7 @@ export class FlowsEffects {
settings: {
...step.settings,
actionName: name,
input: defaultValues,
input: initialValues,
inputUiInfo: {
customizedInputs: {},
},
Expand Down

0 comments on commit fbf1c7b

Please sign in to comment.