1+ //REACT
2+ import React , { useContext , useEffect , useState } from 'react' ;
3+ //MUI COMPONENTS
4+ import {
5+ Table ,
6+ TableHead ,
7+ TableBody ,
8+ TableRow ,
9+ TableCell ,
10+ Typography ,
11+ IconButton ,
12+ TableContainer , Paper ,
13+ } from '@material-ui/core' ;
14+ //MUI ICONS
15+ import { Edit as EditIcon , Done as DoneIcon , Close as CloseIcon } from '@material-ui/icons' ;
16+ //CONTEXTS
17+ import { TagContext } from '../../../contexts/TagContext' ;
18+ //CUSTOM COMPONENTS
19+ import AddTag from './AddTag' ;
20+ import DeleteButton from './DeleteButton' ;
21+ import ExtractTextFromMessage from '../../functions/ExtractTextFromMessage' ;
22+
23+
24+ const TagTable = ( ) => {
25+ const context = useContext ( TagContext ) ;
26+ const { tags, message} = context ;
27+
28+ const initialState = { tagEditId : null } ;
29+
30+ const [ state , setState ] = useState ( initialState ) ;
31+
32+ const setEdit = ( e ) => {
33+ setState ( { tagEditId : e . target . name ? e . target . name : null } ) ;
34+ } ;
35+
36+ useEffect ( ( ) => {
37+ if ( ! context . tags ) context . read ( ) ;
38+ } , [ context ] ) ;
39+
40+ console . log ( message ) ;
41+
42+ return (
43+ < TableContainer component = { Paper } >
44+ < Table size = "small" >
45+ < TableHead >
46+ < TableRow >
47+ < TableCell colSpan = { 2 } >
48+ < AddTag />
49+ </ TableCell >
50+ </ TableRow >
51+ < TableRow >
52+ < TableCell > { message . level === 'error' ? message . text . join ( '\n' ) : 'Tag Name' } </ TableCell >
53+ < TableCell align = "right" > Actions</ TableCell >
54+ </ TableRow >
55+ </ TableHead >
56+ < TableBody >
57+ { tags ? tags . map ( ( tag , index ) => (
58+ < TableRow key = { tag . id } >
59+ < TableCell > < Typography > { tag . name } </ Typography > </ TableCell >
60+ < TableCell align = "right" >
61+ { state . tagEditId !== tag . id ?
62+ < >
63+ < IconButton color = "primary" name = { tag . id } onClick = { setEdit } >
64+ < EditIcon />
65+ </ IconButton >
66+ < DeleteButton entity = { tag } />
67+ </ >
68+ :
69+ < >
70+ < IconButton color = "primary" >
71+ < DoneIcon />
72+ </ IconButton >
73+ < IconButton color = "secondary" onClick = { setEdit } >
74+ < CloseIcon />
75+ </ IconButton >
76+ </ >
77+ }
78+ </ TableCell >
79+ </ TableRow >
80+ ) ) : null }
81+
82+ { context . isLoading &&
83+ < TableRow > < TableCell > < Typography > Loading...</ Typography > </ TableCell > </ TableRow > }
84+ </ TableBody >
85+ </ Table >
86+ </ TableContainer >
87+ ) ;
88+ } ;
89+
90+ export default TagTable ;
0 commit comments