-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: class component to functional component
- Loading branch information
Showing
7 changed files
with
66 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,59 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import React, { useState } from 'react'; | ||
import 'react-date-range/dist/styles.css'; | ||
import 'react-date-range/dist/theme/default.css'; | ||
import * as locales from 'react-date-range/dist/locale'; | ||
import { DateRange } from 'react-date-range'; | ||
import Range from './domain/range'; | ||
|
||
function CountResult({ counter, range }) { | ||
const [count, setCount] = useState(0); | ||
|
||
useEffect(() => { | ||
const handleRange = async () => { | ||
const cache = await counter.countWeekdayInRange(range); | ||
setCount(cache); | ||
}; | ||
handleRange(); | ||
}, [counter, range]); | ||
|
||
function CountResult({ count }) { | ||
return ( | ||
<p data-testid='result'>{count} 일</p> | ||
); | ||
} | ||
|
||
class App extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.counter = props.counter; | ||
} | ||
|
||
state = { | ||
function DateRangePicker({ onChangeRange }) { | ||
const [dateRange, setRange] = useState({ | ||
startDate: new Date(), | ||
endDate: new Date(), | ||
key: 'range' | ||
} | ||
|
||
toRange = (state) => { | ||
const { startDate, endDate } = state; | ||
key: 'dateRange' | ||
}); | ||
const handleChangeDateRange = ({ dateRange }) => { | ||
const range = new Range(dateRange.startDate, dateRange.endDate); | ||
|
||
return new Range(startDate, endDate); | ||
} | ||
setRange(dateRange); | ||
onChangeRange(range); | ||
}; | ||
|
||
update = ({ range }) => { | ||
this.setState(range); | ||
} | ||
return ( | ||
<DateRange | ||
locale={locales['ko']} | ||
editableDateInputs={true} | ||
onChange={item => handleChangeDateRange(item)} | ||
|
||
ranges={[dateRange]} | ||
dateDisplayFormat={'yyyy/MM/dd'} | ||
/> | ||
); | ||
} | ||
|
||
render() { | ||
const range = this.state; | ||
function App({ counter }) { | ||
const [count, setCount] = useState(0); | ||
const handleChangeRange = async (range) => { | ||
const newCount = await counter.countWeekdayInRange(range); | ||
setCount(newCount); | ||
}; | ||
|
||
return ( | ||
<div className="App"> | ||
<h1>날짜 수 세기</h1> | ||
<DateRange | ||
locale={locales['ko']} | ||
editableDateInputs={true} | ||
onChange={item => this.update(item)} | ||
moveRangeOnFirstSelection={false} | ||
ranges={[range]} | ||
dateDisplayFormat={'yyyy/MM/dd'} | ||
/> | ||
<CountResult | ||
counter={this.counter} | ||
range={this.toRange(range)} | ||
/> | ||
</div> | ||
); | ||
} | ||
return ( | ||
<div className="App"> | ||
<h1>날짜 수 세기</h1> | ||
<DateRangePicker | ||
onChangeRange={handleChangeRange} | ||
/> | ||
<CountResult | ||
count={count} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import format from 'date-fns/format'; | ||
|
||
function getKey(date) { | ||
return format(date, 'yyyy/MM'); | ||
} | ||
|
||
export default class HolidayCache { | ||
constructor(dataSource) { | ||
this.dataSource = dataSource; | ||
this.cache = {}; | ||
} | ||
|
||
getHolidaysIn = async (month) => { | ||
const key = getKey(month); | ||
if (!(key in Object.keys(this.cache))) { | ||
this.cache[key] = await this.dataSource.getHolidaysIn(month); | ||
} | ||
return this.cache[key]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters