Skip to content

Commit c00dbc6

Browse files
fix: move state, cleanup
1 parent c2eedb8 commit c00dbc6

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

.eslintrc

+1-7
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@
44
"es2021": true,
55
"node": true
66
},
7-
"extends": [
8-
"eslint:recommended",
9-
"prettier",
10-
"plugin:solid/recommended",
11-
"plugin:markdown/recommended",
12-
"plugin:json/recommended"
13-
],
7+
"extends": ["eslint:recommended", "prettier", "plugin:solid/recommended"],
148
"parserOptions": {
159
"ecmaFeatures": {
1610
"jsx": true

src/Dialog.jsx

+1-8
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,9 @@ import {
66
TransitionChild,
77
DialogOverlay
88
} from 'solid-headless'
9-
import { createStore } from 'solid-js/store'
109
import ImageDrop from './ImageDrop.jsx'
1110

1211
export default function ImageUploadDialog(props) {
13-
const [state, setState] = createStore({
14-
error: null,
15-
loading: false,
16-
file: {}
17-
})
18-
1912
return (
2013
<Transition appear show={props.isOpen()}>
2114
<Dialog
@@ -54,7 +47,7 @@ export default function ImageUploadDialog(props) {
5447
>
5548
{props.title}
5649
</DialogTitle>
57-
<ImageDrop saveImage={props.saveImage} setState={setState} />
50+
<ImageDrop saveImage={props.saveImage} />
5851
</DialogPanel>
5952
</TransitionChild>
6053
</div>

src/ImageDrop.jsx

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import 'cropperjs/dist/cropper.css'
2+
23
import { createSignal, Show } from 'solid-js'
4+
import { createStore } from 'solid-js/store'
35
import Cropper from 'cropperjs'
46
import { Icon } from 'solid-heroicons'
57
import { cloudArrowUp, photo } from 'solid-heroicons/solid'
8+
69
export default function ImageDrop(props) {
710
let cropImage
8-
const [dropZoneActive, setDropZoneActive] = createSignal(false),
11+
const [state, setState] = createStore({
12+
error: null,
13+
loading: false,
14+
file: {},
15+
croppedImage: null
16+
}),
17+
[dropZoneActive, setDropZoneActive] = createSignal(false),
918
[uploading, setUploading] = createSignal(false),
1019
[preview, setPreview] = createSignal(null),
1120
[cropper, setCropper] = createSignal(null),
@@ -15,8 +24,8 @@ export default function ImageDrop(props) {
1524
uploadFile = async (file) => {
1625
if (!file) return
1726
setUploading(true)
18-
props.setState('loading', true)
19-
props.setState('file', file)
27+
setState('loading', true)
28+
setState('file', file)
2029
try {
2130
const reader = new FileReader()
2231
reader.onload = (e) => {
@@ -33,14 +42,13 @@ export default function ImageDrop(props) {
3342
} catch (e) {
3443
console.error('upload failed', e)
3544
const message = e instanceof Error ? e.message : String(e)
36-
props.setState('error', message)
45+
setState('error', message)
3746
}
38-
props.setState('loading', false)
47+
setState('loading', false)
3948
setUploading(false)
4049
},
4150
handleFileDrop = async (e) => {
4251
e.preventDefault()
43-
console.log(e)
4452
setDropZoneActive(false)
4553
uploadFile(e.dataTransfer.files[0])
4654
},
@@ -64,9 +72,13 @@ export default function ImageDrop(props) {
6472
<button
6573
type="button"
6674
class="inline-flex items-center gap-x-1.5 rounded-md bg-indigo-600 py-2 px-3 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 mt-2"
67-
onClick={() =>
68-
props.saveImage(cropper().getCroppedCanvas().toDataURL())
69-
}
75+
onClick={() => {
76+
setState(
77+
'croppedImage',
78+
cropper().getCroppedCanvas().toDataURL(state.file.type)
79+
)
80+
props.saveImage(state)
81+
}}
7082
>
7183
<Icon
7284
path={cloudArrowUp}

0 commit comments

Comments
 (0)