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

Fix crash bugs UI 2 #251

Merged
merged 3 commits into from
Jan 26, 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
2 changes: 1 addition & 1 deletion src/Routes/Base/Header/TagsFiltersViews.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const TagsFiltersViews = ({ sectionName }) => {
pathname: urlParams.pathname,
search: `?${_qParams}${_qMoreParam}`,
});
}, [history, instanceFilters, sectionName, urlParams.pathname]);
}, [instanceFilters, sectionName, urlParams.pathname]);

const cancelPropFilter = ['datesRange', 'experimentName', 'limit'];

Expand Down
12 changes: 3 additions & 9 deletions src/Routes/Base/Header/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useState, useMemo } from 'react';
import { filterToggeledVar, instanceFiltersVar } from 'cache';
import React, { useState, useMemo } from 'react';
import { filterToggeledVar } from 'cache';
import useQueryHook from 'hooks/useQuery';
import styled from 'styled-components';
import { Route } from 'react-router-dom';
Expand All @@ -11,7 +11,7 @@ import {
MenuUnfoldOutlined,
FilterOutlined,
} from '@ant-design/icons';
import { useLeftSidebar, useCacheFilters } from 'hooks';
import { useLeftSidebar } from 'hooks';

// import AutoComplete from './AutoComplete';

