Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert React components to functions #581

Open
wants to merge 85 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 74 commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
e156a9e
refactored Access Token List
Dec 1, 2024
a43d922
convert logout component to function
shantinghou Dec 1, 2024
1269bd8
formatting
shantinghou Dec 1, 2024
575f1f0
refactored login class to function
ilong4rennes Dec 2, 2024
ea865dc
changed class to function in DateTimeInput.jsx
ilong4rennes Dec 2, 2024
219367f
message form refactor
shantinghou Dec 2, 2024
6c7e402
refactor to use functions
Dec 2, 2024
8f9d8b6
refact message form
shantinghou Dec 2, 2024
cd936c2
changed class to function in dateperiodinput
ilong4rennes Dec 2, 2024
b270c4b
product form refactor
shantinghou Dec 2, 2024
a4de00e
refactor admin/src/Membership/MemberAdd.js
Dec 2, 2024
f1a7783
make test fix
shantinghou Dec 2, 2024
7335846
refactor MemberBoxGroups
Dec 2, 2024
6e46093
request password refactor
shantinghou Dec 2, 2024
1029cea
form input componenets refactored
shantinghou Dec 2, 2024
d250307
refactor Member Box Keys
Dec 2, 2024
f222d83
refactor group functions other than groupBox
shantinghou Dec 2, 2024
adfb807
refactor MemberBoxGroups.js
Dec 2, 2024
a91644d
refactor Group Box Permissions
Dec 2, 2024
3ac3fc6
refactor Member Box New Message
Dec 3, 2024
9816428
Refactor MemberBoxOrders.js
Dec 3, 2024
26919cf
Refactor MemberBoxPermissions
Dec 3, 2024
88be7a0
remove unnecessary dependency in refactor
Dec 3, 2024
c497477
modernize legacy api with use of contexts
shantinghou Dec 3, 2024
9eb0ced
refactor KeyEdit
Dec 3, 2024
b985bc8
refactor MemberBox and associated children components
Dec 3, 2024
fa772c5
changed class to function in app.jsx
ilong4rennes Dec 3, 2024
6da2498
changed class to function in checkboxinput
ilong4rennes Dec 3, 2024
f09cf23
changed class to function in memberform
ilong4rennes Dec 3, 2024
93e2275
changed GroupForm.js
ilong4rennes Dec 4, 2024
0390540
changed class to function in fileinput
ilong4rennes Dec 4, 2024
0696837
changed class to function in countrydropdown
ilong4rennes Dec 4, 2024
3935f2d
changed class to function in categoryperiodsinput
ilong4rennes Dec 4, 2024
f90483d
changed class to function in keyhandoutform.jsx
ilong4rennes Dec 4, 2024
00ba257
changed class to functions in CollectionTable.js
ilong4rennes Dec 4, 2024
a6616aa
Merge pull request #1 from ilong4rennes/feature/refactorAccessTokenList
shantinghou Dec 5, 2024
ddfb7f9
Merge pull request #2 from ilong4rennes/refactor-logout
shantinghou Dec 5, 2024
e556caa
Merge pull request #3 from ilong4rennes/refactor/login
shantinghou Dec 5, 2024
ca3aef8
Merge pull request #4 from ilong4rennes/refactor-messageForm
shantinghou Dec 5, 2024
a572a66
Merge pull request #5 from ilong4rennes/password-reset
shantinghou Dec 5, 2024
f502ce6
Merge pull request #6 from ilong4rennes/refactor-input-componenets
shantinghou Dec 5, 2024
b7404c0
Merge pull request #7 from ilong4rennes/refactor-group
shantinghou Dec 5, 2024
9435fa9
Merge pull request #8 from ilong4rennes/refactor/component-files
shantinghou Dec 5, 2024
2ab7b07
Merge pull request #10 from ilong4rennes/refactor/component-files-2
shantinghou Dec 5, 2024
ea2f1a3
Merge pull request #11 from ilong4rennes/features/refactorMemberBoxRe…
shantinghou Dec 5, 2024
74da2c2
Merge pull request #12 from ilong4rennes/features/keyedit
shantinghou Dec 5, 2024
76f3e64
Merge pull request #13 from ilong4rennes/features/refactorMemberBoxOr…
shantinghou Dec 5, 2024
4aa8942
Merge pull request #14 from ilong4rennes/features/refactorMemberBoxPe…
shantinghou Dec 5, 2024
f0adae3
Merge pull request #15 from ilong4rennes/features/refactorMemberBoxNe…
shantinghou Dec 5, 2024
3611163
Merge pull request #16 from ilong4rennes/features/GroupBoxPermissions
shantinghou Dec 5, 2024
7b488aa
Merge pull request #18 from ilong4rennes/features/refactorMemberBoxKeys
shantinghou Dec 5, 2024
2c8d057
Merge pull request #19 from ilong4rennes/features/refactorMemberAdd
shantinghou Dec 5, 2024
275cd34
lint error fix
shantinghou Dec 5, 2024
0d57538
Merge pull request #17 from ilong4rennes/features/refactorMemberBoxGr…
shantinghou Dec 5, 2024
9b4ece6
fix membership group lint
shantinghou Dec 5, 2024
9836d0a
Merge pull request #20 from ilong4rennes/membership-group-fix
shantinghou Dec 5, 2024
ea94660
fix get error
ilong4rennes Dec 5, 2024
4e7aa3e
Merge pull request #21 from ilong4rennes/fix-memberboxgroup
ilong4rennes Dec 5, 2024
760ac77
changed class to function in servicetokenlist
ilong4rennes Dec 5, 2024
4d19245
changed productEdit
ilong4rennes Dec 5, 2024
806563c
changed productadd
ilong4rennes Dec 5, 2024
fdedf23
changed sales
ilong4rennes Dec 6, 2024
0c716bb
refactored messages
ilong4rennes Dec 6, 2024
ce56943
refactored membership
ilong4rennes Dec 6, 2024
6ed27d1
update accounting product
ilong4rennes Dec 6, 2024
e5ad72f
Merge pull request #29 from ilong4rennes/refactor-membership
ilong4rennes Dec 6, 2024
fd7ef5f
Merge pull request #28 from ilong4rennes/refactor-messages
ilong4rennes Dec 6, 2024
d2455a3
Merge pull request #27 from ilong4rennes/refactor-sales
ilong4rennes Dec 6, 2024
a06154c
Merge pull request #25 from ilong4rennes/refactor-file47
ilong4rennes Dec 6, 2024
6e80562
Merge pull request #24 from ilong4rennes/refactor-file48
ilong4rennes Dec 6, 2024
b1af1d7
Merge pull request #23 from ilong4rennes/refactor-file49
ilong4rennes Dec 6, 2024
4604cd1
Merge pull request #30 from ilong4rennes/refactor/component-files
ilong4rennes Dec 6, 2024
112ec5c
fix span error
ilong4rennes Dec 8, 2024
eaccc32
Merge pull request #31 from ilong4rennes/refactor/fix-membership
ilong4rennes Dec 8, 2024
a5b9cfb
fix gift card network error
shantinghou Dec 8, 2024
ce5f2b0
fix order show
shantinghou Dec 8, 2024
6b3976a
Merge pull request #32 from ilong4rennes/fix-gift-card-show
shantinghou Dec 8, 2024
7df1644
fix messages
ilong4rennes Dec 9, 2024
850746e
fix span show
ilong4rennes Dec 9, 2024
98fca29
Merge pull request #34 from ilong4rennes/refactor/span-info
ilong4rennes Dec 9, 2024
d69424c
Merge pull request #33 from ilong4rennes/refactor/email-log
ilong4rennes Dec 9, 2024
c58b7a8
refactored the 'preventDefault' error, and removed comments
ilong4rennes Dec 13, 2024
ef57643
Remove useCallback
ilong4rennes Dec 13, 2024
577b374
Remove useCallback
ilong4rennes Dec 13, 2024
f82d9d8
Merge pull request #35 from ilong4rennes/refactor/fix-prevent-default
ilong4rennes Dec 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 57 additions & 61 deletions admin/src/Components/CategoryPeriodsInput.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,65 @@
import React from "react";
import React, { useEffect, useState } from "react";
import DatePeriod from "../Models/DatePeriod";
import { utcToday } from "../utils";
import DatePeriodInput from "./DatePeriodInput";
import DatePeriod from "../Models/DatePeriod";

