Advertisement
STANAANDREY

Untitled

Mar 24th, 2022
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. import React from 'react';
  2. import { axiosAuthInstanceToAPI } from '../../utils/serverAPI';
  3. import Problem from './Problem';
  4. import ProblemBox from './ProblemBox';
  5. import { CATEGORIES } from './../../utils/constants';
  6.  
  7. export default function Problems({ socket }) {
  8.  
  9. const [fetchedData, setfetchedData] = React.useState([]);
  10. const [problems, setProblems] = React.useState([]);
  11. const [displayedPb, setDisplayed] = React.useState('');
  12. const [srcInp, setSrcInp] = React.useState('');
  13. const [selInp, setSelInp] = React.useState('*');
  14.  
  15. React.useEffect(() => {
  16. axiosAuthInstanceToAPI.get('/user/problems').then(res => {
  17. //console.log(res.data);
  18. setfetchedData(res.data);
  19. setProblems(res.data);
  20. }, err => {
  21. console.error(err);
  22. alert("ERROR!");
  23. })
  24. }, [])
  25.  
  26. const filterData = React.useCallback(() => {
  27. setProblems(fetchedData.filter(problem => {
  28. let ok = true;
  29. ok &= problem.name.startsWith(srcInp);
  30. if (selInp !== '*') {
  31. ok &= problem.category === selInp;
  32. }
  33. return ok;
  34. }));
  35. }, [srcInp, selInp, fetchedData])
  36.  
  37. React.useEffect(() => filterData(), [srcInp, selInp, filterData])
  38.  
  39. const handleInputChange = event => {
  40. event.preventDefault();
  41. const { value: prefix } = event.target;
  42. setSrcInp(prefix);
  43. }
  44.  
  45. const handleSelChange = event => {
  46. event.preventDefault();
  47. const { value: category } = event.target;
  48. setSelInp(category);
  49. }
  50.  
  51. React.useEffect(() => {
  52. socket?.on('back', () => {
  53. setDisplayed('');
  54. setSrcInp('');
  55. setSelInp('*');
  56. })
  57. }, [socket])
  58.  
  59. const handleBackClick = event => {
  60. event.preventDefault();
  61. setDisplayed('');
  62. setSrcInp('');
  63. setSelInp('*');
  64. socket?.emit('back');
  65. }
  66.  
  67. return (
  68. <div>
  69.  
  70.  
  71. {displayedPb && <button onClick={handleBackClick}
  72. className="bg-purple-500 hover:bg-purple-800 text-white font-bold py-2 px-4 rounded"
  73. >
  74. back
  75. </button>}
  76.  
  77. <br />
  78. {(displayedPb && <Problem
  79. socket={socket}
  80. problem={problems.find(problem => problem.name === displayedPb)}
  81. />)
  82. ||
  83. <div className='px-6 py-10 '>
  84. <p>Probleme:</p>
  85. <input type="text" placeholder='search by name'
  86. className='appearance-none block bg-white text-gray-700 border border-gray-400 shadow-inner rounded-md py-3 px-4 leading-tight focus:outline-none focus:border-gray-500' onChange={handleInputChange} /> <br />
  87. Categorie:
  88. <select className=' block bg-white text-gray-700 border border-gray-400 shadow-inner rounded-md px-4 leading-tight focus:outline-none focus:border-gray-500' name="category" id="category" onChange={handleSelChange}>
  89. {
  90.  
  91. ['*', ...CATEGORIES].map(category => <option key={Math.random()} className='' value={category}>{category}</option>)
  92. }
  93. </select>
  94. <br />
  95. {
  96. problems.map(problem => <div key={Math.random()} className='flex justify-center mb-5'><ProblemBox socket={socket} setDisplayed={setDisplayed} problem={problem} /> </div>)
  97. }
  98. </div>
  99. }
  100. </div>
  101. )
  102. }//*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement