Skip to content

Commit

Permalink
[Core, Admin] Auto format date (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmyb authored Aug 24, 2023
1 parent 2dc206e commit 59ae493
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 48 deletions.
2 changes: 1 addition & 1 deletion apps/admin/public/locales/fr/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"hide_types": "Cacher les types",
"display_params": "Montrer les paramètres",
"hide_params": "Cacher les paramètres",
"connection_problem": "Il y a un problème de connection",
"connection_problem": "Il y a un problème de connexion",
"permissions": "Permissions",
"permissions_settings_title": "Paramètres",
"permissions_relation": "Relation",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function Param({param, actionId, changeParam, setBlockCard, index}: IParamProps)

//////////////////// SETTING VALUES ON CHANGE

const _onChange = (event: React.SyntheticEvent<HTMLInputElement>|React.FormEvent<HTMLTextAreaElement>) => {
const _onChange = (event: React.SyntheticEvent<HTMLInputElement> | React.FormEvent<HTMLTextAreaElement>) => {
const target = event.target as HTMLInputElement;
const value =
param && correspondences[param.type] === 'checkbox' ? target.checked.toString() : target.value.toString();
Expand All @@ -57,52 +57,52 @@ function Param({param, actionId, changeParam, setBlockCard, index}: IParamProps)
};

const _getRenderedElement = () => {
if (param.type === 'textarea') {
return <Form>
<Label attached="top" basic size="large">{param.name}:</Label>
<TextArea
style={{marginBottom: '3px'}}
name={param.name}
placeholder={param.default_value}
value={currentValue ? currentValue : ''}
onChange={_onChange}
onFocus={_onFocus}
onBlur={_onBlur}
/>
</Form>;
if (correspondences[param.type] === 'text') {
return (
<Form>
<Label attached="top" basic size="large">
{param.name}
</Label>
{/* <Label attached="top" italic>
{param.description}
</Label> */}
<TextArea
style={{marginBottom: '3px'}}
name={param.name}
placeholder={param.default_value}
value={currentValue ?? ''}
onChange={_onChange}
onFocus={_onFocus}
onBlur={_onBlur}
/>
</Form>
);
} else {
return <Input
style={{marginBottom: '3px'}}
fluid
label={{basic: true, content: `${param.name}:`}}
labelPosition="left"
type={correspondences[param.type]}
name={param.name}
placeholder={param.default_value}
value={currentValue ? currentValue : ''}
checked={
correspondences[param.type] === 'checkbox' && currentValue
? JSON.parse(currentValue)
: false
}
onChange={_onChange}
onFocus={_onFocus}
onBlur={_onBlur}
/>;
return (
<Input
style={{marginBottom: '3px'}}
description="test"
fluid
label={{basic: true, content: `${param.name}:`}}
labelPosition="left"
type={correspondences[param.type]}
name={param.name}
placeholder={param.default_value}
value={currentValue ?? ''}
checked={
correspondences[param.type] === 'checkbox' && currentValue ? JSON.parse(currentValue) : false
}
onChange={_onChange}
onFocus={_onFocus}
onBlur={_onBlur}
/>
);
}
};

//////////////////// RENDER

return (
<div>
{param && (
<>
{_getRenderedElement()}
</>
)}
</div>
);
return <div>{param && <>{_getRenderedElement()}</>}</div>;
}

export default Param;
2 changes: 1 addition & 1 deletion apps/core/src/domain/actions/excelCalculationAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default function ({
},
{
name: 'Formula',
type: 'textarea',
type: 'string',
description: 'Excel formula to perform, place variables like so : {attribute_identifier}',
required: true,
default_value: '21*2'
Expand Down
31 changes: 26 additions & 5 deletions apps/core/src/domain/actions/formatDateAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
// This file is released under LGPL V3
// License text available at https://www.gnu.org/licenses/lgpl-3.0.txt
import moment from 'moment';
import {ActionsListIOTypes, ActionsListValueType, IActionsListFunction} from '../../_types/actionsList';
import {
ActionsListIOTypes,
ActionsListValueType,
IActionsListContext,
IActionsListFunction
} from '../../_types/actionsList';

export default function (): IActionsListFunction {
return {
Expand All @@ -12,23 +17,39 @@ export default function (): IActionsListFunction {
input_types: [ActionsListIOTypes.NUMBER],
output_types: [ActionsListIOTypes.STRING],
params: [
{
name: 'auto',
type: 'boolean',
description: 'Adapt format to current language',
required: true,
default_value: 'false'
},
{
name: 'format',
type: 'string',
description: 'Date format. Available format: https://momentjs.com/docs/#/displaying/format/',
required: true,
required: false,
default_value: 'DD/MM/YYYY HH:mm:ss'
}
],
action: (value: ActionsListValueType, params: any): string => {
action: (value: ActionsListValueType, params: any, ctx: IActionsListContext): string => {
if (value === null) {
return null;
}

const format = params.format || '';
const format = params.format;
const auto = params.auto === 'true';
const numberVal = Number(value);

return !isNaN(numberVal) ? moment.unix(numberVal).format(format) : '';
let newValue = '';

if (!isNaN(numberVal)) {
newValue = auto
? new Date(numberVal * 1000).toLocaleString(ctx.lang)
: moment.unix(numberVal).format(format);
}

return newValue;
}
};
}

0 comments on commit 59ae493

Please sign in to comment.