-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from AntaresSimulatorTeam/feature/ANT-2478_cre…
…ate_study Feature/ant 2478 create study
- Loading branch information
Showing
15 changed files
with
619 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
@layer rte-design-system-react | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import { useEffect } from 'react'; | ||
|
||
const FOCUSABLE_ELEMENTS = [ | ||
'button', | ||
'a[href]', | ||
'input', | ||
'select', | ||
'textarea', | ||
'details', | ||
'[tabindex]:not([tabindex="-1"])', | ||
]; | ||
const FOCUSABLE_ELEMENTS_QUERY = FOCUSABLE_ELEMENTS.map((elmt) => elmt + ':not([disabled]):not([aria-hidden])').join( | ||
',', | ||
); | ||
|
||
const getFocusableElements = (containerElement: HTMLElement) => { | ||
const elmts = containerElement.querySelectorAll(FOCUSABLE_ELEMENTS_QUERY) as unknown as HTMLElement[]; | ||
return [elmts[0], elmts[elmts.length - 1]]; | ||
}; | ||
|
||
const useFocusTrapping = <TElement extends HTMLElement>(ref: React.RefObject<TElement | null>, show: boolean) => { | ||
useEffect(() => { | ||
if (!show || !ref.current) { | ||
return; | ||
} | ||
const containerElement = ref.current; | ||
|
||
const handleTabKeyPress = (event: KeyboardEvent) => { | ||
const [firstElement, lastElement] = getFocusableElements(containerElement); | ||
if (event.key === 'Tab') { | ||
if (event.shiftKey && document.activeElement === firstElement) { | ||
event.preventDefault(); | ||
lastElement.focus(); | ||
} else if (!event.shiftKey && document.activeElement === lastElement) { | ||
event.preventDefault(); | ||
firstElement.focus(); | ||
} | ||
} | ||
}; | ||
|
||
containerElement.addEventListener('keydown', handleTabKeyPress); | ||
|
||
return () => { | ||
containerElement.removeEventListener('keydown', handleTabKeyPress); | ||
}; | ||
}, [ref, show]); | ||
}; | ||
|
||
export default useFocusTrapping; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import { notifyToast } from '@/shared/notification/notification'; | ||
|
||
interface StudyData { | ||
name: string; | ||
createdBy: string; | ||
keywords: string[]; | ||
project: string; | ||
horizon: string; | ||
trajectoryIds: number[]; | ||
} | ||
|
||
export const saveStudy = async (studyData: StudyData, toggleModal: () => void) => { | ||
try { | ||
const response = await fetch('http://localhost:8093/v1/study', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(studyData), | ||
}); | ||
if (!response.ok) { | ||
const errorText = await response.text(); | ||
const errorData = JSON.parse(errorText); | ||
throw new Error(`${errorData.message || errorText}`); | ||
} | ||
notifyToast({ | ||
type: 'success', | ||
message: 'Study created successfully', | ||
}); | ||
toggleModal(); | ||
} catch (error: any) { | ||
notifyToast({ | ||
type: 'error', | ||
message: `${error.message}`, | ||
}); | ||
} | ||
}; | ||
export const fetchSuggestedKeywords = async (query: string): Promise<string[]> => { | ||
const response = await fetch(`http://localhost:8093/v1/study/keywords/search?partialName=${query}`); | ||
if (!response.ok) { | ||
throw new Error('Failed to fetch suggested keywords'); | ||
} | ||
const data = await response.json(); | ||
return data; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import React, { useState } from 'react'; | ||
import { RdsButton, RdsIconId, RdsInputText } from 'rte-design-system-react'; | ||
|
||
interface YearDropdownProps { | ||
value: string; | ||
onChange: (value: string) => void; | ||
} | ||
|
||
const generateYears = (startYear: number, endYear: number): number[] => { | ||
const years = []; | ||
for (let year = startYear; year <= endYear; year++) { | ||
years.push(year); | ||
} | ||
return years; | ||
}; | ||
|
||
const HorizonInput: React.FC<YearDropdownProps> = ({ value, onChange }) => { | ||
const [isOpen, setIsOpen] = useState(false); // State to control dropdown visibility | ||
const [errorMessage, setErrorMessage] = useState<string>(''); // State for error message | ||
|
||
const currentYear = new Date().getFullYear(); | ||
const years = generateYears(currentYear, 2050); | ||
|
||
const toggleDropdown = () => { | ||
setIsOpen(!isOpen); // Toggle dropdown visibility | ||
}; | ||
|
||
const handleBlur = () => { | ||
// If the entered value is not in the list, show an error and clear it | ||
if (!years.includes(parseInt(value))) { | ||
setErrorMessage('Veuillez choisir une date valide.'); | ||
onChange(''); | ||
} else { | ||
setErrorMessage(''); // Clear error if the value is valid | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="relative flex w-[320px] flex-col"> | ||
{/* Container for input and button */} | ||
<div className="flex w-full items-center"> | ||
{/* Input field */} | ||
<RdsInputText | ||
label="Horizon" | ||
value={value} | ||
onChange={(t) => { | ||
onChange(t || ''); | ||
setErrorMessage(''); // Clear error message on input change | ||
}} | ||
onBlur={handleBlur} // Validate input on blur | ||
placeHolder="Select a horizon" | ||
variant="outlined" | ||
/> | ||
|
||
{/* Toggle Button */} | ||
<RdsButton | ||
icon={RdsIconId.KeyboardArrowDown} | ||
onClick={toggleDropdown} | ||
size="small" | ||
variant="text" | ||
color="secondary" | ||
/> | ||
</div> | ||
|
||
{/* Error Message */} | ||
{errorMessage && <div className="text-red-500 text-sm mt-1">{errorMessage}</div>} | ||
|
||
{/* Dropdown list */} | ||
{isOpen && ( | ||
<div | ||
className="bg-white max-h-40 absolute z-10 mt-1 w-full overflow-y-auto border border-gray-300" | ||
style={{ | ||
backgroundColor: 'white', // Ensure opaque background | ||
maxHeight: '100px', | ||
top: '100%', | ||
left: 0, | ||
boxShadow: '0 4px 6px rgba(0, 0, 0, 0.1)', // Optional: add shadow for better visibility | ||
}} | ||
onMouseDown={(e) => e.preventDefault()} // Prevent dropdown from closing when clicking inside | ||
> | ||
{years.map((year, index) => ( | ||
<div | ||
key={index} | ||
className="cursor-pointer px-2 py-1 hover:bg-gray-200" | ||
onClick={() => { | ||
onChange(year.toString()); | ||
setIsOpen(false); // Close the dropdown on selection | ||
setErrorMessage(''); // Clear error on valid selection | ||
}} | ||
> | ||
{year} | ||
</div> | ||
))} | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default HorizonInput; |
Oops, something went wrong.