homeflowjs 0.8.7 → 0.8.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homeflowjs",
3
- "version": "0.8.7",
3
+ "version": "0.8.8",
4
4
  "description": "JavaScript toolkit for Homeflow themes",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,58 +1,75 @@
1
- import React, { useEffect, useState } from 'react';
2
- import { connect } from 'react-redux';
1
+ import React, { useState, useEffect } from 'react';
2
+ import { useDispatch, useSelector } from 'react-redux';
3
3
  import { Checkbox } from 'pretty-checkbox-react';
4
4
 
5
5
  import { setSearchField } from '../../actions/search.actions';
6
6
 
7
- const PrettyCheckBox = ({ checked, value, setSearchField, children }) => {
8
- return (
9
- <Checkbox
10
- name="status"
11
- checked={checked}
12
- onChange={() => setSearchField({ status: checked ? '' : value })}
13
- >
14
- {children}
15
- </Checkbox>
16
- );
17
- };
7
+ const PrettyCheckBox = ({ checked, value, handleChange, children }) => (
8
+ <Checkbox
9
+ name="status"
10
+ checked={checked}
11
+ onChange={handleChange}
12
+ value={value}
13
+ >
14
+ {children}
15
+ </Checkbox>
16
+ );
18
17
 
19
- const NormalCheckBox = ({ value, uncheckedValue, checked, defaultChecked, setSearchField, ...otherProps }) => {
20
- const [initialized, setInitialized] = useState(false);
21
-
22
- useEffect(() => {
23
- setSearchField({ status: defaultChecked ? value : (uncheckedValue || '') });
24
- return () => setInitialized(true);
25
- }, [initialized]);
18
+ const NormalCheckBox = ({
19
+ value,
20
+ uncheckedValue,
21
+ checked,
22
+ status,
23
+ handleChange,
24
+ ...otherProps
25
+ }) => {
26
26
 
27
27
  return (
28
28
  <input
29
29
  type="checkbox"
30
30
  name="status"
31
- value={defaultChecked ? uncheckedValue : value}
32
- defaultChecked={defaultChecked || checked}
33
- onChange={() => setSearchField({ status: checked ? (uncheckedValue || '') : value })}
31
+ value={value}
32
+ checked={checked}
33
+ onChange={handleChange}
34
34
  {...otherProps}
35
35
  />
36
36
  );
37
37
  };
38
38
 
39
39
  const StatusCheckbox = (props) => {
40
- const { pretty } = props;
40
+ const { pretty, value, uncheckedValue } = props;
41
41
 
42
- if (pretty) return <PrettyCheckBox {...props} />;
42
+ const dispatch = useDispatch();
43
43
 
44
- return <NormalCheckBox {...props} />;
45
- };
44
+ const defaultSearchStatus = useSelector((state) => state.app.themePreferences.defaultSearchStatus || 'all');
45
+ const currentSearchStatus = useSelector((state) => state.search.currentSearch.status);
46
+
47
+ const calculateChecked = () => {
48
+ // if no current status then checked if value matches default
49
+ if (currentSearchStatus && currentSearchStatus === value) return true;
50
+
51
+ if (currentSearchStatus) return false;
52
+
53
+ // when there is no status set
54
+ return value === defaultSearchStatus;
55
+ };
56
+
57
+ const [checked, setChecked] = useState(calculateChecked());
58
+
59
+ useEffect(() => {
60
+ setChecked(calculateChecked());
61
+ }, [defaultSearchStatus, currentSearchStatus]);
62
+
63
+ const handleChange = (e) => {
64
+ const newValue = checked ? (uncheckedValue || '') : value;
65
+
66
+ dispatch(setSearchField({ status: newValue === defaultSearchStatus ? '' : newValue }));
67
+ setChecked(calculateChecked());
68
+ };
46
69
 
47
- const mapStateToProps = (state, ownProps) => ({
48
- checked: state.search.currentSearch.status === ownProps.value,
49
- });
70
+ if (pretty) return <PrettyCheckBox {...props} handleChange={handleChange} checked={checked} />;
50
71
 
51
- const mapDispatchToProps = {
52
- setSearchField,
72
+ return <NormalCheckBox {...props} handleChange={handleChange} checked={checked} />;
53
73
  };
54
74
 
55
- export default connect(
56
- mapStateToProps,
57
- mapDispatchToProps,
58
- )(StatusCheckbox);
75
+ export default StatusCheckbox;