Advertisement
shadiff

membersearchmorning.jsx

Sep 21st, 2023
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 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. // Filter the results based on the search query
  61. const filteredResults = data.filter((member) => {
  62. // Assuming you want to search for the memberNumber field
  63. return member.memberNumber === this.state.searchQuery;
  64. });
  65.  
  66. // Update the state with the filtered results
  67. this.setState({ results: filteredResults, error: null });
  68. console.log(filteredResults);
  69. })
  70. .catch((error) => {
  71. // Handle errors and update the state
  72. this.setState({ error });
  73. });
  74. };
  75.  
  76. render() {
  77. const { searchQuery, results, error, filterOptions, sortOption } = this.state;
  78.  
  79. return (
  80. <div>
  81. <h1>Scheme Member Search</h1>
  82. <div>
  83. <label htmlFor="searchInput">Search:</label>
  84. <input
  85. type="text"
  86. id="searchInput"
  87. value={searchQuery}
  88. onChange={this.handleSearchInputChange}
  89. />
  90. <button onClick={this.performSearch}>Search</button>
  91. </div>
  92. <div>
  93. <label htmlFor="categoryFilter">Filter by Category:</label>
  94. <select
  95. id="categoryFilter"
  96. value={filterOptions.category}
  97. onChange={this.handleCategoryFilterChange}
  98. >
  99. <option value="">All</option>
  100. <option value="category1">Category 1</option>
  101. <option value="category2">Category 2</option>
  102. {/* Add more options as needed */}
  103. </select>
  104. </div>
  105. <div>
  106. <label htmlFor="sortOptions">Sort by:</label>
  107. <select
  108. id="sortOptions"
  109. value={sortOption}
  110. onChange={this.handleSortOptionChange}
  111. >
  112. <option value="name_asc">Name (A-Z)</option>
  113. <option value="name_desc">Name (Z-A)</option>
  114. <option value="date_asc">Date (Oldest First)</option>
  115. <option value="date_desc">Date (Newest First)</option>
  116. {/* Add more sorting options as needed */}
  117. </select>
  118. </div>
  119. {error ? (
  120. <p>Error: {error.message}</p>
  121. ) : (
  122. <div>
  123. <h2>Results:</h2>
  124. <ul>
  125. {results.map((result, index) => (
  126. <li key={index}>
  127. <p>First Name: {result.member.firstName}</p>
  128. <p>Middle Name: {result.member.middleName}</p>
  129. <p>Last Name: {result.member.lastName}</p>
  130. </li>
  131. ))}
  132. </ul>
  133. </div>
  134. )}
  135. </div>
  136. );
  137. }
  138. }
  139.  
  140. export default SchemeMemberSearch;
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement