Skip to content

Commit

Permalink
fix pipelineList and dynamicForm React state
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamNowotny committed Aug 28, 2024
1 parent 46699c4 commit b3dabe1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/components/dynamicForm/dynamicForm.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
button {
margin: 5px;
i {
padding-right: 5px;
padding-right: 10px;
}
}
}
11 changes: 5 additions & 6 deletions src/components/dynamicForm/dynamicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export default ({
const [error, setError] = useState<WorkerError>();
let updatedService = { ...service };
const serviceTypes = useContext(ServiceTypesContext);
const serviceDefinition = serviceTypes.find(
definition => definition.baseUrl === service.baseUrl,
);
const serviceFields =
serviceTypes.find(definition => definition.baseUrl === service.baseUrl)?.fields ??
[];
const [isLoading, setIsLoading] = useState<boolean>(false);

const handleShow = () => {
Expand All @@ -45,7 +45,7 @@ export default ({
};
return (
<Form className="settings-form" key={updatedService.name}>
{serviceDefinition?.fields.map(field => {
{serviceFields.map(field => {
return (
<ServiceDefinitionField
key={field.type}
Expand All @@ -59,8 +59,7 @@ export default ({
})}
<div className="settings-buttons">
<button type="button" className="btn btn-primary" onClick={handleShow}>
<i className={`fa fa-refresh ${isLoading ? 'fa-spin' : ''}`}></i>
Show
<i className={`fa fa-refresh ${isLoading ? 'fa-spin' : ''}`}></i>Show
</button>
<button type="button" className="btn btn-success" onClick={handleSave}>
<i className="fa fa-save"></i>Save
Expand Down
15 changes: 8 additions & 7 deletions src/components/pipelineList/pipelineList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export default ({
pipelines,
filter,
selectedItems = [],
onSelected,
onChanged,
}: {
pipelines?: CIPipelineList;
filter?: string;
selectedItems?: string[];
onSelected?: (selected: string[]) => void;
onChanged?: (selected: string[]) => void;
}) => {
if (!pipelines) return null;
let updatedSelected = [...selectedItems];
Expand All @@ -35,11 +35,11 @@ export default ({

const handleChanged = (id: string, checked: boolean) => {
selectPipeline(id, checked);
if (onSelected) onSelected(updatedSelected);
if (onChanged) onChanged(updatedSelected);
};
const handleAllChanged = (ids: string[], checked: boolean) => {
ids.forEach(id => selectPipeline(id, checked));
if (onSelected) onSelected(updatedSelected);
if (onChanged) onChanged(updatedSelected);
};
const groups = Map.groupBy(pipelines.items, ({ group }) => group ?? '');
const groupNames: string[] = Array.from(groups.keys());
Expand Down Expand Up @@ -77,6 +77,7 @@ const GroupPanel = ({
};
const filteredItems = items.filter(filterFunc);
if (filteredItems.length === 0) return null;

const allVisibleChecked = filteredItems.every(item =>
selectedItems.includes(item.id),
);
Expand All @@ -99,7 +100,7 @@ const GroupPanel = ({
<Col xs="auto">
<Form.Check
type={'checkbox'}
defaultChecked={allVisibleChecked}
checked={allVisibleChecked}
onChange={checkAll}
/>
</Col>
Expand Down Expand Up @@ -131,7 +132,7 @@ const GroupPanel = ({
<Col sm="auto">
<Form.Check
type={'checkbox'}
defaultChecked={isSelected}
checked={isSelected}
onChange={e => {
if (onChanged)
onChanged(
Expand All @@ -158,7 +159,7 @@ const GroupPanel = ({
Disabled
</Badge>
)}
</Col>{' '}
</Col>
</Row>
</Form.Group>
);
Expand Down
43 changes: 24 additions & 19 deletions src/options/pages/servicePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,57 @@ import React, { useContext, useState } from 'react';
import { Col, Container, Row } from 'react-bootstrap';
import { useNavigate } from 'react-router-dom';

// TODO: clear pipelines when serviceId changed

export default () => {
const navigate = useNavigate();
const service = useContext(ServiceContext);
if (!service) return null;
let updatedService = { ...service };
const [pipelines, setPipelines] = useState<CIPipelineList>();
const [newService, setNewService] = useState<CIServiceSettings>({ ...service });
const [allPipelines, setAllPipelines] = useState<CIPipelineList>();
const [filter, setFilter] = useState();
const [toastAlertReset, setToastAlertReset] = useState(0);

if (newService.name !== service.name) {
// reset state
setNewService(service);
setAllPipelines(undefined);
}

const handleSave = (settings: CIServiceSettings) => {
setNewService(settings);
core.saveService(settings);
setToastAlertReset(toastAlertReset + 1);
navigate(`/service/${settings.name}`);
};

const updateFilter = value => {
setFilter(value);
};
const updateSelected = (selected: string[]) => {
updatedService.pipelines = selected;
};
const showPipelines = piplines => {
setPipelines(piplines);
setNewService({ ...newService, ...{ pipelines: selected } });
};
const handleSave = (settings: CIServiceSettings) => {
updatedService = { ...settings, pipelines: updatedService.pipelines };
core.saveService(updatedService);
setToastAlertReset(toastAlertReset + 1);
navigate(`/service/${settings.name}`);
const showPipelines = pipelines => {
setAllPipelines(pipelines);
};
return (
<>
<Container fluid>
<Row>
<Col xs={6}>
<DynamicForm
service={updatedService}
service={newService}
onShow={showPipelines}
onSave={handleSave}
/>
<SelectedPipelines pipelines={updatedService.pipelines} />
<SelectedPipelines pipelines={service.pipelines} />
</Col>
<Col xs={6} className="project-selection-container">
{pipelines && <PipelineFilter onUpdate={updateFilter} />}
{allPipelines && <PipelineFilter onUpdate={updateFilter} />}
<PipelineList
key={service.name}
pipelines={pipelines}
pipelines={allPipelines}
filter={filter}
selectedItems={service.pipelines}
onSelected={updateSelected}
selectedItems={newService.pipelines}
onChanged={updateSelected}
/>
</Col>
</Row>
Expand Down

0 comments on commit b3dabe1

Please sign in to comment.