11import { FilterParameter } from '@circleci/circleci-config-sdk/dist/src/lib/Components/Parameters/types' ;
22import { WorkflowJob } from '@circleci/circleci-config-sdk/dist/src/lib/Components/Workflow' ;
33import { Form , Formik , FormikValues } from 'formik' ;
4- import React , { useState } from 'react' ;
4+ import React , { useEffect , useState } from 'react' ;
55import { useStoreActions } from '../../../state/Hooks' ;
66import { NavigationComponent } from '../../../state/Store' ;
77import { Button } from '../../atoms/Button' ;
@@ -65,7 +65,6 @@ const FilterList = ({ type, values, target }: FilterListProps) => {
6565 expanded
6666 required
6767 listItem = { FilterItem }
68- empty = "filter "
6968 addButton
7069 />
7170 ) ;
@@ -74,9 +73,6 @@ const FilterList = ({ type, values, target }: FilterListProps) => {
7473const StagedFilterMenu = ( { job, values } : WorkflowJobMenuProps ) => {
7574 const navigateBack = useStoreActions ( ( actions ) => actions . navigateBack ) ;
7675 const tabs = [ 'BRANCHES' , 'TAGS' ] ;
77- const [ target , setTarget ] = useState ( tabs [ 0 ] . toLowerCase ( ) ) ;
78-
79- console . log ( values ) ;
8076
8177 return (
8278 < div className = "h-full bg-white flex flex-col overflow-y-auto" >
@@ -97,46 +93,34 @@ const StagedFilterMenu = ({ job, values }: WorkflowJobMenuProps) => {
9793 navigateBack ( {
9894 distance : 1 ,
9995 applyValues : ( currentValues ) => {
100- const filterEmpty = ( values : string [ ] ) => {
101- const filtered = values
102- . map ( ( value ) => value . trim ( ) )
103- . filter ( ( value ) => value ) ;
96+ const strip = ( list : string [ ] ) => {
97+ const filtered = list . filter ( ( item ) => item ) ;
10498
105- if ( filtered . length > 0 ) {
106- return filtered ;
107- }
99+ return filtered . length > 0 ? filtered : undefined ;
108100 } ;
109-
110- const filters = Object . assign (
111- { } ,
112- // filter the the empty values from only and ignore lists
113- ...Object . entries ( values )
114- . map ( ( [ targetKey , target ] ) => {
115- return {
116- [ targetKey ] : Object . entries ( target ) . map (
117- ( [ typeKey , type ] ) => {
118- console . log ( filterEmpty ( type ) ) ;
119- return {
120- [ typeKey ] : filterEmpty ( type ) ,
121- } ;
122- } ,
123- ) ,
124- } ;
125- } )
126- // filter out any target types that have no defined values
127- . filter ( ( target ) =>
128- Object . values ( target ) . some (
129- ( type ) =>
130- type && Object . values ( Object . values ( type ) [ 0 ] ) [ 0 ] ,
131- ) ,
132- ) ,
133- ) ;
101+ const branches = {
102+ only : strip ( values . branches . only ) ,
103+ ignore : strip ( values . branches . ignore ) ,
104+ } ;
105+ const hasBranches =
106+ branches . only ?. length || branches . ignore ?. length ;
107+ const tags = {
108+ only : strip ( values . tags . only ) ,
109+ ignore : strip ( values . tags . ignore ) ,
110+ } ;
111+ const hasTags = tags . only ?. length || tags . ignore ?. length ;
134112
135113 return {
136114 ...currentValues ,
137115 parameters : {
138116 ...currentValues . parameters ,
139- filters,
117+ filters :
118+ hasBranches || hasTags
119+ ? {
120+ branches : hasBranches ? branches : undefined ,
121+ tags : hasTags ? tags : undefined ,
122+ }
123+ : undefined ,
140124 } ,
141125 } ;
142126 } ,
@@ -145,32 +129,29 @@ const StagedFilterMenu = ({ job, values }: WorkflowJobMenuProps) => {
145129 >
146130 { ( formikProps ) => (
147131 < Form className = "flex flex-col flex-1" >
148- < TabbedMenu
149- tabs = { tabs }
150- onChange = { ( index ) => {
151- setTarget ( tabs [ index ] . toLowerCase ( ) ) ;
152- } }
153- >
154- < div className = "p-6" >
155- < FilterList
156- type = "Only"
157- { ...formikProps }
158- target = { target }
159- > </ FilterList >
160- < FilterList
161- type = "Ignore"
162- { ...formikProps }
163- target = { target }
164- > </ FilterList >
165- </ div >
132+ < TabbedMenu tabs = { tabs } >
133+ { [ 'branches' , 'tags' ] . map ( ( target ) => (
134+ < div className = "p-6" key = { target } >
135+ < FilterList
136+ type = "Only"
137+ { ...formikProps }
138+ target = { target }
139+ > </ FilterList >
140+ < FilterList
141+ type = "Ignore"
142+ { ...formikProps }
143+ target = { target }
144+ > </ FilterList >
145+ </ div >
146+ ) ) }
166147 </ TabbedMenu >
167148
168149 < div className = "border-t border-circle-gray-300 p-6 flex" >
169150 < Button
170151 type = "submit"
171152 className = "ml-auto"
172153 variant = "primary"
173- disabled = { ! formikProps . dirty }
154+ // disabled={!formikProps.dirty}
174155 >
175156 Save Filter
176157 </ Button >
0 commit comments