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

Feat/featws conditions #18

Merged
merged 9 commits into from
Jun 30, 2023
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
5 changes: 4 additions & 1 deletion .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ JAMIE_UI_BASE_URL=http://localhost:3000

JAMIE_KEYCLOAK_BASE_URL=http://localhost:8080

JAMIE_API_BASE_URL=https://api.jamie.g6tech.com.br
JAMIE_API_BASE_URL=https://api.jamie.g6tech.com.br

# The conditions feature requires FeatWS to be installed and configured.
# JAMIE_FEATURE_CONDITIONS=true
3 changes: 3 additions & 0 deletions src/api/services/MenuService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export const ALL_MENU_PROPERTIES = `
id
name
mustDeferChanges
hasConditions
parameters
createdAt
updatedAt
version
Expand Down Expand Up @@ -64,6 +66,7 @@ items {
createdAt
updatedAt
version
rules
}
`;

Expand Down
58 changes: 57 additions & 1 deletion src/components/Menu/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { useTranslation } from 'react-i18next';
import { DatePicker, LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterLuxon } from '@mui/x-date-pickers/AdapterLuxon';
import { DateTime } from 'luxon';
import { MENU_VALIDATION } from '../../../constants';
import { JAMIE_FEATURE_CONDITIONS, MENU_VALIDATION } from '../../../constants';
import { EnumInputAction, FormAction, IMenuMetaWithErrors, MenuMetaType } from '../../../types';
import './styles.css';

Expand Down Expand Up @@ -59,6 +59,10 @@ interface Props {
setMustDeferChanges: (mustDeferChanges: boolean) => void;
meta: IMenuMetaWithErrors[];
setMeta: (meta: IMenuMetaWithErrors[]) => void;
hasConditions: boolean;
setHasConditions: (hasConditions: boolean) => void;
parameters: string;
setParameters: (parameters: string) => void;
loadingSubmit: boolean;
onSubmit: () => void;
onBack: () => void;
Expand All @@ -74,6 +78,10 @@ export const MenuForm = ({
setMustDeferChanges,
meta,
setMeta,
hasConditions,
setHasConditions,
parameters,
setParameters,
loadingSubmit,
onSubmit,
onBack,
Expand Down Expand Up @@ -425,6 +433,52 @@ export const MenuForm = ({
)}
</Draggable>
));

const renderHasConditionCheckbox = () => {
if (!JAMIE_FEATURE_CONDITIONS) return null;
return (
<Box sx={{ mt: '1rem' }}>
<FormControlLabel
control={
<Checkbox
id="hasConditions"
checked={hasConditions}
disabled={action === FormAction.UPDATE}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
const { checked } = e.target;
setHasConditions(checked);
}}
color="primary"
/>
}
label={`${t('menu.fields.hasConditions')}?`}
/>
</Box>
);
};

const renderParameters = () => {
if (!hasConditions) return null;
return (
<TextField
id="parameters"
label="Parameters"
multiline
minRows={3}
value={parameters}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
const { value } = e.target;
setParameters(value);
}}
InputLabelProps={{
shrink: true,
}}
sx={{ width: '16rem' }}
className="bg-white"
/>
);
};

