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

Sidebar useEffect conversion #541

Merged
Merged
Changes from all commits
Commits
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
39 changes: 16 additions & 23 deletions frontend/src/components/sidebar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import React, { useContext, useState } from 'react';
import PropTypes from 'prop-types';
import React, { useCallback, useContext, useEffect, useState } from 'react';

import { Link } from 'react-router-dom';
import { IbutsuContext } from '../services/context';
Expand All @@ -12,44 +11,42 @@ import { PageSidebar,
import { HttpClient } from '../services/http';
import { Settings } from '../settings';


const IbutsuSidebar = (props) => {
function IbutsuSidebar() {
const context = useContext(IbutsuContext);
const [views, setViews] = useState();
props.eventEmitter.on('projectChange', (project) => {
setProjectViews(project);
})
// TODO: useEffect for view reset on project change
const { primaryType, primaryObject } = context;

const [views, setViews] = useState();

function setProjectViews(project) {
const { primaryObject } = context;
const targetProject = project ?? primaryObject;
if (!targetProject) {return;}
const setProjectViews = useCallback(() => {
if (!primaryObject) {return;}

let params = {'filter': ['type=view', 'navigable=true']};

// read selected project from location
params['filter'].push('project_id=' + targetProject.id);
params['filter'].push('project_id=' + primaryObject.id);

HttpClient.get([Settings.serverUrl, 'widget-config'], params)
.then(response => HttpClient.handleResponse(response))
.then(data => {
data.widgets.forEach(widget => {
if (targetProject) {
widget.params['project'] = targetProject.id;
if (primaryObject) {
widget.params['project'] = primaryObject.id;
}
else {
delete widget.params['project'];
}
});
setViews(data.widgets)})
.catch(error => console.log(error));
}
}, [primaryObject]);

useEffect(() => {
// When the project selection changes, fetch views to include unique sidebar items.
setProjectViews();
}, [primaryObject, setProjectViews])


const { primaryType, primaryObject } = context;
if ( primaryType == 'project' && primaryObject ) {
if (! views ) {setProjectViews(primaryObject);}
return (
<PageSidebar theme="dark" >
<PageSidebarBody>
Expand Down Expand Up @@ -82,8 +79,4 @@ const IbutsuSidebar = (props) => {
}
};

IbutsuSidebar.propTypes = {
eventEmitter: PropTypes.object,
};

export default IbutsuSidebar;
Loading