From caef794231c0953038728bbd67ce25b6be55937b Mon Sep 17 00:00:00 2001 From: Nuwan Date: Sat, 11 Dec 2021 10:24:05 +0530 Subject: [PATCH] fix for latency data not showing. found a problem in react component structrue of JKPeople and JKPeopleSearch --- jam-ui/src/components/page/JKPeople.js | 7 +- jam-ui/src/components/page/JKPeopleSearch.js | 356 ++++++++++--------- jam-ui/src/store/features/peopleSlice.js | 11 +- web/app/controllers/api_search_controller.rb | 58 +-- web/app/helpers/latency_helper.rb | 5 +- 5 files changed, 232 insertions(+), 205 deletions(-) diff --git a/jam-ui/src/components/page/JKPeople.js b/jam-ui/src/components/page/JKPeople.js index 84509c858..a37e1f326 100644 --- a/jam-ui/src/components/page/JKPeople.js +++ b/jam-ui/src/components/page/JKPeople.js @@ -19,6 +19,7 @@ const JKPeople = ({ className }) => { const [page, setPage] = useState(1); const [resetFilter, setResetFilter] = useState(false); const peopleListRef = useRef(); + const searchRef = useRef(); const dispatch = useDispatch(); const { t } = useTranslation(); @@ -35,14 +36,15 @@ const JKPeople = ({ className }) => { } try { console.log('BEFORE fetching people'); - dispatch(fetchPeople({ page })); + //dispatch(fetchPeople({ page })); + searchRef.current.getResults() } catch (error) { console.log('Error fetching people', error); } }, [page, totalPages, dispatch]); useEffect(() => { - loadPeople(); + loadPeople(); }, [page]); // useEffect(() => { @@ -80,6 +82,7 @@ const JKPeople = ({ className }) => { setShow={setShowSearch} resetFilter={resetFilter} setResetFilter={setResetFilter} + ref={searchRef} />
diff --git a/jam-ui/src/components/page/JKPeopleSearch.js b/jam-ui/src/components/page/JKPeopleSearch.js index cbcc9dd6c..1dee2ed04 100644 --- a/jam-ui/src/components/page/JKPeopleSearch.js +++ b/jam-ui/src/components/page/JKPeopleSearch.js @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, forwardRef, useImperativeHandle } from 'react'; import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap'; import Select from 'react-select'; import JKTooltip from '../common/JKTooltip'; @@ -6,13 +6,13 @@ import PropTypes from 'prop-types'; import { getGenres, getInstruments } from '../../helpers/rest'; import { useForm, Controller, useFormState } from 'react-hook-form'; import { useDispatch } from 'react-redux'; -import { fetchPeople } from '../../store/features/peopleSlice'; +import { fetchPeople, resetState as clearPeople} from '../../store/features/peopleSlice'; -const JKPeopleSearch = props => { - const { show, setShow, resetFilter, setResetFilter } = props; +const JKPeopleSearch = forwardRef((props, ref) => { + const { show, setShow, resetFilter, setResetFilter, submit } = props; const [instruments, setInstruments] = useState([]); const [genres, setGenres] = useState([]); - + const [page, setPage] = useState(1); const dispatch = useDispatch(); const { register, handleSubmit, setValue, control, reset } = useForm({ @@ -29,7 +29,7 @@ const JKPeopleSearch = props => { } }); - const {isDirty} = useFormState({control}); + const { isDirty } = useFormState({ control }); const toggle = () => setShow(!show); @@ -78,41 +78,57 @@ const JKPeopleSearch = props => { }; useEffect(() => { - if(resetFilter){ - reset() - setResetFilter(false) + if (resetFilter) { + reset(); + setResetFilter(false); } - }, [resetFilter]) + }, [resetFilter]); useEffect(() => { fetchGenres(); fetchInstruments(); + //handleSubmit(onSubmit)(); }, []); const submitForm = event => { - event.preventDefault(); + if(event){ + event.preventDefault(); + } + console.log("BEFORE CLEAR"); + dispatch(clearPeople()) handleSubmit(onSubmit)(); setShow(false); }; - const onSubmit = (data) => { - let genres = [] - let joined_within_days, active_within_days = '' - - if(data.genres){ - genres = data.genres.map(genre => genre.value) + useImperativeHandle(ref, () => ({ + getResults(page) { + setPage(page) + console.log("GET RESULTS", page); + handleSubmit(onSubmit)(); + //submitForm() + //setShow(false); + } + })); + + const onSubmit = data => { + console.log("Submitting......"); + let genres = []; + let joined_within_days, + active_within_days = ''; + + if (data.genres) { + genres = data.genres.map(genre => genre.value); } joined_within_days = data.joined_within_days.value; active_within_days = data.active_within_days.value; - - const updatedData = {...data, genres, joined_within_days, active_within_days} - + + const updatedData = { ...data, genres, joined_within_days, active_within_days }; + try { - dispatch(fetchPeople({data: updatedData, page: 1})) + dispatch(fetchPeople({ data: updatedData, page: page })); } catch (error) { console.log('Error fetching people', error); } - }; const lastActiveOpts = [ @@ -132,164 +148,166 @@ const JKPeopleSearch = props => { ]; return ( - - - Update Search - -
- -
- {/* first column */} -
-
-
- -
- setValue('latency_good', e.target.checked)} - /> - -
-
- setValue('latency_fair', e.target.checked)} - /> - -
-
- setValue('latency_high', e.target.checked)} - /> - -
+ + Update Search + +
+ +
+ {/* first column */} +
+
+
+ +
+ setValue('latency_good', e.target.checked)} + /> +
- -
- -
- setValue('proficiency_beginner', e.target.checked)} - /> - -
-
- setValue('proficiency_intermediate', e.target.checked)} - /> - -
-
- setValue('proficiency_expert', e.target.checked)} - /> - -
+
+ setValue('latency_fair', e.target.checked)} + /> + +
+
+ setValue('latency_high', e.target.checked)} + /> +
-
- {/* second column */} -
- -
- ( - - )} - /> -
- -
- ( - - )} - /> +
+ +
+ setValue('proficiency_beginner', e.target.checked)} + /> + +
+
+ setValue('proficiency_intermediate', e.target.checked)} + /> + +
+
+ setValue('proficiency_expert', e.target.checked)} + /> + +
- -
- - - {' '} - - - - + + {/* second column */} +
+ +
+ ( + + )} + /> +
+ +
+ } + /> +
+
+
+ +
+ + + {' '} + + + ); -}; +}); JKPeopleSearch.propTypes = { show: PropTypes.bool, - setShow: PropTypes.func, + setShow: PropTypes.func //setPeople: PropTypes.func }; diff --git a/jam-ui/src/store/features/peopleSlice.js b/jam-ui/src/store/features/peopleSlice.js index ef00e1b81..241f9cc91 100644 --- a/jam-ui/src/store/features/peopleSlice.js +++ b/jam-ui/src/store/features/peopleSlice.js @@ -16,7 +16,6 @@ export const fetchPeople = createAsyncThunk( } ) - export const fetchPerson = createAsyncThunk( 'people/fetchPerson', async (options, thunkAPI) => { @@ -53,7 +52,11 @@ export const peopleSlice = createSlice({ reducers: { add: (state, action) => { state.people.push(action.payload) - } + }, + resetState: () => { + console.log("RESET STATE"); + return { ...initialState } + }, }, extraReducers: (builder) => { builder @@ -94,8 +97,6 @@ export const peopleSlice = createSlice({ export const selectPersonById = (state, userId) => state.people.find((person) => person.id === userId) - - -export const { add } = peopleSlice.actions; +export const { add, resetState } = peopleSlice.actions; export default peopleSlice.reducer; \ No newline at end of file diff --git a/web/app/controllers/api_search_controller.rb b/web/app/controllers/api_search_controller.rb index 34a4976c8..259344f13 100644 --- a/web/app/controllers/api_search_controller.rb +++ b/web/app/controllers/api_search_controller.rb @@ -99,27 +99,31 @@ class ApiSearchController < ApiController #for latency specific filter options and then query the postgresql relational #database for other filter options def filter + latency_good = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:latency_good]) latency_fair = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:latency_fair]) latency_high = ActiveRecord::Type::Boolean.new.type_cast_from_user(params[:latency_high]) - begin - - bm = Benchmark.measure do - @latency_data = users_latency_data(latency_good, latency_fair, latency_high) - end - Rails.logger.info "benchmark neo4j" - Rails.logger.info "*" * 50 - Rails.logger.info bm - Bugsnag.notify("search_users_benchmark") do |report| - report.severity = "info" - report.add_tab(:benchmark, benchmark: bm.to_s) - end + #begin + + #bm = Benchmark.measure do + @latency_data = users_latency_data(latency_good, latency_fair, latency_high) + #end + + # Bugsnag.notify("search_users_benchmark") do |report| + # report.severity = "info" + # report.add_tab(:benchmark, benchmark: bm.to_s) + # end if Rails.env.production? + + + + user_ids = @latency_data.map{ |l_data| l_data[:user_id] } + Rails.logger.info "user_ids from neo4j: #{user_ids}" #debugger - user_ids = @latency_data.map{ |l_data| l_data[:user_id] } - + + filter_params = { "sort_order"=>"latency", "instruments"=>[], @@ -164,19 +168,19 @@ class ApiSearchController < ApiController respond_with @search, responder: ApiResponder, status: 201, template: 'api_search/index' - rescue => exception - logger.debug("Latency exception: #{exception.message}") - Bugsnag.notify(exception) do |report| - report.severity = "error" - report.add_tab(:latency, { - params: params, - user_id: current_user.id, - name: current_user.name, - url: filter_latency_url, - }) - end - render json: {}, status: 500 - end + # rescue => exception + # logger.debug("Latency exception: #{exception.message}") + # Bugsnag.notify(exception) do |report| + # report.severity = "error" + # report.add_tab(:latency, { + # params: params, + # user_id: current_user.id, + # name: current_user.name, + # url: filter_latency_url, + # }) + # end + # render json: {}, status: 500 + # end end diff --git a/web/app/helpers/latency_helper.rb b/web/app/helpers/latency_helper.rb index 250ef0020..95b9b2eef 100644 --- a/web/app/helpers/latency_helper.rb +++ b/web/app/helpers/latency_helper.rb @@ -12,7 +12,7 @@ module LatencyHelper def users_latency_data(latency_good, latency_fair, latency_high) latency_data = [] - if latency_good || latency_fair || latency_high + #if latency_good || latency_fair || latency_high uri = URI(filter_latency_url) begin http = Net::HTTP.new(uri.host, uri.port) @@ -31,6 +31,7 @@ module LatencyHelper if response.is_a?(Net::HTTPOK) || response.is_a?(Net::HTTPSuccess) graph_db_users = JSON.parse(response.body)["users"] + #debugger if latency_good || latency_fair || latency_high #fiter by latency params graph_db_users.select! do |user| @@ -67,7 +68,7 @@ module LatencyHelper rescue => exception raise exception end - end + #end latency_data end