|
| 1 | +import React, { useContext, useState } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { getConfig } from '@edx/frontend-platform/config'; |
| 4 | +import { logError } from '@edx/frontend-platform/logging'; |
| 5 | +import { InstantSearch } from 'react-instantsearch-dom'; |
| 6 | +import { SearchContext, deleteRefinementAction } from '@edx/frontend-enterprise-catalog-search'; |
| 7 | +import { getAuthenticatedUser } from '@edx/frontend-platform/auth'; |
| 8 | +import FacetListRefinement from '@edx/frontend-enterprise-catalog-search/FacetListRefinement'; |
| 9 | +import { camelCaseObject } from '@edx/frontend-platform/utils'; |
| 10 | +import { |
| 11 | + Button, Form, StatefulButton, |
| 12 | +} from '@edx/paragon'; |
| 13 | +import { CURRENT_JOB_FACET } from '../skills-quiz/constants'; |
| 14 | +import { patchProfile, fetchJobDetailsFromAlgolia } from './data/service'; |
| 15 | +import { CURRENT_JOB_PROFILE_FIELD_NAME, SAVE_BUTTON_LABELS } from './data/constants'; |
| 16 | +import { useAlgoliaSearch } from '../../utils/hooks'; |
| 17 | + |
| 18 | +const SearchJobRole = (props) => { |
| 19 | + const config = getConfig(); |
| 20 | + const [searchClient, searchIndex] = useAlgoliaSearch(config, config.ALGOLIA_INDEX_NAME_JOBS); |
| 21 | + const { username } = getAuthenticatedUser(); |
| 22 | + const { refinements, dispatch } = useContext(SearchContext); |
| 23 | + const { current_job: currentJob } = refinements; |
| 24 | + const [loadingRequest, setLoadingRequest] = useState(false); |
| 25 | + const [requestComplete, setRequestComplete] = useState(false); |
| 26 | + const [requestError, setRequestError] = useState(false); |
| 27 | + |
| 28 | + const { |
| 29 | + title, attribute, typeaheadOptions, facetValueType, customAttribute, |
| 30 | + } = CURRENT_JOB_FACET; |
| 31 | + |
| 32 | + const getDisabledStates = () => { |
| 33 | + if (currentJob) { |
| 34 | + return ['pending', 'complete']; |
| 35 | + } |
| 36 | + return ['pending', 'complete', 'default']; |
| 37 | + }; |
| 38 | + |
| 39 | + const getButtonState = () => { |
| 40 | + if (requestError) { |
| 41 | + return 'error'; |
| 42 | + } |
| 43 | + if (requestComplete) { |
| 44 | + return 'complete'; |
| 45 | + } |
| 46 | + if (loadingRequest) { |
| 47 | + return 'pending'; |
| 48 | + } |
| 49 | + return 'default'; |
| 50 | + }; |
| 51 | + |
| 52 | + const handleSubmit = async () => { |
| 53 | + let resp = {}; |
| 54 | + setLoadingRequest(true); |
| 55 | + const { id: currentJobID } = await fetchJobDetailsFromAlgolia(searchIndex, currentJob); |
| 56 | + const params = { |
| 57 | + extended_profile: [ |
| 58 | + { field_name: CURRENT_JOB_PROFILE_FIELD_NAME, field_value: currentJobID }, |
| 59 | + ], |
| 60 | + }; |
| 61 | + |
| 62 | + try { |
| 63 | + resp = await patchProfile(username, params); |
| 64 | + } catch (error) { |
| 65 | + setLoadingRequest(false); |
| 66 | + setRequestError(true); |
| 67 | + logError(new Error(error)); |
| 68 | + } |
| 69 | + setRequestComplete(true); |
| 70 | + setLoadingRequest(false); |
| 71 | + if (currentJob) { |
| 72 | + dispatch(deleteRefinementAction(customAttribute)); |
| 73 | + } |
| 74 | + props.onSave(camelCaseObject(resp)); |
| 75 | + }; |
| 76 | + |
| 77 | + const handleCancelButtonClick = () => { |
| 78 | + if (currentJob) { |
| 79 | + dispatch(deleteRefinementAction(customAttribute)); |
| 80 | + } |
| 81 | + props.onCancel(); |
| 82 | + }; |
| 83 | + |
| 84 | + return ( |
| 85 | + <div> |
| 86 | + <form> |
| 87 | + <Form.Group> |
| 88 | + <InstantSearch |
| 89 | + indexName={config.ALGOLIA_INDEX_NAME_JOBS} |
| 90 | + searchClient={searchClient} |
| 91 | + > |
| 92 | + <p> Search for your job role </p> |
| 93 | + <FacetListRefinement |
| 94 | + id="current-job-dropdown" |
| 95 | + key={attribute} |
| 96 | + title={refinements[customAttribute]?.length > 0 ? refinements[customAttribute][0] : title} |
| 97 | + attribute={attribute} |
| 98 | + limit={300} |
| 99 | + refinements={refinements} |
| 100 | + facetValueType={facetValueType} |
| 101 | + typeaheadOptions={typeaheadOptions} |
| 102 | + searchable={!!typeaheadOptions} |
| 103 | + doRefinement={false} |
| 104 | + customAttribute={customAttribute} |
| 105 | + showBadge={false} |
| 106 | + variant="default" |
| 107 | + /> |
| 108 | + </InstantSearch> |
| 109 | + </Form.Group> |
| 110 | + <p> |
| 111 | + <StatefulButton |
| 112 | + type="submit" |
| 113 | + className="mr-2" |
| 114 | + labels={{ |
| 115 | + default: SAVE_BUTTON_LABELS.DEFAULT, |
| 116 | + pending: SAVE_BUTTON_LABELS.PENDING, |
| 117 | + complete: SAVE_BUTTON_LABELS.COMPLETE, |
| 118 | + error: SAVE_BUTTON_LABELS.ERROR, |
| 119 | + }} |
| 120 | + state={getButtonState()} |
| 121 | + onClick={(e) => { |
| 122 | + e.preventDefault(); |
| 123 | + handleSubmit(); |
| 124 | + }} |
| 125 | + disabledStates={getDisabledStates()} |
| 126 | + data-testid="save-button" |
| 127 | + /> |
| 128 | + <Button |
| 129 | + variant="outline-primary" |
| 130 | + onClick={handleCancelButtonClick} |
| 131 | + className="cancel-btn" |
| 132 | + data-testid="cancel-button" |
| 133 | + > |
| 134 | + Cancel |
| 135 | + </Button> |
| 136 | + </p> |
| 137 | + </form> |
| 138 | + </div> |
| 139 | + ); |
| 140 | +}; |
| 141 | + |
| 142 | +SearchJobRole.propTypes = { |
| 143 | + onSave: PropTypes.func.isRequired, |
| 144 | + onCancel: PropTypes.func.isRequired, |
| 145 | +}; |
| 146 | + |
| 147 | +export default SearchJobRole; |
0 commit comments