export default class CategoryPeriodsInput extends React.Component {
constructor(props) {
super(props);
this.state = { periods: [] };
}
const CategoryPeriodsInput = ({ categoryPeriods, showHistoric }) => {
const [periods, setPeriods] = useState([]);

componentDidMount() {
const { categoryPeriods } = this.props;
categoryPeriods.subscribe(() => {
this.setState({ periods: categoryPeriods.periods });
useEffect(() => {
const unsubscribe = categoryPeriods.subscribe(() => {
setPeriods(categoryPeriods.periods);
});
}

render() {
const { category } = this.props.categoryPeriods;
const { showHistoric } = this.props;
const { periods } = this.state;

const today = utcToday();
const categoryTitle = {
labaccess: "Labaccess",
special_labaccess: "Access till lokalen (styrelsen etc)",
membership: "Medlemsskap",
return () => {
unsubscribe();
};
}, [categoryPeriods]);

const { category } = categoryPeriods;

const today = utcToday();
const categoryTitle = {
labaccess: "Labaccess",
special_labaccess: "Access till lokalen (styrelsen etc)",
membership: "Medlemsskap",
};

return (
<div>
<h4 className="uk-margin-top">{categoryTitle[category]}</h4>
{periods.map((p) => {
if (!showHistoric && p.end < today) {
return null;
}
return (
<div key={p.id}>
<DatePeriodInput period={p} />
&nbsp;
<a
onClick={() => categoryPeriods.remove(p)}
className="removebutton"
>
<i className="uk-icon-trash" />
</a>
</div>
);
})}
<button
type="button"
style={{ marginTop: "2px" }}
className="uk-button uk-button-small uk-button-success"
onClick={() => {
const period = new DatePeriod();
period.start = utcToday();
period.end = utcToday();
categoryPeriods.add(period);
}}
>
Lägg till period
</button>
</div>
);
};

return (
<div>
<h4 className="uk-margin-top">{categoryTitle[category]}</h4>
{periods.map((p) => {
if (!showHistoric && p.end < today) {
return null;
}
return (
<div key={p.id}>
<DatePeriodInput key={p.id} period={p} />
&nbsp;
<a
onClick={() =>
this.props.categoryPeriods.remove(p)
}
className="removebutton"
>
<i className="uk-icon-trash" />
</a>
</div>
);
})}
<button
type="button"
style={{ marginTop: "2px" }}
className="uk-button uk-button-small uk-button-success"
onClick={() => {
const period = new DatePeriod();
period.start = utcToday();
period.end = utcToday();
this.props.categoryPeriods.add(period);
}}
>
Lägg till period
</button>
</div>
);
}
}
export default CategoryPeriodsInput;
124 changes: 62 additions & 62 deletions admin/src/Components/CheckboxInput.jsx
Original file line number Diff line number Diff line change
@@ -1,76 +1,76 @@
import React from "react";
import classNames from "classnames/bind";
import React, { useEffect, useState } from "react";

export default class CheckboxInput extends React.Component {
constructor(props) {
super(props);
this.state = {
value: false,
selected: false,
isDirty: false,
};
}
const CheckboxInput = ({
model,
name,
title,
icon,
disabled,
formrow,
tabIndex,
label,
}) => {
const [value, setValue] = useState(false);
const [isDirty, setIsDirty] = useState(false);

componentDidMount() {
const { model, name } = this.props;
this.unsubscribe = model.subscribe(() =>
this.setState({
value: model[name] === "" ? false : model[name],
isDirty: model.isDirty(name),
}),
);
}
useEffect(() => {
const unsubscribe = model.subscribe(() => {
setValue(model[name] === "" ? false : model[name]);
setIsDirty(model.isDirty(name));
});

componentWillUnmount() {
this.unsubscribe();
}
return () => {
unsubscribe();
};
}, [model, name]);
Comment on lines +17 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Optimize model subscription updates

The subscription callback should use functional updates to ensure state updates are based on the latest values.

 useEffect(() => {
     const unsubscribe = model.subscribe(() => {
-        setValue(model[name] === "" ? false : model[name]);
-        setIsDirty(model.isDirty(name));
+        setValue(Boolean(model[name]));
+        setIsDirty(prev => {
+            const newDirty = model.isDirty(name);
+            return prev !== newDirty ? newDirty : prev;
+        });
     });

     return () => {
         unsubscribe();
     };
 }, [model, name]);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
useEffect(() => {
const unsubscribe = model.subscribe(() => {
setValue(model[name] === "" ? false : model[name]);
setIsDirty(model.isDirty(name));
});
componentWillUnmount() {
this.unsubscribe();
}
return () => {
unsubscribe();
};
}, [model, name]);
useEffect(() => {
const unsubscribe = model.subscribe(() => {
setValue(Boolean(model[name]));
setIsDirty(prev => {
const newDirty = model.isDirty(name);
return prev !== newDirty ? newDirty : prev;
});
});
return () => {
unsubscribe();
};
}, [model, name]);


render() {
const { value, isDirty } = this.state;
const { model, name, title, icon, disabled, formrow, tabIndex, label } =
this.props;
const classes = classNames(name, {
"uk-form-row": formrow,
changed: isDirty,
});

const classes = classNames(name, {
"uk-form-row": formrow,
changed: isDirty,
});
const handleChange = () => {
model[name] = !model[name];
};

const input = (
<input
id={name}
name={name}
className=""
checked={value}
disabled={disabled}
type="checkbox"
tabIndex={tabIndex}
onChange={() => (model[name] = !model[name])}
/>
);
const input = (
<input
id={name}
name={name}
className=""
checked={value}
disabled={disabled}
type="checkbox"
tabIndex={tabIndex}
onChange={handleChange}
/>
);

return (
<div className={classes}>
{label ? (
<label className="uk-form-label" htmlFor={name}>
{title}
</label>
) : null}
<div className="uk-form-controls">
{icon ? (
<div className="uk-form-icon">
<i className={"uk-icon-" + icon} />
{input}
</div>
) : (
input
)}
</div>
return (
<div className={classes}>
{label && (
<label className="uk-form-label" htmlFor={name}>
{title}
</label>
)}
<div className="uk-form-controls">
{icon ? (
<div className="uk-form-icon">
<i className={"uk-icon-" + icon} />
{input}
</div>
) : (
input
)}
</div>
);
}
}
</div>
);
};

CheckboxInput.defaultProps = {
formrow: true,
label: true,
};

export default CheckboxInput;
Loading