Advertisement
shadiff

membersearch.jsx

Sep 20th, 2023
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. import React, { Component } from 'react';
  2.  
  3. class SchemeMemberSearch extends Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = {
  7. searchQuery: '', // Initialize search query state
  8. results: [],
  9. error: null,
  10. filterOptions: {
  11. category: '', // Initialize with an empty filter
  12. },
  13. sortOption: 'name_asc', // Initialize with a default sorting option
  14. };
  15. }
  16.  
  17. // Function to handle changes in the search input field
  18. handleSearchInputChange = (event) => {
  19. this.setState({ searchQuery: event.target.value });
  20. };
  21.  
  22. // Function to handle changes in the category filter
  23. handleCategoryFilterChange = (event) => {
  24. const selectedCategory = event.target.value;
  25. this.setState({
  26. filterOptions: {
  27. category: selectedCategory,
  28. },
  29. });
  30. };
  31.  
  32. // Function to handle changes in the sort option
  33. handleSortOptionChange = (event) => {
  34. const selectedSortOption = event.target.value;
  35. this.setState({
  36. sortOption: selectedSortOption,
  37. });
  38. };
  39.  
  40. // Function to perform the search
  41. performSearch = () => {
  42. // Replace with your actual access token
  43. const accessToken = localStorage.getItem('accessToken');
  44. const schemeId = 7950;
  45. const apiUrl = `http://192.0.1.23:5000/scheme-member/search?schemeId=${schemeId}&category=${this.state.filterOptions.category}&sort=${this.state.sortOption}`;
  46.  
  47. // Fetch data with authorization header
  48. fetch(apiUrl, {
  49. headers: {
  50. Authorization: `${accessToken}`, // Use 'Bearer' for authorization
  51. },
  52. })
  53. .then((response) => {
  54. if (!response.ok) {
  55. throw new Error(`Network response was not ok: ${response.status}`);
  56. }
  57. return response.json();
  58. })
  59. .then((data) => {
  60. // Update the state with the results
  61. this.setState({ results: data, error: null });
  62. console.log(data);
  63. })
  64. .catch((error) => {
  65. // Handle errors and update the state
  66. this.setState({ error });
  67. });
  68. };
  69.  
  70. render() {
  71. const { searchQuery, results, error, filterOptions, sortOption } = this.state;
  72.  
  73. return (
  74. <div>
  75. <h1>Scheme Member Search</h1>
  76. <div>
  77. <label htmlFor="searchInput">Search:</label>
  78. <input
  79. type="text"
  80. id="searchInput"
  81. value={searchQuery}
  82. onChange={this.handleSearchInputChange}
  83. />
  84. <button onClick={this.performSearch}>Search</button>
  85. </div>
  86. <div>
  87. <label htmlFor="categoryFilter">Filter by Category:</label>
  88. <select
  89. id="categoryFilter"
  90. value={filterOptions.category}
  91. onChange={this.handleCategoryFilterChange}
  92. >
  93. <option value="">All</option>
  94. <option value="category1">Category 1</option>
  95. <option value="category2">Category 2</option>
  96. {/* Add more options as needed */}
  97. </select>
  98. </div>
  99. <div>
  100. <label htmlFor="sortOptions">Sort by:</label>
  101. <select
  102. id="sortOptions"
  103. value={sortOption}
  104. onChange={this.handleSortOptionChange}
  105. >
  106. <option value="name_asc">Name (A-Z)</option>
  107. <option value="name_desc">Name (Z-A)</option>
  108. <option value="date_asc">Date (Oldest First)</option>
  109. <option value="date_desc">Date (Newest First)</option>
  110. {/* Add more sorting options as needed */}
  111. </select>
  112. </div>
  113. {error ? (
  114. <p>Error: {error.message}</p>
  115. ) : (
  116. <div>
  117. <h2>Results:</h2>
  118. <pre>{JSON.stringify(results, null, 2)}</pre>
  119. </div>
  120. )}
  121. </div>
  122. );
  123. }
  124. }
  125.  
  126. export default SchemeMemberSearch;
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement