Skip to content

Commit a5aa257

Browse files
committed
Card class and refolder
1 parent 47fc2ad commit a5aa257

File tree

9 files changed

+43
-34
lines changed

9 files changed

+43
-34
lines changed

src/App.js

+9-12
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import './App.css';
44
import CardList from './Cards/CardList'
55
import AddCard from './Cards/AddCard'
66
import Context from './context'
7-
import Loader from './Content/Loader'
7+
import Loader from './Shared/Loader'
88
import ModalCardEdit from './Cards/ModalCardEdit'
99
import ModalLogin from './Login/ModalLogin'
1010
import DataService from './Services/DataService'
11-
import checkCardsArr from './Shared/Card'
11+
import Card, { checkCardsArr } from './Shared/Card'
1212

1313
const { loadData, postData, updDataServLogin } = DataService()
1414

@@ -162,14 +162,7 @@ function App() {
162162

163163
function addCard(cardData = {}) {
164164
setCards(
165-
cardsArr.concat([
166-
{
167-
id: Number(++cardCount),
168-
name: String(cardData.name),
169-
completed: Boolean(cardData.sel),
170-
text: String(cardData.text),
171-
}
172-
])
165+
cardsArr.concat([new Card({ id: ++cardCount, name: cardData.name, completed: cardData.sel, text: cardData.text })])
173166
)
174167
}
175168

@@ -179,9 +172,13 @@ function App() {
179172
}
180173

181174
function editCardContent(index, name, text) {
175+
182176
if (cardsArr[index]) {
183-
cardsArr[index].name = name
184-
cardsArr[index].text = text
177+
let card
178+
card = new Card(cardsArr[index])
179+
card.name = name
180+
card.text = text
181+
cardsArr[index]=card
185182
}
186183
setCards([...cardsArr])
187184
}

src/Cards/CardItem.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useContext } from 'react'
22
import PropTypes from 'prop-types'
33
import Context from '../context'
4+
import Card from '../Shared/Card'
45