return (
<Form onSubmit={handleFormSubmit}>
<Box sx={{ flex: '0 1 auto', flexDirection: 'column' }}>
Expand Down Expand Up @@ -469,6 +523,8 @@ export const MenuForm = ({
label={t('menu.fields.mustDeferChanges')}
/>
</Box>
{renderHasConditionCheckbox()}
{renderParameters()}
<Typography variant="h3" sx={{ mt: '1rem' }}>
{t('menu.fields.meta.title', { count: 2 })}
</Typography>
Expand Down
29 changes: 29 additions & 0 deletions src/components/Menu/Items/OperationScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export const OperationScreen = ({
menuId,
defaultTemplate,
action,
rules,
...rest
} = node;
let meta = Object.keys(rest.meta).reduce((acc, key) => {
Expand Down Expand Up @@ -166,6 +167,7 @@ export const OperationScreen = ({
startPublication,
endPublication,
meta,
rules,
children: children && formatNodes(children),
id: id === -1 ? undefined : id,
};
Expand Down Expand Up @@ -487,6 +489,31 @@ export const OperationScreen = ({
));
};

const renderConditions = () => {
if (!data.menu.hasConditions) return null;
return (
<Box sx={{ mt: '2rem', width: '100%' }}>
<Typography variant="h3">{t('menu.preview.inputs.conditions.title')}</Typography>
<TextField
type="text"
label="Rules"
multiline
minRows={3}
InputLabelProps={{ shrink: true }}
value={editingNode.rules}
onChange={e => {
setLabelError('');
setEditingNode({ ...editingNode, rules: e.target.value });
}}
sx={{
mt: '2rem',
width: '100%',
}}
/>
</Box>
);
};

switch (operationScreen) {
case EnumInputActionScreen.SELECTING_ACTION:
return (
Expand Down Expand Up @@ -912,6 +939,7 @@ export const OperationScreen = ({
<Divider sx={{ mt: '1.5rem' }} />
</Box>
)}
{renderConditions()}
<Box
sx={{
display: 'flex',
Expand Down Expand Up @@ -1205,6 +1233,7 @@ export const OperationScreen = ({
<Divider sx={{ mt: '1.5rem' }} />
</Box>
)}
{renderConditions()}
<Box
sx={{
display: 'flex',
Expand Down
3 changes: 3 additions & 0 deletions src/constants/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ const getConfig = varname => {
export const JAMIE_UI_BASE_URL = getConfig('${JAMIE_UI_BASE_URL}');
export const JAMIE_KEYCLOAK_BASE_URL = getConfig('${JAMIE_KEYCLOAK_BASE_URL}');
export const JAMIE_API_BASE_URL = getConfig('${JAMIE_API_BASE_URL}');

// The conditions feature requires FeatWS to be installed and configured.
export const JAMIE_FEATURE_CONDITIONS = getConfig('${JAMIE_FEATURE_CONDITIONS}');
4 changes: 4 additions & 0 deletions src/i18n/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
"fields": {
"name": "Name",
"mustDeferChanges": "Mandatory deferral",
"hasConditions": "Has Conditions",
"items": "Items",
"template": "Template",
"templateFormat": {
Expand Down Expand Up @@ -268,6 +269,9 @@
},
"meta": {
"placeholder": "Enter the menu item's {{meta}}..."
},
"conditions": {
"title": "Conditions"
}
},
"buttons": {
Expand Down
4 changes: 4 additions & 0 deletions src/i18n/locales/pt-BR/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
"fields": {
"name": "Nome",
"mustDeferChanges": "Deferimento obrigatório",
"hasConditions": "Tem Condições",
"items": "Itens",
"template": "Template",
"templateFormat": {
Expand Down Expand Up @@ -268,6 +269,9 @@
},
"meta": {
"placeholder": "Digite o valor do {{meta}}..."
},
"conditions": {
"title": "Condições"
}
},
"buttons": {
Expand Down
10 changes: 9 additions & 1 deletion src/pages/Menu/Create/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export const CreateMenu = () => {

const [loadingSubmit, setLoadingSubmit] = React.useState<boolean>(false);

const [hasConditions, setHasConditions] = React.useState<boolean>(false);

const [parameters, setParameters] = React.useState<string>();

const [createMenu] = useMutation(MenuService.CREATE_MENU);

const onBackClickHandler = () => {
Expand All @@ -46,7 +50,7 @@ export const CreateMenu = () => {
return rest;
});
createMenu({
variables: { menu: { name, mustDeferChanges, meta } },
variables: { menu: { name, mustDeferChanges, meta, hasConditions, parameters } },
onCompleted: data => {
setLoadingSubmit(false);
dispatch({
Expand Down Expand Up @@ -92,6 +96,10 @@ export const CreateMenu = () => {
setMustDeferChanges={setMustDeferChanges}
meta={metaWithErrors}
setMeta={setMetaWithErrors}
hasConditions={hasConditions}
setHasConditions={setHasConditions}
parameters={parameters}
setParameters={setParameters}
loadingSubmit={loadingSubmit}
onSubmit={onSubmit}
onBack={onBackClickHandler}
Expand Down
14 changes: 13 additions & 1 deletion src/pages/Menu/Edit/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export const EditMenu = () => {

const [mustDeferChanges, setMustDeferChanges] = React.useState<boolean>(false);

const [hasConditions, setHasConditions] = React.useState<boolean>(false);

const [parameters, setParameters] = React.useState<string>();

const [metaWithErrors, setMetaWithErrors] = React.useState<IMenuMetaWithErrors[]>([]);

const [loadingSubmit, setLoadingSubmit] = React.useState<boolean>(false);
Expand All @@ -45,6 +49,8 @@ export const EditMenu = () => {
if (loaded || !data) return;
setName(data.menu.name);
setMustDeferChanges(data.menu.mustDeferChanges);
setHasConditions(data.menu.hasConditions);
setParameters(data.menu.parameters);
setMetaWithErrors(
data?.menu.meta
.map(m => {
Expand Down Expand Up @@ -83,7 +89,9 @@ export const EditMenu = () => {
});
const updatedName = name !== data.menu.name ? name : undefined;
updateMenu({
variables: { menu: { id: Number(id), name: updatedName, mustDeferChanges, meta } },
variables: {
menu: { id: Number(id), name: updatedName, mustDeferChanges, meta, parameters },
},
onCompleted: data => {
setLoadingSubmit(false);
dispatch({
Expand Down Expand Up @@ -148,6 +156,10 @@ export const EditMenu = () => {
setNameError={setNameError}
mustDeferChanges={mustDeferChanges}
setMustDeferChanges={setMustDeferChanges}
hasConditions={hasConditions}
setHasConditions={setHasConditions}
parameters={parameters}
setParameters={setParameters}
meta={metaWithErrors}
setMeta={setMetaWithErrors}
loadingSubmit={loadingSubmit}
Expand Down
17 changes: 17 additions & 0 deletions src/pages/Menu/Show/Show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
NotificationContext,
openDefaultErrorNotification,
} from '../../../contexts/NotificationContext';
import { JAMIE_FEATURE_CONDITIONS } from '../../../constants';

export const ShowMenu = () => {
const { t, i18n } = useTranslation();
Expand Down Expand Up @@ -124,6 +125,21 @@ export const ShowMenu = () => {
</Box>
));
};

const renderHasConditions = () => {
if (!JAMIE_FEATURE_CONDITIONS) return null;
return (
<Box className="space-x-1.5 flex flex-row items-center">
<Typography variant="h5" component="h5" className="mb-2">
{t('menu.fields.hasConditions')}?
</Typography>
<Typography variant="body1" component="p">
<b>{data?.menu.hasConditions ? t('common.yes') : t('common.no')}</b>
</Typography>
</Box>
);
};

if (loading) return <Loading />;

if (error)
Expand Down Expand Up @@ -216,6 +232,7 @@ export const ShowMenu = () => {
<b>{data?.menu.mustDeferChanges ? t('common.yes') : t('common.no')}</b>
</Typography>
</Box>
{renderHasConditions()}
</Box>
<Divider />
<Typography variant="h3" component="h3" sx={{ py: '1rem' }}>
Expand Down
5 changes: 4 additions & 1 deletion src/types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface IMenu {
id: number;
name: string;
mustDeferChanges: boolean;
hasConditions: boolean;
parameters?: string;
meta: IMenuMeta[];
items: IMenuItem[];
template?: string;
Expand All @@ -22,6 +24,7 @@ export interface IMenu {

export interface IMenuItem {
id: number;
menuId?: number;
label: string;
order: number;
meta: IMenuItemMeta;
Expand All @@ -39,7 +42,7 @@ export interface IMenuItem {
[EnumTemplateFormat.XML]: string;
[EnumTemplateFormat.PLAIN]: string;
};
menuId?: number;
rules?: string;
createdAt?: string;
updatedAt?: string;
version?: number;
Expand Down