|
| 1 | +import ExtensionRoot from '@views/components/common/ExtensionRoot/ExtensionRoot'; |
| 2 | +import React, { useEffect, useState } from 'react'; |
| 3 | +import ReactDOM from 'react-dom'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Component that transforms the days dropdown into a series of checkboxes |
| 7 | + * on the course catalog search page |
| 8 | + * |
| 9 | + * @returns The rendered checkbox component or null if the container is not found. |
| 10 | + */ |
| 11 | +export default function DaysCheckbox(): JSX.Element | null { |
| 12 | + const [container, setContainer] = useState<HTMLDivElement | null>(null); |
| 13 | + const [daysValue, setDaysValue] = useState<string>('000000'); |
| 14 | + |
| 15 | + const days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; |
| 16 | + |
| 17 | + useEffect(() => { |
| 18 | + const daysDropdown = document.getElementById('mtg_days_st') as HTMLSelectElement | null; |
| 19 | + if (!daysDropdown) { |
| 20 | + console.error('Days dropdown not found'); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + const formElement = daysDropdown.closest('.form_element')!; |
| 25 | + const checkboxContainer = document.createElement('div'); |
| 26 | + |
| 27 | + // Create a hidden input to store the value |
| 28 | + const hiddenInput = document.createElement('input'); |
| 29 | + hiddenInput.type = 'hidden'; |
| 30 | + hiddenInput.name = 'mtg_days_st'; |
| 31 | + hiddenInput.id = 'mtg_days_st_hidden'; |
| 32 | + hiddenInput.value = daysDropdown.value; |
| 33 | + |
| 34 | + // Remove old dropdown |
| 35 | + formElement.innerHTML = ''; |
| 36 | + |
| 37 | + // Add the label back |
| 38 | + const newLabel = document.createElement('label'); |
| 39 | + newLabel.className = 'primary_label'; |
| 40 | + newLabel.htmlFor = 'mtg_days_st_hidden'; |
| 41 | + newLabel.textContent = 'AND days'; |
| 42 | + |
| 43 | + formElement.appendChild(newLabel); |
| 44 | + formElement.appendChild(hiddenInput); |
| 45 | + formElement.appendChild(checkboxContainer); |
| 46 | + setContainer(checkboxContainer); |
| 47 | + |
| 48 | + return () => { |
| 49 | + checkboxContainer.remove(); |
| 50 | + }; |
| 51 | + }, []); |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + // Update hidden input when daysValue changes |
| 55 | + const hiddenInput = document.getElementById('mtg_days_st_hidden') as HTMLInputElement | null; |
| 56 | + if (hiddenInput) { |
| 57 | + hiddenInput.value = daysValue; |
| 58 | + } |
| 59 | + }, [daysValue]); |
| 60 | + |
| 61 | + const handleDayChange = (position: number, checked: boolean) => { |
| 62 | + setDaysValue(prev => { |
| 63 | + const currentValue = prev.split(''); |
| 64 | + currentValue[position] = checked ? '1' : '0'; |
| 65 | + return currentValue.join(''); |
| 66 | + }); |
| 67 | + }; |
| 68 | + |
| 69 | + if (!container) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + return ReactDOM.createPortal( |
| 74 | + <ExtensionRoot> |
| 75 | + <div className='flex flex-col gap-0.5 mt-1'> |
| 76 | + {days.map((day, index) => ( |
| 77 | + <div key={day} className='flex items-center'> |
| 78 | + <input |
| 79 | + type='checkbox' |
| 80 | + id={`day_${day}`} |
| 81 | + checked={daysValue !== '000000' && daysValue.charAt(index) === '1'} |
| 82 | + onChange={e => { |
| 83 | + handleDayChange(index, e.target.checked); |
| 84 | + }} |
| 85 | + className='form-checkbox' |
| 86 | + /> |
| 87 | + <label htmlFor={`day_${day}`} className='ml-1'> |
| 88 | + {day} |
| 89 | + </label> |
| 90 | + </div> |
| 91 | + ))} |
| 92 | + </div> |
| 93 | + </ExtensionRoot>, |
| 94 | + container |
| 95 | + ); |
| 96 | +} |
0 commit comments