11import React from 'react'
22import styled from 'styled-components'
3- import { Drawing } from '../../types'
3+ import { Drawing , TagDrawingSets } from '../../types'
44import Modal from 'react-modal'
5+ import { addTagToDrawing , loadTagDrawingSets , drawingHasTag , removeTagFromDrawing } from '../../lib/hashtags'
56
67export const AddTagModal : React . FC < { drawing : Drawing ; isOpen : boolean ; closeModal : ( ) => void } > = ( {
78 drawing,
89 isOpen,
910 closeModal,
1011} ) => {
11- const onNewTagSubmit : ( e : React . FormEvent ) => void = ( e ) => {
12+ const [ newTag , setNewTag ] = React . useState ( '' )
13+ const [ tagDrawingSets , setTagDrawingSets ] = React . useState < TagDrawingSets > ( loadTagDrawingSets ( ) )
14+
15+ function onNewTagSubmit ( e : React . FormEvent < HTMLFormElement > ) {
16+ addTagToDrawing ( newTag , drawing )
17+ setNewTag ( '' )
18+ setTagDrawingSets ( loadTagDrawingSets ( ) )
1219 e . preventDefault ( )
1320 }
21+ const tags = Object . keys ( tagDrawingSets )
1422
1523 return (
1624 < Modal
1725 isOpen = { isOpen }
1826 onRequestClose = { closeModal }
1927 >
20- < CloseButton onClick = { closeModal } > close</ CloseButton >
21- < Title > Add Hashtags</ Title >
22- < pre > { JSON . stringify ( drawing , null , 2 ) } </ pre >
23- < form onSubmit = { onNewTagSubmit } >
24- New: < input /> < input type = 'submit' />
25- </ form >
28+ < div onKeyDown = { e => e . stopPropagation ( ) } >
29+ < CloseButton onClick = { closeModal } > close</ CloseButton >
30+ < Title > Add Hashtags</ Title >
31+ < pre style = { { fontSize : '9px' } } > { JSON . stringify ( drawing , null , 2 ) } </ pre >
32+ < form onSubmit = { onNewTagSubmit } >
33+ New: < input value = { newTag } onChange = { e => setNewTag ( e . currentTarget . value ) } /> < input type = 'submit' />
34+ </ form >
35+ < br />
36+ < ul >
37+ { tags . sort ( ) . map ( tag => (
38+ < Tag key = { tag } >
39+ < label >
40+ < input
41+ type = 'checkbox'
42+ checked = { drawingHasTag ( drawing , tag ) }
43+ onChange = { e => {
44+ e . currentTarget . checked ? addTagToDrawing ( tag , drawing ) : removeTagFromDrawing ( tag , drawing )
45+ setTagDrawingSets ( loadTagDrawingSets ( ) )
46+ } }
47+ /> { tag }
48+ </ label >
49+ </ Tag >
50+ ) ) }
51+ </ ul >
52+ </ div >
2653 </ Modal >
2754 )
2855}
@@ -34,6 +61,13 @@ const CloseButton = styled.button.attrs({ classNames: 'Explorer__AddTagModal__Cl
3461 top: 24px;
3562`
3663
37- const Title = styled . h1 . attrs ( { classNames : 'Explorer__AddTagModal__CloseButton ' } ) `
64+ const Title = styled . h1 . attrs ( { classNames : 'Explorer__AddTagModal__Title ' } ) `
3865 text-align: center;
3966`
67+
68+ const Tag = styled . li . attrs ( { classNames : 'Explorer__AddTagModal__Tag' } ) `
69+ border-right: 1px solid #DDD;
70+ display: inline-block;
71+ margin: 8px 0;
72+ padding: 0 8px;
73+ `
0 commit comments