Skip to content

Commit

Permalink
Fixed issue with endpoint wrapper callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
ANeaves committed Nov 23, 2023
1 parent be8e678 commit 93b43b5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion example/src/EndpointExample.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function EndpointExamplePage(props) {
<Col xs={5}>
<TitleCard title="Button">
<EndpointButton endpoint={periodicEndpoint} event_type="click" fullpath="trigger" value={42}
pre_method={prePutMethod} pre_kwargs={["Test", 42]}>Trigger</EndpointButton>
pre_method={prePutMethod} pre_args={["Test", 42]}>Trigger</EndpointButton>
</TitleCard>
</Col>
<Col>
Expand Down
31 changes: 25 additions & 6 deletions src/services/withEndpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ function WithEndpoint(WrappedComponent)
const data = useRef(value);
const timer = useRef(null);

const pre_func = useRef(pre_method);
const post_func = useRef(post_method);
// const pre_func = useRef(pre_method);
// const post_func = useRef(post_method);

const pre_func_kwargs = useRef(pre_kwargs);
const post_func_kwargs = useRef(post_kwargs);
const pre_func_kwargs = useRef(pre_args);
const post_func_kwargs = useRef(post_args);

const [error, setError] = useState(null);
const [eventProp, setEventProp] = useState(null);
Expand Down Expand Up @@ -108,12 +108,31 @@ function WithEndpoint(WrappedComponent)

const sendRequest = useCallback((val) => {
clearInterval(timer.current);
pre_func.current && pre_func.current(...pre_func_kwargs.current);
if(pre_method)
{
if(pre_func_kwargs.current)
{
pre_method(...pre_func_kwargs.current);
}else{
pre_method();
}
}
// pre_method && pre_method(
// (pre_func_kwargs.current) ? ...pre_func_kwargs.current : null);
const sendVal = {[valueName]: val};
endpoint.put(sendVal, path)
.then((response) => {
endpoint.mergeData(response, path);
post_func.current && post_func.current(...post_func_kwargs.current);
if(post_method)
{
if(post_func_kwargs.current)
{
post_method(...post_func_kwargs.current);
}else{
post_method();
}
}
post_method && post_method(...post_func_kwargs.current);
})
.catch((err) => {
setError(err);
Expand Down

0 comments on commit 93b43b5

Please sign in to comment.