|
1 | | -import React, { Component } from 'react' |
| 1 | +import React, { useEffect, useState } from 'react' |
2 | 2 | import { ipcRenderer } from 'electron' |
3 | | -import autobind from 'autobind-decorator' |
4 | 3 | import update from 'immutability-helper' |
5 | 4 | import { Button } from '@material-ui/core' |
6 | 5 | import Loading from '../../../Loading' |
7 | 6 | import PhotoList from './PhotoList' |
8 | 7 |
|
9 | | -class GooglePhotos extends Component { |
10 | 8 |
|
11 | | - constructor(props, context) { |
12 | | - super(props, context) |
13 | | - this.state = { |
14 | | - initialized: false, |
15 | | - images: [], |
16 | | - nextPageToken: null, |
17 | | - fetching: false |
18 | | - } |
19 | | - } |
20 | | - |
21 | | - componentWillMount() { |
22 | | - ipcRenderer.on("receive-google-photos-images", this.handleReceiveImages) |
23 | | - ipcRenderer.on("start-fetch-google-photos-images", this.handleStartFetch) |
24 | | - ipcRenderer.on("receive-google-connected", this.handleReceiveConnected) |
25 | | - } |
26 | | - |
27 | | - componentWillUnmount() { |
28 | | - ipcRenderer.removeListener("receive-google-photos-images", this.handleReceiveImages) |
29 | | - ipcRenderer.removeListener("start-fetch-google-photos-images", this.handleStartFetch) |
30 | | - ipcRenderer.removeListener("receive-google-connected", this.handleReceiveConnected) |
31 | | - } |
| 9 | +export default function GooglePhotos({ connected, onConnect, onDisconnect, onSelectImage }) { |
| 10 | + const [initialized, setInitialized] = useState(false) |
| 11 | + const [images, setImages] = useState([]) |
| 12 | + const [fetching, setFetching] = useState(false) |
| 13 | + const [nextPageToken, setNextPageToken] = useState(null) |
32 | 14 |
|
33 | | - componentDidMount() { |
34 | | - this.handleRequestFetch() |
35 | | - } |
36 | | - |
37 | | - @autobind |
38 | | - handleReceiveImages(e, data) { |
39 | | - const { onConnect, onDisconnect } = this.props |
40 | | - const { images, fetching } = this.state |
| 15 | + function handleReceiveImages(e, data) { |
41 | 16 | if (data === null) { |
42 | 17 | onDisconnect() |
43 | 18 | return |
44 | 19 | } |
45 | 20 |
|
46 | | - this.setState({ |
47 | | - initialized: true, |
48 | | - images: update(images, { |
49 | | - $push: data.images |
50 | | - }), |
51 | | - nextPageToken: data.nextPageToken, |
52 | | - fetching: false |
53 | | - }) |
| 21 | + console.log("connected", data) |
| 22 | + |
| 23 | + setInitialized(true) |
| 24 | + setImages(update(images, { |
| 25 | + $push: data.images |
| 26 | + })) |
| 27 | + setNextPageToken(data.nextPageToken) |
| 28 | + setFetching(false) |
54 | 29 | onConnect(true) |
55 | 30 | } |
56 | 31 |
|
57 | | - @autobind |
58 | | - handleReceiveConnected(e, connected) { |
59 | | - const { onConnect, onDisconnect } = this.props |
| 32 | + function handleStartFetch(e) { |
| 33 | + setFetching(true) |
| 34 | + } |
| 35 | + |
| 36 | + function handleReceiveConnected(e, connected) { |
| 37 | + setInitialized(true) |
| 38 | + |
60 | 39 | if (connected) { |
61 | | - this.setState({ |
62 | | - initialized: true, |
63 | | - }) |
64 | 40 | onConnect() |
65 | 41 | } else { |
66 | | - this.setState({ |
67 | | - initialized: true, |
68 | | - images: [], |
69 | | - nextPageToken: null, |
70 | | - }) |
| 42 | + setImages([]) |
| 43 | + setNextPageToken(null) |
71 | 44 | onDisconnect() |
72 | 45 | } |
73 | 46 | } |
74 | 47 |
|
75 | | - @autobind |
76 | | - handleStartFetch(e) { |
77 | | - this.setState({ |
78 | | - fetching: true |
79 | | - }) |
80 | | - } |
81 | | - |
82 | | - @autobind |
83 | | - handleRequestFetch() { |
84 | | - const { nextPageToken } = this.state |
| 48 | + function handleRequestFetch() { |
85 | 49 | ipcRenderer.send('fetch-google-photos-images', nextPageToken) |
86 | 50 | } |
87 | 51 |
|
88 | | - @autobind |
89 | | - handleRequestAuth() { |
| 52 | + function handleRequestAuth() { |
90 | 53 | ipcRenderer.send("request-google-photos-auth") |
91 | 54 | } |
92 | 55 |
|
93 | | - |
94 | | - @autobind |
95 | | - handleImageSelect(image) { |
96 | | - const { onSelectImage } = this.props |
| 56 | + function handleImageSelect(image) { |
97 | 57 | if (confirm('이미지를 삽입하시겠습니까?')) { |
98 | 58 | onSelectImage(image.url, image.title) |
99 | 59 | } |
100 | 60 | } |
101 | 61 |
|
102 | | - render() { |
103 | | - const { connected } = this.props |
104 | | - const { initialized, images, fetching } = this.state |
105 | | - |
106 | | - if (!initialized) { |
107 | | - return ( |
108 | | - <div className="google-photos-wrap"> |
109 | | - <div className="google-photos-cover"> |
110 | | - <Loading position='absolute' /> |
111 | | - </div> |
112 | | - </div> |
113 | | - ) |
114 | | - } |
115 | | - |
116 | | - if (!connected) { |
117 | | - return ( |
118 | | - <div className="google-photos-wrap"> |
119 | | - <div className="google-photos-cover"> |
120 | | - <Button variant='contained' className='btn btn_tistory' onClick={this.handleRequestAuth}> |
121 | | - Google Photos 연결 |
122 | | - </Button> |
123 | | - </div> |
124 | | - </div> |
125 | | - ) |
126 | | - } |
| 62 | + useEffect(() => { |
| 63 | + handleRequestFetch() |
| 64 | + ipcRenderer.on("receive-google-photos-images", handleReceiveImages) |
| 65 | + ipcRenderer.on("start-fetch-google-photos-images", handleStartFetch) |
| 66 | + ipcRenderer.on("receive-google-connected", handleReceiveConnected) |
127 | 67 |
|
128 | | - return ( |
129 | | - <PhotoList images={images} fetching={fetching} |
130 | | - onFetch={this.handleRequestFetch} |
131 | | - onClick={this.handleImageSelect} /> |
132 | | - ) |
133 | | - } |
| 68 | + return () => { |
| 69 | + ipcRenderer.removeListener("receive-google-photos-images", handleReceiveImages) |
| 70 | + ipcRenderer.removeListener("start-fetch-google-photos-images", handleStartFetch) |
| 71 | + ipcRenderer.removeListener("receive-google-connected", handleReceiveConnected) |
| 72 | + } |
| 73 | + }, []) |
| 74 | + |
| 75 | + |
| 76 | + if (!initialized) { |
| 77 | + return ( |
| 78 | + <div className="google-photos-wrap"> |
| 79 | + <div className="google-photos-cover"> |
| 80 | + <Loading position='absolute' /> |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + ) |
| 84 | + } |
| 85 | + |
| 86 | + if (!connected) { |
| 87 | + return ( |
| 88 | + <div className="google-photos-wrap"> |
| 89 | + <div className="google-photos-cover"> |
| 90 | + <Button variant='contained' className='btn btn_tistory' onClick={handleRequestAuth}> |
| 91 | + Google Photos 연결 |
| 92 | + </Button> |
| 93 | + </div> |
| 94 | + </div> |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + return ( |
| 99 | + <PhotoList images={images} fetching={fetching} |
| 100 | + onFetch={handleRequestFetch} |
| 101 | + onClick={handleImageSelect} /> |
| 102 | + ) |
134 | 103 | } |
135 | | - |
136 | | -export default GooglePhotos |
0 commit comments