|
| 1 | +import styles from './Sidebar.module.scss' |
| 2 | +import { useAppContext } from 'src/context/appContext'; |
| 3 | +import Button from '@components/Button'; |
| 4 | +import { |
| 5 | + ArrowRightLine |
| 6 | +} from '@vectopus/atlas-icons-react'; |
| 7 | + |
| 8 | +import { ALL_PROVIDERS as providers } from 'src/utils/constants'; |
| 9 | + |
| 10 | +export default function RefinePane() { |
| 11 | + const { |
| 12 | + userParams, |
| 13 | + setUserParams, |
| 14 | + setOpenFilters, |
| 15 | + products, |
| 16 | + } = useAppContext(); |
| 17 | + |
| 18 | + function providerSelectHandler(e) { |
| 19 | + setUserParams({ |
| 20 | + ...userParams, |
| 21 | + provider: e.target.value, |
| 22 | + product: 'all' |
| 23 | + }) |
| 24 | + } |
| 25 | + |
| 26 | + function productSelectHandler(e) { |
| 27 | + setUserParams({ |
| 28 | + ...userParams, |
| 29 | + product: e.target.value |
| 30 | + }) |
| 31 | + } |
| 32 | + |
| 33 | + function constraintsSelectHandler(key, e){ |
| 34 | + setUserParams({ |
| 35 | + ...userParams, |
| 36 | + constraints: { |
| 37 | + ...userParams.constraints, |
| 38 | + [key]: e.target.value |
| 39 | + } |
| 40 | + }) |
| 41 | + } |
| 42 | + |
| 43 | + function isProviderSelected(provider) { |
| 44 | + return provider === userParams.provider; |
| 45 | + } |
| 46 | + |
| 47 | + function isProductSelected(product) { |
| 48 | + return product === userParams.product; |
| 49 | + } |
| 50 | + |
| 51 | + function isConstraintSelected(constraint, key) { |
| 52 | + return userParams.constraints && constraint === userParams.constraints[key]; |
| 53 | + } |
| 54 | + |
| 55 | + function getProductOptions(products){ |
| 56 | + return products && products.filter(product => product).map(({ title, id }) => { |
| 57 | + return ( |
| 58 | + <option |
| 59 | + key={id} |
| 60 | + value={id} |
| 61 | + selected={isProductSelected(id)} |
| 62 | + > |
| 63 | + {title} |
| 64 | + </option> |
| 65 | + ); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + function displayProducts() { |
| 70 | + return userParams.provider !== 'all' ? |
| 71 | + getProductOptions(products[userParams.provider]) |
| 72 | + : getProductOptions(Object.values(products).flat()) |
| 73 | + } |
| 74 | + |
| 75 | +// //object of reduced constraints per provider |
| 76 | +// function getConstraints(p){ |
| 77 | +// } |
| 78 | + |
| 79 | +// function displayProviderConstraints(){ |
| 80 | +// return userParams.provider !== 'all' ? ( |
| 81 | +// Object.entries(getConstraints(userParams.provider) ?? {}).map(([label, value]) => { |
| 82 | +// return ( |
| 83 | +// <div> |
| 84 | +// <span className={styles.constraintLabel}>{label}: </span> |
| 85 | +// <select onChange={e => constraintsSelectHandler(label, e)} className={styles.refineSelection}> |
| 86 | +// <option |
| 87 | +// value='' |
| 88 | +// key='default' |
| 89 | +// selected |
| 90 | +// > |
| 91 | +// </option> |
| 92 | +// {value.map((v) => ( |
| 93 | +// <option |
| 94 | +// value={v} |
| 95 | +// key={v} |
| 96 | +// selected={isConstraintSelected(v, label)} |
| 97 | +// > |
| 98 | +// {v} |
| 99 | +// </option> |
| 100 | +// ))} |
| 101 | +// </select> |
| 102 | +// </div> |
| 103 | +// ) |
| 104 | +// }) |
| 105 | +// ) : null; |
| 106 | +// } |
| 107 | + |
| 108 | + return ( |
| 109 | + <div className={styles.filtersFlyout}> |
| 110 | + <div className={styles.filtersTopBar}> |
| 111 | + <h4>Refine</h4> |
| 112 | + <Button |
| 113 | + className={styles.closeButton} |
| 114 | + onClick={() => setOpenFilters(false)} |
| 115 | + > |
| 116 | + <ArrowRightLine size={12} /> |
| 117 | + </Button> |
| 118 | + </div> |
| 119 | + <div className={styles.filtersBody}> |
| 120 | + <div> |
| 121 | + <span className={styles.refineLabel}>Providers: </span> |
| 122 | + <select onChange={providerSelectHandler} className={styles.refineSelection}> |
| 123 | + <option |
| 124 | + value='all' |
| 125 | + key='all' |
| 126 | + selected |
| 127 | + > |
| 128 | + All |
| 129 | + </option> |
| 130 | + {providers.map((provider) => ( |
| 131 | + <option |
| 132 | + value={provider.id} |
| 133 | + key={provider.id} |
| 134 | + selected={isProviderSelected(provider.id)} |
| 135 | + > |
| 136 | + {provider.name} |
| 137 | + </option> |
| 138 | + ))} |
| 139 | + </select> |
| 140 | + </div> |
| 141 | + <div> |
| 142 | + <div className={styles.refineLabel}>Products: </div> |
| 143 | + <select onChange={productSelectHandler} className={styles.refineSelection}> |
| 144 | + <option |
| 145 | + value='all' |
| 146 | + key='all' |
| 147 | + selected |
| 148 | + > |
| 149 | + All |
| 150 | + </option> |
| 151 | + {displayProducts()} |
| 152 | + </select> |
| 153 | + </div> |
| 154 | + {userParams.provider !== 'all' && <div> |
| 155 | + <div className={styles.refineLabel}>Constraints: </div> |
| 156 | + {/* {displayProviderConstraints()} */} |
| 157 | + </div>} |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + ); |
| 161 | +} |
0 commit comments