Skip to content

Commit 54ba19f

Browse files
authored
Merge pull request #162 from bavix/history
[v1.3.0] history LocalStorage
2 parents 4b733c4 + 92fa1dd commit 54ba19f

File tree

6 files changed

+38
-6
lines changed

6 files changed

+38
-6
lines changed

.github/workflows/compile-assets.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
ui:
99
permissions:
1010
contents: write
11-
uses: bavix/.github/.github/workflows/[email protected].0
11+
uses: bavix/.github/.github/workflows/[email protected].2
1212
secrets: inherit
1313
with:
1414
nodejs: 21.x

public/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app.jsx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react'
2-
import InputComponent from "./input.jsx"
2+
import InputComponent, {Item} from "./input.jsx"
33
import HistoryComponent from "./history.jsx"
44
import NavComponent from './nav.jsx'
55
import '@creativebulma/bulma-tooltip/dist/bulma-tooltip.css'
@@ -29,6 +29,36 @@ export default class AppComponent extends React.Component {
2929
super(props);
3030
}
3131

32+
/**
33+
* ComponentDidMount lifecycle hook.
34+
* It gets the items from localStorage and sets the state.
35+
*/
36+
componentDidMount() {
37+
const itemsFromLocalStorage = JSON.parse(localStorage.getItem('uuidItems')) || [];
38+
39+
let newItem
40+
const items = {}
41+
for (const item of itemsFromLocalStorage) {
42+
newItem = new Item(item.input, item.output, item.info)
43+
items[newItem.toString()] = newItem
44+
}
45+
46+
this.setState({ items: Object.values(items) });
47+
}
48+
49+
/**
50+
* ComponentDidUpdate lifecycle hook.
51+
* It saves the items to localStorage when the state changes.
52+
* @param {Object} prevProps - The previous props.
53+
* @param {Object} prevState - The previous state.
54+
*/
55+
componentDidUpdate(prevProps, prevState) {
56+
let newState = [...this.state.items].slice(0, 100)
57+
if (prevState.items !== newState) {
58+
localStorage.setItem('uuidItems', JSON.stringify(newState));
59+
}
60+
}
61+
3262
/**
3363
* Render method for the AppComponent.
3464
*

src/history.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ export default class HistoryComponent extends React.Component {
5151
{/* Field containing two tags */}
5252
<div className="field">
5353
{/* Output tag */}
54-
<a href="javascript:" onClick={this.copy} className="tag is-link is-light" data-tooltip={i.info}>{ i.output }</a>
54+
<div className="tags">
55+
<a href="javascript:" onClick={this.copy} className="tag is-link is-light" data-tooltip={i.info}>{ i.output }</a>
56+
</div>
5557
{/* Input tag */}
5658
<div className="tags">
5759
<a href="javascript:" onClick={this.copy} className="tag is-primary is-light" data-tooltip={i.info}>{ i.input }</a>

src/input.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function intTypeList() {
5858
* Represents an item with input, output, and additional information.
5959
* @class
6060
*/
61-
class Item {
61+
export class Item {
6262
/**
6363
* Creates a new Item.
6464
* @constructor

0 commit comments

Comments
 (0)