Expand Down Expand Up @@ -74,12 +74,6 @@ const FilterButton = () => {
const Header = () => {
const { toggle, isCollapsed } = useLeftSidebar();

const { filtersInitCacheItems } = useCacheFilters();

useEffect(() => {
instanceFiltersVar(filtersInitCacheItems);
}, []);

return (
<Container className={USER_GUIDE.WELCOME}>
<ButtonsContainer>
Expand Down
130 changes: 30 additions & 100 deletions src/Routes/SidebarRight/AddAlgorithm/AddAlgorithmForm.react.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import React, {
memo,
useCallback,
useEffect,
useMemo,
useState,
useRef,
} from 'react';
import React, { memo, useEffect, useMemo, useState } from 'react';
import PropTypes from 'prop-types';
import _ from 'lodash';
import { Input, InputNumber, Radio, Select, Checkbox, Modal } from 'antd';
import Text from 'antd/lib/typography/Text';
import { Input, InputNumber, Radio, Select, Checkbox } from 'antd';
import { Form } from 'components/common';
import {
BottomPanel,
Expand All @@ -28,9 +20,7 @@ import {
deepCopyFromKeyValue,
flattenObjKeyValue,
} from 'utils';
import client from 'client';
import { OVERVIEW_TABS } from 'const';
import usePath from './../../Tables/Algorithms/usePath';
import { CodeBuild, GitBuild, ImageBuild } from './BuildTypes';
import MemoryField from './MemoryField.react';
import schema from './schema';
Expand Down Expand Up @@ -95,20 +85,22 @@ const getBuildTypes = ({ buildType, ...props }) => {
};
// #endregion

const AddAlgorithmForm = ({ onToggle, onSubmit, algorithmValue }) => {
const isEdit = algorithmValue !== undefined;

const keyValueObject =
(algorithmValue && JSON.parse(algorithmValue)) || undefined;
const AddAlgorithmForm = ({
onToggle,
onSubmit,
isEdit,
keyValueObject,
setIsSubmitLoading,
onOverviewAlgorithm,
applyAlgorithmVersion,
onAfterSaveAlgorithm,
isCheckForceStopAlgorithms,
isSubmitLoading,
setIsCheckForceStopAlgorithms,
refCheckForceStopAlgorithms,
}) => {
const [form] = Form.useForm();
const [isCheckForceStopAlgorithms, setIsCheckForceStopAlgorithms] = useState(
!isEdit
);
const refCheckForceStopAlgorithms = useRef(false);

const [isSubmitLoading, setIsSubmitLoading] = useState(false);
const [fileList, setFileList] = useState([]);

const [buildType, setBuildType] = useState(
(keyValueObject && toSelectedBuildType(keyValueObject?.type)) ||
BUILD_TYPES.GIT.field
Expand Down Expand Up @@ -151,80 +143,6 @@ const AddAlgorithmForm = ({ onToggle, onSubmit, algorithmValue }) => {
});

const { applyAlgorithm } = useActions();
const { goTo } = usePath();

const onOverviewAlgorithm = useCallback(
tab => {
if (keyValueObject) {
goTo.overview({
nextAlgorithmId: keyValueObject?.name,
nextTabKey: tab || OVERVIEW_TABS.VERSIONS,
});
}
},
[goTo, keyValueObject]
);

const onAfterSaveAlgorithm = useCallback(
dataResponse => {
setIsSubmitLoading(false);

if (dataResponse.buildId) {
onOverviewAlgorithm(OVERVIEW_TABS.BUILDS);
}
},
[onOverviewAlgorithm]
);

const applyAlgorithmVersion = useCallback(
dataResponse => {
// create new version and apply version if force
// const errorNotification = ({ message }) => notification({ message });
client
.post(`/versions/algorithms/apply`, {
name: dataResponse.algorithm.name,
version: dataResponse.algorithm.version,
force: refCheckForceStopAlgorithms.current.state.checked,
})
.then(() => {
setIsSubmitLoading(false);
onAfterSaveAlgorithm(dataResponse);
})
.catch(error => {
const { data } = error.response;

Modal.confirm({
title: 'WARNING : Version not upgrade',
content: (
<>
<div>
<Text>{data.error.message}</Text>
</div>
<Checkbox
onClick={e => {
setIsCheckForceStopAlgorithms(e.target.checked);
}}>
Stop running algorithms.
</Checkbox>
</>
),
okText: 'Try again',
okType: 'danger',
cancelText: 'Cancel',
onCancel() {
setIsSubmitLoading(false);
onOverviewAlgorithm();
},
onOk() {
setIsSubmitLoading(false);
applyAlgorithmVersion(dataResponse);
},
});
});
},
[onAfterSaveAlgorithm, onOverviewAlgorithm]
);

const onFormSubmit = () => {
validateFields().then(formObject => {
if (buildType === BUILD_TYPES.CODE.field && !fileList.length && !isEdit) {
Expand Down Expand Up @@ -398,14 +316,26 @@ const AddAlgorithmForm = ({ onToggle, onSubmit, algorithmValue }) => {
AddAlgorithmForm.propTypes = {
// TODO: detail the props
// eslint-disable-next-line
algorithmValue: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),

keyValueObject: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
onToggle: PropTypes.func.isRequired,
onSubmit: PropTypes.func,
isEdit: PropTypes.bool.isRequired,

setIsSubmitLoading: PropTypes.bool.isRequired,
onOverviewAlgorithm: PropTypes.func.isRequired,
applyAlgorithmVersion: PropTypes.func.isRequired,
onAfterSaveAlgorithm: PropTypes.func.isRequired,
refCheckForceStopAlgorithms: PropTypes.func.isRequired,
isCheckForceStopAlgorithms: PropTypes.bool.isRequired,
isSubmitLoading: PropTypes.bool.isRequired,
setIsCheckForceStopAlgorithms: PropTypes.func.isRequired,
};

AddAlgorithmForm.defaultProps = {
onSubmit: () => {},
algorithmValue: undefined,

keyValueObject: undefined,
};

export default memo(AddAlgorithmForm);
138 changes: 126 additions & 12 deletions src/Routes/SidebarRight/AddAlgorithm/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
import React, { memo, useState } from 'react';
import React, { memo, useState, useCallback, useRef } from 'react';
import client from 'client';
import PropTypes from 'prop-types';
import {
BottomPanel,
RightAlignedButton,
PanelButton,
RightPanel,
} from 'components/Drawer';
import { Checkbox, Modal } from 'antd';
import Text from 'antd/lib/typography/Text';
import { Card, JsonEditor } from 'components/common';
import { addAlgorithmTemplate } from 'config';
import { useActions } from 'hooks';
import { stringify } from 'utils';
import tryParse from 'utils/handleParsing';
import { OVERVIEW_TABS } from 'const';
import usePath from './../../Tables/Algorithms/usePath';
import AddAlgorithmForm from './AddAlgorithmForm.react';

const DEFAULT_EDITOR_VALUE = stringify(addAlgorithmTemplate);
const noop = () => {};

const AddAlgorithm = ({ onSubmit = noop, algorithmValue }) => {
// #region Editor State
const refCheckForceStopAlgorithms = useRef(false);
const [editorIsVisible, setEditorIsVisible] = useState(false);
// eslint-disable-next-line no-unused-vars
const [isEdit, setIsEdit] = useState(algorithmValue !== undefined);
const [isSubmitLoading, setIsSubmitLoading] = useState(false);
const [isCheckForceStopAlgorithms, setIsCheckForceStopAlgorithms] = useState(
!isEdit
);

const [editorValue, setEditorValue] = useState(
algorithmValue || DEFAULT_EDITOR_VALUE
Expand All @@ -29,16 +42,97 @@ const AddAlgorithm = ({ onSubmit = noop, algorithmValue }) => {
const onDefault = () => setEditorValue(DEFAULT_EDITOR_VALUE);

const { applyAlgorithm } = useActions();
// #endregion
const { goTo } = usePath();

const keyValueObject =
(algorithmValue && JSON.parse(algorithmValue)) || undefined;
const onOverviewAlgorithm = useCallback(
tab => {
if (keyValueObject) {
goTo.overview({
nextAlgorithmId: keyValueObject?.name,
nextTabKey: tab || OVERVIEW_TABS.VERSIONS,
});
}
},
[goTo, keyValueObject]
);

const onAfterSaveAlgorithm = useCallback(
dataResponse => {
setIsSubmitLoading(false);

// #region Handle submit
const apply = payload => applyAlgorithm(payload);
if (dataResponse.buildId) {
onOverviewAlgorithm(OVERVIEW_TABS.BUILDS);
}
},
[onOverviewAlgorithm]
);

const applyAlgorithmVersion = useCallback(
dataResponse => {
// create new version and apply version if force
// const errorNotification = ({ message }) => notification({ message });
client
.post(`/versions/algorithms/apply`, {
name: dataResponse.algorithm.name,
version: dataResponse.algorithm.version,
force: refCheckForceStopAlgorithms.current.state.checked,
})
.then(() => {
setIsSubmitLoading(false);
onAfterSaveAlgorithm(dataResponse);
})
.catch(error => {
const { data } = error.response;

Modal.confirm({
title: 'WARNING : Version not upgrade',
content: (
<>
<div>
<Text>{data.error.message}</Text>
</div>
<Checkbox
onClick={e => {
setIsCheckForceStopAlgorithms(e.target.checked);
}}>
Stop running algorithms.
</Checkbox>
</>
),
okText: 'Try again',
okType: 'danger',
cancelText: 'Cancel',
onCancel() {
setIsSubmitLoading(false);
onOverviewAlgorithm();
},
onOk() {
setIsSubmitLoading(false);
applyAlgorithmVersion(dataResponse);
},
});
});
},
[onAfterSaveAlgorithm, onOverviewAlgorithm]
);
const onSuccess = ({ src }) => {
const formData = new FormData();
formData.append(`payload`, src);
onSubmit({ payload: src });
apply(formData);

if (isEdit) {
applyAlgorithm(formData, res => {
if (res.buildId) {
onOverviewAlgorithm(OVERVIEW_TABS.BUILDS);
} else {
applyAlgorithmVersion(res);
}
});
} else {
applyAlgorithm(formData, res => onAfterSaveAlgorithm(res));
}
};

const onEditorSubmit = () => tryParse({ src: editorValue, onSuccess });
Expand All @@ -59,19 +153,39 @@ const AddAlgorithm = ({ onSubmit = noop, algorithmValue }) => {
<PanelButton key="clear" type="danger" onClick={onClear}>
Clear
</PanelButton>
<RightAlignedButton
key="Submit"
type="primary"
onClick={onEditorSubmit}>
Submit
</RightAlignedButton>

<RightPanel>
{isEdit && (
<Checkbox
ref={refCheckForceStopAlgorithms}
checked={isCheckForceStopAlgorithms}
onClick={e => setIsCheckForceStopAlgorithms(e.target.checked)}>
Stop running algorithms
</Checkbox>
)}
<RightAlignedButton
key="Submit"
type="primary"
onClick={onEditorSubmit}>
Submit
</RightAlignedButton>
</RightPanel>
</BottomPanel>
</>
) : (
<AddAlgorithmForm
algorithmValue={algorithmValue}
onToggle={toggleEditor}
onSubmit={onSubmit}
isEdit={isEdit}
keyValueObject={keyValueObject}
setIsSubmitLoading={setIsSubmitLoading}
onOverviewAlgorithm={onOverviewAlgorithm}
applyAlgorithmVersion={applyAlgorithmVersion}
isCheckForceStopAlgorithms={isCheckForceStopAlgorithms}
isSubmitLoading={isSubmitLoading}
setIsCheckForceStopAlgorithms={setIsCheckForceStopAlgorithms}
onAfterSaveAlgorithm={onAfterSaveAlgorithm}
refCheckForceStopAlgorithms={refCheckForceStopAlgorithms}
/>
);
};
Expand Down
Loading