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

fix(ui): Migrate to functions #215

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
41 changes: 17 additions & 24 deletions src/pages/Policy.jsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
import React, { Component, createRef } from 'react';
import { useRef } from 'react';
import Col from 'react-bootstrap/esm/Col';
import Row from 'react-bootstrap/esm/Row';
import { PolicyEditor } from '../components/policy-editor/PolicyEditor';
import { CLIEquivalent, GoBackButton, parseQuery, PolicyTypeName } from '../utils/uiutil';

export class Policy extends Component {
constructor() {
super();
export function Policy({ location, history }) {
const source = parseQuery(location.search);
const { userName, host, path } = source;
const editorRef = useRef();

this.editorRef = createRef();
}

render() {
const source = parseQuery(this.props.location.search);
const { userName, host, path } = source;

return <>
<h4>
<GoBackButton onClick={this.props.history.goBack} />
&nbsp;&nbsp;{PolicyTypeName(source)}</h4>
<PolicyEditor ref={this.editorRef} userName={userName} host={host} path={path} close={this.props.history.goBack} />
<Row><Col>&nbsp;</Col></Row>
<Row>
<Col xs={12}>
<CLIEquivalent command={`policy set "${userName}@${host}:${path}"`} />
</Col>
</Row>
</>;
}
return <>
<h4>
<GoBackButton onClick={history.goBack} />
&nbsp;&nbsp;{PolicyTypeName(source)}</h4>
<PolicyEditor ref={editorRef} userName={userName} host={host} path={path} close={history.goBack} />
<Row><Col>&nbsp;</Col></Row>
<Row>
<Col xs={12}>
<CLIEquivalent command={`policy set "${userName}@${host}:${path}"`} />
</Col>
</Row>
</>;
}
69 changes: 33 additions & 36 deletions src/pages/Preferences.jsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
import { Component } from 'react';
import { useContext } from 'react';
import { UIPreferencesContext } from '../contexts/UIPreferencesContext';

/**
* Class that exports preferences
*/
export class Preferences extends Component {
render() {
const { pageSize, theme, bytesStringBase2, setByteStringBase, setTheme } = this.context;
return <>
<form>
<div className='form-group'>
<label className='label-description' htmlFor='themeSelector' id='themeLabel'>Theme</label>
<select className="form-select form-select-sm" title='Select theme' id='themeSelector' value={theme} onChange={e => setTheme(e.target.value)}>
<option value="light">light</option>
<option value="dark">dark</option>
<option value="pastel">pastel</option>
<option value="ocean">ocean</option>
</select>
<small hmtlfor='themeLabel' id='themeHelp' className='form-text text-muted'>The current active theme</small>
</div>
<br />
<div className='form-group'>
<label className='label-description' htmlFor="bytesBaseInput">Byte representation</label>
<select className="form-select form-select-sm" title='Select byte representation' id='bytesBaseInput' value={bytesStringBase2} onChange={e => setByteStringBase(e.target.value)}>
<option value="true">Base-2 (KiB, MiB, GiB, TiB)</option>
<option value="false">Base-10 (KB, MB, GB, TB)</option>
</select>
<small hmtlfor='bytesBaseInput' id='bytesHelp' className='form-text text-muted'>Specifies the representation of bytes</small>
</div>
<br />
<div className='form-group'>
<label className='label-description'>Page size</label>
<input className='form-control form-control-sm' id='pageSizeInput'
type='text' placeholder='Page size' value={pageSize} disabled={true} />
<small hmtlfor="pageSizeInput" id='pageSizeHelp' className='form-text text-muted'>Specifies the pagination size in tables</small>
</div>
</form>
</>
}
export function Preferences() {
const { pageSize, theme, bytesStringBase2, setByteStringBase, setTheme } = useContext(UIPreferencesContext);
return <>
<form>
<div className='form-group'>
<label className='label-description' htmlFor='themeSelector' id='themeLabel'>Theme</label>
<select className="form-select form-select-sm" title='Select theme' id='themeSelector' value={theme} onChange={e => setTheme(e.target.value)}>
<option value="light">light</option>
<option value="dark">dark</option>
<option value="pastel">pastel</option>
<option value="ocean">ocean</option>
</select>
<small hmtlfor='themeLabel' id='themeHelp' className='form-text text-muted'>The current active theme</small>
</div>
<br />
<div className='form-group'>
<label className='label-description' htmlFor="bytesBaseInput">Byte representation</label>
<select className="form-select form-select-sm" title='Select byte representation' id='bytesBaseInput' value={bytesStringBase2} onChange={e => setByteStringBase(e.target.value)}>
<option value="true">Base-2 (KiB, MiB, GiB, TiB)</option>
<option value="false">Base-10 (KB, MB, GB, TB)</option>
</select>
<small hmtlfor='bytesBaseInput' id='bytesHelp' className='form-text text-muted'>Specifies the representation of bytes</small>
</div>
<br />
<div className='form-group'>
<label className='label-description'>Page size</label>
<input className='form-control form-control-sm' id='pageSizeInput'
type='text' placeholder='Page size' value={pageSize} disabled={true} />
<small hmtlfor="pageSizeInput" id='pageSizeHelp' className='form-text text-muted'>Specifies the pagination size in tables</small>
</div>
</form>
</>
}
Preferences.contextType = UIPreferencesContext
168 changes: 76 additions & 92 deletions src/pages/Tasks.jsx
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@

import { faInfoCircle } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { useState, useLayoutEffect, useCallback } from 'react';
import { Link } from 'react-router-dom';
import axios from 'axios';
import moment from 'moment';
import React, { Component } from 'react';
import Alert from 'react-bootstrap/Alert';
import Col from 'react-bootstrap/Col';
import Dropdown from 'react-bootstrap/Dropdown';
import Form from 'react-bootstrap/Form';
import Row from 'react-bootstrap/Row';
import { Link } from 'react-router-dom';
import { handleChange } from '../forms';
import KopiaTable from '../utils/KopiaTable';
import { redirect, taskStatusSymbol } from '../utils/uiutil';

export class Tasks extends Component {
constructor() {
super();
this.state = {
items: [],
isLoading: false,
error: null,
showKind: "All",
showStatus: "All",
uniqueKinds: [],
};

this.handleChange = handleChange.bind(this);
this.fetchTasks = this.fetchTasks.bind(this);
this.interval = window.setInterval(this.fetchTasks, 3000);
}
export function Tasks() {
const [isLoading, setIsLoading] = useState(false);
const [searchDescription, setDescription] = useState("")
const [response, setResponse] = useState({ items:[], kinds:[]});
const [kind, setKind] = useState("All");
const [status, setStatus] = useState("All");
const [error, setError] = useState();

componentDidMount() {
this.setState({
isLoading: true,
const fetchTasks = useCallback(() => {
axios.get('/api/v1/tasks').then(result => {
setIsLoading(false);
setResponse({items: result.data.tasks, kinds:getUniqueKinds(result.data.tasks)});
}).catch(error => {
redirect(error);
setError(error);
setIsLoading(false);
});
}, [])

useLayoutEffect(() => {
setIsLoading(true)
fetchTasks()
let interval = setInterval(fetchTasks, 5000)
return () => {
window.clearInterval(interval);
};
}, [fetchTasks]);

this.fetchTasks();
}

componentWillUnmount() {
window.clearInterval(this.interval);
}

getUniqueKinds(tasks) {
function getUniqueKinds(tasks) {
let o = {};

for (const tsk of tasks) {
Expand All @@ -58,112 +56,98 @@ export class Tasks extends Component {
return result;
}

fetchTasks() {
axios.get('/api/v1/tasks').then(result => {
this.setState({
items: result.data.tasks,
uniqueKinds: this.getUniqueKinds(result.data.tasks),
isLoading: false,
});
}).catch(error => {
redirect(error);
this.setState({
error,
isLoading: false
});
});
function handleDescription(desc) {
setDescription(desc.target.value)
}

taskMatches(t) {
if (this.state.showKind !== "All" && t.kind !== this.state.showKind) {
function taskMatches(t) {
if (kind !== "All" && t.kind !== kind) {
return false;
}

if (this.state.showStatus !== "All" && t.status.toLowerCase() !== this.state.showStatus.toLowerCase()) {
if (status !== "All" && t.status.toLowerCase() !== status.toLowerCase()) {
return false;
}

if (this.state.searchDescription && t.description.indexOf(this.state.searchDescription) < 0) {
if (searchDescription && t.description.indexOf(searchDescription) < 0) {
return false;
}

return true
}

filterItems(items) {
return items.filter(c => this.taskMatches(c))
function filterItems(items) {
return items.filter(c => taskMatches(c))
}

render() {
const { items, isLoading, error } = this.state;
if (error) {
return <p>{error.message}</p>;
}
if (isLoading) {
return <p>Loading ...</p>;
}
if (error) {
return <p>{error.message}</p>;
}
if (isLoading) {
return <p>Loading ...</p>;
}

const columns = [{
Header: 'Start Time',
width: 160,
accessor: x => <Link to={'/tasks/' + x.id} title={moment(x.startTime).toLocaleString()}>
{moment(x.startTime).fromNow()}
</Link>
}, {
Header: 'Status',
width: 240,
accessor: x => taskStatusSymbol(x),
}, {
Header: 'Kind',
width: "",
accessor: x => <p>{x.kind}</p>,
}, {
Header: 'Description',
width: "",
accessor: x => <p>{x.description}</p>,
}]

const filteredItems = this.filterItems(items)

return <>
const columns = [{
Header: 'Start Time',
width: 160,
accessor: x => <Link to={'/tasks/' + x.id} title={moment(x.startTime).toLocaleString()}>
{moment(x.startTime).fromNow()}
</Link>
}, {
Header: 'Status',
width: 240,
accessor: x => taskStatusSymbol(x),
}, {
Header: 'Kind',
width: "",
accessor: x => <p>{x.kind}</p>,
}, {
Header: 'Description',
width: "",
accessor: x => <p>{x.description}</p>,
}]

const filteredItems = filterItems(response.items)

return (
<>
<Form>
<div className="list-actions">
<Row>
<Col xs="auto">
<Dropdown>
<Dropdown.Toggle size="sm" variant="primary">Status: {this.state.showStatus}</Dropdown.Toggle>
<Dropdown.Toggle size="sm" variant="primary">Status: {status}</Dropdown.Toggle>
<Dropdown.Menu>
<Dropdown.Item onClick={() => this.setState({ showStatus: "All" })}>All</Dropdown.Item>
<Dropdown.Item onClick={() => setStatus("All")}>All</Dropdown.Item>
<Dropdown.Divider />
<Dropdown.Item onClick={() => this.setState({ showStatus: "Running" })}>Running</Dropdown.Item>
<Dropdown.Item onClick={() => this.setState({ showStatus: "Failed" })}>Failed</Dropdown.Item>
<Dropdown.Item onClick={() => setStatus("Running")}>Running</Dropdown.Item>
<Dropdown.Item onClick={() => setStatus("Failed")}>Failed</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
</Col>
<Col xs="auto">
<Dropdown>
<Dropdown.Toggle size="sm" variant="primary">Kind: {this.state.showKind}</Dropdown.Toggle>
<Dropdown.Toggle size="sm" variant="primary">Kind: {kind}</Dropdown.Toggle>
<Dropdown.Menu>
<Dropdown.Item onClick={() => this.setState({ showKind: "All" })}>All</Dropdown.Item>
<Dropdown.Item onClick={() => setKind("All")}>All</Dropdown.Item>
<Dropdown.Divider />
{this.state.uniqueKinds.map(k => <Dropdown.Item key={k} onClick={() => this.setState({ showKind: k })}>{k}</Dropdown.Item>)}
{response.kinds.map(kind => <Dropdown.Item key={kind} onClick={() => setKind(kind)}>{kind}</Dropdown.Item>)}
</Dropdown.Menu>
</Dropdown>
</Col>
<Col xs="4">
<Form.Control size="sm" type="text" name="searchDescription" placeholder="case-sensitive search description" value={this.state.searchDescription} onChange={this.handleChange} autoFocus={true} />
<Form.Control size="sm" type="text" name="searchDescription" placeholder="Search logs by description" value={searchDescription} onChange={handleDescription} autoFocus={true} />
</Col>
</Row>
</div>
<Row>
<Col>
{!items.length ?
{!response.items.length ?
<Alert variant="info">
<FontAwesomeIcon size="sm" icon={faInfoCircle} /> A list of tasks will appear here when you create snapshots, restore, run maintenance, etc.
</Alert> : <KopiaTable data={filteredItems} columns={columns} />}
</Col>
</Row>
</Form>
</>;
}
</>);
}
Loading