Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. import React, { useState } from "react";
  2. import { connect } from 'react-redux'
  3. import { useTranslation } from 'react-i18next'
  4.  
  5. import 'react-bootstrap-typeahead/css/Typeahead.css'
  6. import 'react-bootstrap-typeahead/css/Typeahead-bs4.css'
  7.  
  8. import * as actions from '../../actions'
  9. import { setFilter } from '../../actions/lists'
  10. import { isPending, getFilter, getPagination } from "../../reducers";
  11. import { TAXONDB_SEARCH_REQUEST_ID, TAXONDB_SEARCH_REQUEST_URL, TAXONDB_DETAILS_REQUEST_ID_PREFIX } from '../../settings'
  12.  
  13. import InputWithAsyncTypeahead from "./InputWithAsyncTypeahead"
  14.  
  15. import { Form, Container, Row, Col, Button } from "react-bootstrap"
  16.  
  17. const TaxonDBSearchForm = props => {
  18. const { t } = useTranslation(['common', 'taxondb'])
  19.  
  20. const [fields, setFields] = useState({})
  21.  
  22.  
  23. const handleFormChange = (index, newValue) => {
  24. const temp = { ...fields }
  25. if (newValue) {
  26. temp[index] = newValue
  27. } else {
  28. temp[index] = undefined
  29. }
  30. setFields(temp)
  31. }
  32.  
  33. const submitForm = () => {
  34. props.save(fields)
  35. const newPagination = { ...props.pagination }
  36. newPagination.currentPage = 1
  37. // remove previous item details to get updated version from server on demand
  38. for (let request in props.requests) {
  39. if (request.startsWith(TAXONDB_DETAILS_REQUEST_ID_PREFIX)) {
  40. props.removeDetails(request)
  41. }
  42. }
  43. props.search({
  44. filter: fields,
  45. pagination: newPagination,
  46. })
  47.  
  48. }
  49.  
  50. /* NEW */
  51. const clearFields = () => {
  52. const temp = { ...fields }
  53. temp["rodzajGatunek"] = undefined
  54. temp["dataZbioru"] = undefined
  55. temp["autorZbioru"] = undefined
  56. temp["krajZbioru"] = undefined
  57. temp["taksonNadrzedny"] = undefined
  58. setFields(temp)
  59. props.save(fields)
  60. }
  61.  
  62.  
  63. return (
  64. <Form>
  65. <Container>
  66. <Row>
  67. <Form.Group as={Col} sm={6} md={4}>
  68. <Form.Label>{t('taxondb:rodzajgatunek')}</Form.Label>
  69. <InputWithAsyncTypeahead filter={fields} url="/taxondb/suggest/rodzajGatunek/" autoFocus={true} id="rodzajGatunek" value={props.filter ? props.filter.rodzajGatunek : ''} onCurrentValueChange={newValue => handleFormChange("rodzajGatunek", newValue)} disabled={props.loading}></InputWithAsyncTypeahead>
  70. </Form.Group>
  71. <Form.Group as={Col} sm={6} md={4}>
  72. <Form.Label>{t('taxondb:taksonnadrzedny')}</Form.Label>
  73. <InputWithAsyncTypeahead filter={fields} url="/taxondb/suggest/taksonNadrzedny/" value={props.filter ? props.filter.taksonNadrzedny : ''} id="taksonNadrzedny" onCurrentValueChange={newValue => handleFormChange("taksonNadrzedny", newValue)} disabled={props.loading}></InputWithAsyncTypeahead>
  74. </Form.Group>
  75. <Form.Group as={Col} sm={6} md={4}>
  76. <Form.Label>{t('taxondb:autorzbioru')}</Form.Label>
  77. <InputWithAsyncTypeahead filter={fields} url="/taxondb/suggest/autorZbioru/" value={props.filter ? props.filter.autorZbioru : ''} id="autorZbioru" onCurrentValueChange={newValue => handleFormChange("autorZbioru", newValue)} disabled={props.loading}></InputWithAsyncTypeahead>
  78. </Form.Group>
  79. <Form.Group as={Col} sm={6} md={4}>
  80. <Form.Label>{t('taxondb:datazbioru')}</Form.Label>
  81. <InputWithAsyncTypeahead filter={fields} url="/taxondb/suggest/dataZbioru/" value={props.filter ? props.filter.dataZbioru : ''} id="dataZbioru" onCurrentValueChange={newValue => handleFormChange("dataZbioru", newValue)} disabled={props.loading}></InputWithAsyncTypeahead>
  82. </Form.Group>
  83. <Form.Group as={Col} sm={6} md={4}>
  84. <Form.Label>{t('taxondb:krajzbioru')}</Form.Label>
  85. <InputWithAsyncTypeahead filter={fields} url="/taxondb/suggest/krajZbioru/" value={props.filter ? props.filter.krajZbioru : ''} id="krajZbioru" onCurrentValueChange={newValue => handleFormChange("krajZbioru", newValue)} disabled={props.loading}></InputWithAsyncTypeahead>
  86. </Form.Group>
  87. <Form.Group as={Col} sm={6} md={4} className="d-flex justify-content-end align-items-end">
  88. <Button variant="primary" onClick={e => { e.preventDefault(); submitForm() }} disabled={props.loading} type="submit">
  89. {t('search')}
  90. </Button>
  91. <Button variant="secondary" className="ml-2" onClick= {() => clearFields()} >
  92. Clear
  93. </Button>
  94. </Form.Group>
  95. </Row>
  96. </Container>
  97. </Form>
  98. )
  99. }
  100.  
  101. const mapStateToProps = (state) => ({
  102. loading: isPending(TAXONDB_SEARCH_REQUEST_ID, state),
  103. filter: getFilter(TAXONDB_SEARCH_REQUEST_ID, state),
  104. pagination: getPagination(TAXONDB_SEARCH_REQUEST_ID, state),
  105. requests: state.requests
  106. })
  107.  
  108. const mapDispatchToProps = dispatch => ({
  109. search: data => dispatch(actions.postDataAnc(TAXONDB_SEARCH_REQUEST_ID, TAXONDB_SEARCH_REQUEST_URL, data, [], [])),
  110. save: filter => dispatch(setFilter(TAXONDB_SEARCH_REQUEST_ID, filter)),
  111. removeDetails: requestId => dispatch(actions.requestRemove(requestId)),
  112. })
  113.  
  114. export default connect(
  115. mapStateToProps,
  116. mapDispatchToProps
  117. )(TaxonDBSearchForm)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement