Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task/option actions page 4991 #3563

Merged
merged 6 commits into from
Jan 15, 2025
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "UNLICENSED",
"private": false,
"devDependencies": {
"@betty-blocks/cli": "^25.102.0",
"@betty-blocks/cli": "^25.102.1",
"@commitlint/cli": "^16.1.0",
"@commitlint/config-angular": "^16.0.0",
"@semantic-release/changelog": "^6.0.1",
Expand Down Expand Up @@ -57,7 +57,7 @@
"dist"
],
"dependencies": {
"@betty-blocks/component-sdk": "^1.86.1",
"@betty-blocks/component-sdk": "^1.88.0",
"@date-io/date-fns": "^1.3.13",
"@material-ui/core": "^4.9.11",
"@material-ui/icons": "^4.11.2",
Expand Down
333 changes: 2 additions & 331 deletions src/prefabs/autocompleteInput.tsx
Original file line number Diff line number Diff line change
@@ -1,342 +1,13 @@
import * as React from 'react';
import {
BeforeCreateArgs,
Icon,
prefab,
PrefabComponentOption,
} from '@betty-blocks/component-sdk';
import { Icon, prefab } from '@betty-blocks/component-sdk';
import { AutocompleteInput } from './structures/AutocompleteInput';

const beforeCreate = ({
close,
components: {
Content,
Field,
Footer,
Header,
FormField,
Toggle,
PropertySelector,
Label,
TextInput: Text,
CircleQuestion,
BBTooltip,
},
prefab: originalPrefab,
save,
helpers,
}: BeforeCreateArgs) => {
const {
BettyPrefabs,
prepareInput,
useModelIdSelector,
useActionIdSelector,
useModelRelationQuery,
usePrefabSelector,
usePropertyQuery,
setOption,
createUuid,
useModelQuery,
createBlacklist,
} = helpers;

const [propertyPath, setProperty] = React.useState<any>('');
const [variableInput, setVariableInput] = React.useState(null);
const modelId = useModelIdSelector();
const actionId = useActionIdSelector();
const selectedPrefab = usePrefabSelector();
const [model, setModel] = React.useState<any>(null);
const [propertyBased, setPropertyBased] = React.useState(!!modelId);
const [prefabSaved, setPrefabSaved] = React.useState(false);

const [validationMessage, setValidationMessage] = React.useState('');

const modelRequest = useModelQuery({
variables: { id: modelId },
onCompleted: (result) => {
setModel(result.model);
},
});

const validate = () => {
if (modelRequest.loading) {
setValidationMessage(
'Model details are still loading, please try submitting again.',
);
return false;
}

return true;
};

let name: string | undefined;
let propertyKind;
let propertyModelId;
const componentId = createUuid();

function isProperty(path: string) {
return (
typeof path !== 'string' &&
typeof path === 'object' &&
!Array.isArray(path)
);
}

let propertyId: string;
if (isProperty(propertyPath)) {
const { id } = propertyPath;
propertyId = Array.isArray(id) ? id[id.length - 1] : id;
} else {
propertyId = propertyPath;
}

const propertyResponse = usePropertyQuery(propertyId);

if (!(propertyResponse.loading || propertyResponse.error)) {
if (propertyResponse.data) {
name = propertyResponse.data.property.label;
propertyKind = propertyResponse.data.property.kind;
propertyModelId = propertyResponse.data.property.referenceModel?.id;
}
}

const modelRelationResponse = useModelRelationQuery(propertyModelId);

let relationalProperties;
let modelIdProperty;
if (!(modelRelationResponse.loading || modelRelationResponse.error)) {
if (modelRelationResponse.data) {
relationalProperties = modelRelationResponse.data.model.properties;
modelIdProperty = relationalProperties.find(
(property) => property.name === 'id',
);
}
}
const unsupportedKinds = createBlacklist(['LIST', 'BELONGS_TO']);

const structure = originalPrefab.structure[0];
if (structure.type !== 'COMPONENT')
return <div>expected component prefab, found {structure.type}</div>;

const handlePropertyChange = (propertyOrId): void => {
setProperty(propertyOrId);
};

if (!actionId && !prefabSaved) {
setPrefabSaved(true);
save(originalPrefab);
}

const actionVariableOptionType = structure.options.find(
(option: { type: string }) => option.type === 'ACTION_JS_VARIABLE',
);

const actionVariableOption = actionVariableOptionType?.key || null;
const labelOptionKey = 'label';
const nameOptionKey = 'actionVariableId';

return (
<>
<Header onClose={close} title="Configure form input field" />
<Content>
{modelId && (
<Field label="Property based input">
<FormField onClick={(): void => setPropertyBased(!propertyBased)}>
<Toggle
color="purple"
checked={propertyBased}
onChange={(): void => {}}
/>
</FormField>
</Field>
)}
{propertyBased ? (
<Field
label="Property"
error={
validationMessage && (
<Text color="#e82600">{validationMessage}</Text>
)
}
>
<PropertySelector
allowRelations
disabledNames={['created_at', 'updated_at']}
disabledKinds={unsupportedKinds}
showFormat={false}
size="large"
onChange={handlePropertyChange}
value={propertyPath}
modelId={modelId}
/>
</Field>
) : (
<Field>
<Label>
Action input variable
<CircleQuestion
color="grey500"
size="medium"
data-tip="You can use this action input variable in the action itself."
data-for="variable-tooltip"
/>
</Label>
<BBTooltip
id="variable-tooltip"
place="top"
type="dark"
effect="solid"
/>
<Text
onChange={(e): void => setVariableInput(e.target.value)}
color="orange"
/>
</Field>
)}
</Content>
<Footer
onClose={close}
canSave={(propertyPath && !!name) || variableInput}
onSave={async (): Promise<void> => {
// eslint-disable-next-line no-param-reassign
structure.id = componentId;

let kind = propertyKind || 'STRING';
const isListProperty = kind === ('LIST' || 'list');

const integerKind = 'BELONGS_TO';

if (!propertyBased && !isListProperty) {
kind = integerKind;
}

const variableName = variableInput || name;
const result = await prepareInput(
actionId,
variableName,
kind,
propertyKind,
propertyResponse?.data?.property,
);

const newPrefab = { ...originalPrefab };
if (newPrefab.structure[0].type !== 'COMPONENT') {
throw new Error('expected Component');
}
setOption(newPrefab.structure[0], actionVariableOption, (option) => ({
...option,
value: variableName,
configuration: {
condition: {
type: 'SHOW',
option: 'property',
comparator: 'EQ',
value: '',
},
},
}));
setOption(newPrefab.structure[0], labelOptionKey, (option) => ({
...option,
value: [variableName],
}));

if (propertyModelId && !isListProperty) {
setOption(newPrefab.structure[0], 'model', (option) => ({
...option,
value: propertyModelId,
}));
}

setOption(newPrefab.structure[0], nameOptionKey, (option) => ({
...option,
value: result.variable.variableId,
}));
if (propertyBased) {
setOption(newPrefab.structure[0], 'optionType', (option) => ({
...option,
value: result.isRelational ? 'model' : 'property',
}));

setOption(
newPrefab.structure[0],
'property',
(originalOption: PrefabComponentOption) => ({
...originalOption,
value: {
id: propertyId,
type: 'PROPERTY',
name: `{{ ${model?.name}.${name} }}`,
},
configuration: {
allowedKinds: ['LIST', 'BELONGS_TO'],
disabled: true,
condition: {
type: 'HIDE',
option: 'property',
comparator: 'EQ',
value: '',
},
},
}),
);
setOption(
newPrefab.structure[0],
'label',
(originalOption: any) => ({
...originalOption,
value: [
{
id: propertyId,
type: 'PROPERTY_LABEL',
name: `{{ ${model?.name}.${name} }}`,
},
],
}),
);
}
if (validate()) {
if (
(selectedPrefab?.name === BettyPrefabs.UPDATE_FORM ||
selectedPrefab?.name === BettyPrefabs.CREATE_FORM ||
selectedPrefab?.name === BettyPrefabs.FORM ||
selectedPrefab?.name === BettyPrefabs.LOGIN_FORM) &&
propertyId
) {
const idProperty = modelIdProperty
? [propertyId, modelIdProperty.id]
: propertyId;

const valueOptions = [
{
id: idProperty,
type: 'PROPERTY',
name: `{{ ${model?.name}.${name}.Id }}`,
},
];

setOption(newPrefab.structure[0], 'value', (option) => ({
...option,
value:
option.type === 'VARIABLE'
? valueOptions
: (idProperty as any),
}));
}
}
save({ ...originalPrefab, structure: [newPrefab.structure[0]] });
}}
/>
</>
);
};

const attributes = {
category: 'FORM',
icon: Icon.AutoCompleteIcon,
keywords: ['Form', 'input'],
};

export default prefab('Autocomplete', attributes, beforeCreate, [
export default prefab('Autocomplete', attributes, undefined, [
AutocompleteInput({
label: 'Autocomplete',
inputLabel: 'Autocomplete',
Expand Down
12 changes: 12 additions & 0 deletions src/prefabs/checkboxGroupInput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { prefab, Icon } from '@betty-blocks/component-sdk';
import { CheckboxGroup } from './structures/CheckboxGroup';

const attributes = {
category: 'FORM',
icon: Icon.CheckboxGroupIcon,
keywords: ['Form', 'input'],
};

export default prefab('Checkbox Group', attributes, undefined, [
CheckboxGroup({ label: 'Checkbox Group' }),
]);
Loading
Loading