Angular job portal app
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

130 lines
4.6 KiB

  1. import { IonButton, IonCheckbox, IonItem, IonLabel, IonList, IonRadio, IonRadioGroup } from '@ionic/react';
  2. import { useEffect, useRef, useState } from 'react';
  3. import { Link } from 'react-router-dom';
  4. import styles from './Options.module.scss';
  5. import { OptionType } from '../../models/QuizDetails'
  6. import RadioButton from '../../components/RadioButton/RadioButton';
  7. interface OwnProps {
  8. options: string[] | undefined;
  9. type: OptionType;
  10. answer: string[];
  11. lastQuestion: boolean;
  12. questionNumber: number;
  13. updateQuestionNumber: () => void;
  14. }
  15. const Options: React.FC<OwnProps> = (props) => {
  16. const [selected, setSelected] = useState<string | undefined>(undefined);
  17. const [selectedOptions, setSelectedOptions] = useState<string[]>([]);
  18. const [textInput, setTextInput] = useState<string>("");
  19. const [answers, setAnswers] = useState<number>(0);
  20. const inputRef = useRef<HTMLTextAreaElement>(null);
  21. useEffect(() => {
  22. setSelected("");
  23. setSelectedOptions([]);
  24. setTextInput("");
  25. }, [props.questionNumber]);
  26. const selectChecked = (option: string) => {
  27. let newOption: string[] = [];
  28. if (selectedOptions.includes(option)) {
  29. newOption = selectedOptions.filter(oldOption => oldOption !== option);
  30. } else {
  31. newOption = selectedOptions.concat([option]);
  32. }
  33. setSelectedOptions(newOption);
  34. }
  35. const validateAnswer = () => {
  36. if (props.type === "singleSelect") {
  37. if (props.answer.includes(selected!)) {
  38. setAnswers(answers + 1);
  39. }
  40. } else if (props.type === "multiSelect") {
  41. if (props.answer.length === selectedOptions.length &&
  42. props.answer.sort().join(',') === selectedOptions.sort().join(',')) {
  43. setAnswers(answers + 1);
  44. }
  45. } else if (props.type === "textInput") {
  46. if (props.answer.includes(textInput)) {
  47. setAnswers(answers + 1);
  48. }
  49. }
  50. }
  51. const handleInput = () => {
  52. setTextInput(inputRef.current?.value!);
  53. }
  54. const setAnswer = () => {
  55. localStorage.setItem("answer", answers.toString());
  56. }
  57. const options = props.options!.map((option, key) => {
  58. return (
  59. <div key={key} onClick={() => setSelected(option)}>
  60. <RadioButton option={option} isChecked={selected === option} />
  61. </div>
  62. );
  63. });
  64. const MultiSelectOptions = props.options!.map((option, key) => {
  65. return (
  66. <div key={key} onClick={() => selectChecked(option)}>
  67. <RadioButton
  68. isCheckBox={true}
  69. option={option}
  70. isChecked={selectedOptions.includes(option)} />
  71. </div>
  72. );
  73. });
  74. return (
  75. <div className={styles.optionHolder}>
  76. <IonList>
  77. {props.type === "singleSelect" &&
  78. <div className={styles.options}>
  79. {options}
  80. </div>
  81. }
  82. {props.type === "multiSelect" &&
  83. <div className={styles.options}>
  84. {MultiSelectOptions}
  85. </div>
  86. }
  87. {props.type === "textInput" &&
  88. <div className={styles.options}>
  89. <textarea
  90. className={styles.textInput}
  91. rows={20}
  92. cols={35}
  93. placeholder="enter your answer here"
  94. ref={inputRef}
  95. onChange={handleInput}>
  96. </textarea>
  97. </div>
  98. }
  99. </IonList>
  100. {!props.lastQuestion &&
  101. <div className={styles.button + " " + ((selected || selectedOptions.length > 0 || textInput) ? styles.active : "")}
  102. onClick={(selected || selectedOptions.length > 0 || textInput) ? () => { props.updateQuestionNumber(); validateAnswer(); } : undefined}>
  103. <IonButton shape="round" expand='block'>Next</IonButton>
  104. </div>
  105. }
  106. {props.lastQuestion &&
  107. < Link to="/preliminaryRoundResults" onClick={setAnswer} className={styles.button + " " + ((selected || selectedOptions.length > 0 || textInput) ? styles.active : "")}>
  108. <IonButton shape="round" expand='block'>Next Step</IonButton>
  109. </Link>
  110. }
  111. </div >
  112. );
  113. }
  114. export default Options;