56
function createHTML(text) {
67
let el = document.createElement("p")
@@ -11,19 +12,20 @@ function createHTML(text) {
1112
function CardItem(props) {
1213
const { removeCard, changeCardState, setEditCard } = useContext(Context)
1314
const { card, index } = props
15+
const cardItem = card && new Card(card)
1416
const lineClip = 12
15-
const [color, btcolor] = card && card.completed ? ["green", "success"] : ["red", "danger"]
17+
const [color, btcolor] = cardItem && cardItem.completed ? ["green", "success"] : ["red", "danger"]
1618
return (
1719

1820
<div className="p-1" >
1921
<div className="card" style={{ color: "white", backgroundColor: color }} >
2022

2123
<div className="card-body" onClick={() => setEditCard(index)} >
22-
<h5 className="card-title">{card.name}</h5>
24+
<h5 className="card-title">{cardItem.name}</h5>
2325
<p
2426
className="card-text"
2527
style={{ overflow: "hidden", display: "-webkit-box", WebkitLineClamp: String(lineClip), WebkitBoxOrient: "vertical" }}
26-
dangerouslySetInnerHTML={createHTML(card.text)}
28+
dangerouslySetInnerHTML={createHTML(cardItem.text)}
2729
/>
2830
</div>
2931

src/Cards/ModalCardEdit.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import React from 'react'
22
import PropTypes from 'prop-types'
33
import Context from '../context'
44
import TextareaAutosize from 'react-textarea-autosize'
5-
import Modal, { ModalProps } from "../Modal/Modal"
5+
import Modal, { ModalProps } from "../Shared/Modal/Modal"
66
import debounce from '../Shared/debounce'
7+
import Card from '../Shared/Card'
78

89
function calcMaxRows() {
910
const small = 576
@@ -21,9 +22,10 @@ function calcMaxRows() {
2122
function ModalCardEdit(props) {
2223
const { removeCard, changeCardState, unsetEditCard, editCardContent } = React.useContext(Context)
2324
const { card, index } = props
24-
2525
React.useEffect(() => { if (card !== null) open() }, [card])
2626

27+
const cardEdit = card && new Card(card)
28+
2729
const [showForm, setShowForm] = React.useState(false)
2830

2931
const modalProps = new ModalProps()
@@ -48,8 +50,8 @@ function ModalCardEdit(props) {
4850
}
4951

5052
function onInputCange(e) {
51-
let name = card.name
52-
let text = card.text
53+
let name = cardEdit.name
54+
let text = cardEdit.text
5355
if (e.target.id === "modal-edit-name") name = e.target.value
5456
if (e.target.id === "modal-edit-text") text = e.target.value
5557
save(name, text)
@@ -69,14 +71,13 @@ function ModalCardEdit(props) {
6971
}
7072

7173
// eslint-disable-next-line no-unused-vars
72-
const [color, btcolor] = card && card.completed ? ["green", "success"] : ["red", "danger"]
74+
const [color, btcolor] = cardEdit && cardEdit.completed ? ["green", "success"] : ["red", "danger"]
7375

7476
return (
7577
<Modal {...modalProps.bind()}>
7678
<div className="container p-2">
77-
7879
<div>
79-
{card ? (
80+
{cardEdit ? (
8081
<React.Fragment>
8182
<TextareaAutosize
8283
className="form-control form-control-lg p-0 mb-2 bg-light text-dark"
@@ -85,14 +86,14 @@ function ModalCardEdit(props) {
8586
minRows={1}
8687
maxRows={3}
8788
maxLength="100"
88-
value={card.name}
89+
value={cardEdit.name}
8990
onChange={debounce(onInputCange, 700)}
9091
/>
9192

9293
<p style={{ fontWeight: "500" }} className="mb-2 text-dark">
9394
Completed:
9495
<span className={`m-1 d-inline-block text-center badge badge-${btcolor}`} style={{ width: "3em" }}>
95-
{String(card.completed)}
96+
{String(cardEdit.completed)}
9697
</span>
9798
</p>
9899

@@ -102,7 +103,7 @@ function ModalCardEdit(props) {
102103
style={{ border: "none", outline: "none", boxShadow: "none", resize: "none" }}
103104
minRows={3}
104105
maxRows={calcMaxRows()}
105-
value={card.text}
106+
value={cardEdit.text}
106107
onChange={debounce(onInputCange, 1000)}
107108
/>
108109
</React.Fragment>
@@ -115,18 +116,18 @@ function ModalCardEdit(props) {
115116
<div>
116117
<button
117118
className="btn btn-light mx-1"
118-
disabled={!card}
119+
disabled={!cardEdit}
119120
onClick={tryStateChange}
120121
>&#10003;</button>
121122
<button
122123
className="btn btn-light"
123-
disabled={!card}
124+
disabled={!cardEdit}
124125
onClick={tryRemove}
125126
>&#10007;</button>
126127
</div>
127128

128129
<div className="mx-auto">
129-
<span style={{ color: "lightgray", fontWeight: "400" }}>Id: {card && card.id}</span>
130+
<span style={{ color: "lightgray", fontWeight: "400" }}>Id: {cardEdit && cardEdit.id}</span>
130131
</div>
131132

132133
<div>
@@ -136,7 +137,6 @@ function ModalCardEdit(props) {
136137
>Close</button>
137138
</div>
138139
</div>
139-
140140
</div>
141141
</Modal>
142142
)

src/Login/ModalLogin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react"
2-
import Modal, { ModalProps } from "../Modal/Modal"
2+
import Modal, { ModalProps } from "../Shared/Modal/Modal"
33
import PropTypes from 'prop-types'
44
import LoginService from '../Services/LoginService'
55
const { logIn, logOut } = LoginService()

src/Services/DataService.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import $ from "jquery"
2-
import checkCardsArr from '../Shared/Card'
2+
import { checkCardsArr } from '../Shared/Card'
33

44
export default function DataService() {
55
////////////////////////////////////////////////////////////

src/Shared/Card.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function checkCardsArr(cardsArr) {
1+
export function checkCardsArr(cardsArr) {
22
if (!Array.isArray(cardsArr)) return false
33
else if (cardsArr.length === 0) return true
44
else {
@@ -15,8 +15,18 @@ const checkCard = (card) => {
1515
(typeof card.id === "number" || typeof card.id === "string") && !isNaN(card.id) &&
1616
typeof card.name === "string" &&
1717
typeof card.completed === "boolean" &&
18-
typeof card.text === "string"
18+
typeof card.text === "string" &&
19+
card instanceof Card
1920
)
2021
}
2122

22-
export default checkCardsArr
23+
export class Card {
24+
constructor({ id, name, completed, text }) {
25+
this.id = Number(id)
26+
this.name = String(name)
27+
this.completed = Boolean(completed)
28+
this.text = String(text)
29+
}
30+
}
31+
32+
export default Card
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)