From 2433eb4630f4dbabb7bc85f8a3476cd83c9a2c32 Mon Sep 17 00:00:00 2001 From: Lee-Null Date: Wed, 26 May 2021 23:49:31 +0900 Subject: [PATCH] feat: class component to functional component --- cypress/integration/one_day_e2e.spec.js | 5 +- cypress/integration/range_date_e2e.spec.js | 3 +- cypress/integration/ui.spec.js | 3 +- cypress/support/commands.js | 3 +- src/App.js | 88 ++++++++++------------ src/cache/holidayCache.js | 20 +++++ src/index.js | 3 +- 7 files changed, 66 insertions(+), 59 deletions(-) create mode 100644 src/cache/holidayCache.js diff --git a/cypress/integration/one_day_e2e.spec.js b/cypress/integration/one_day_e2e.spec.js index 9b26947..5e96370 100644 --- a/cypress/integration/one_day_e2e.spec.js +++ b/cypress/integration/one_day_e2e.spec.js @@ -1,12 +1,11 @@ -/// +/// -const holidays = ["20210801"]; +const holidays = ['20210801']; describe('end to end tests for a day', () => { beforeEach(() => { cy.runServer(holidays); cy.visit('/'); - cy.waitServer(); }); it('count 1 weekday for a weekday', () => { diff --git a/cypress/integration/range_date_e2e.spec.js b/cypress/integration/range_date_e2e.spec.js index 30da42e..48fb6ac 100644 --- a/cypress/integration/range_date_e2e.spec.js +++ b/cypress/integration/range_date_e2e.spec.js @@ -1,10 +1,9 @@ -/// +/// describe('end to end tests for a range of days', () => { beforeEach(() => { cy.runServer([]); cy.visit('/'); - cy.waitServer(); }); it('count 5 weekdays for a week in a month.', () => { diff --git a/cypress/integration/ui.spec.js b/cypress/integration/ui.spec.js index 5f2c908..12cc387 100644 --- a/cypress/integration/ui.spec.js +++ b/cypress/integration/ui.spec.js @@ -1,10 +1,9 @@ -/// +/// describe('test whether ui elements are working', () => { beforeEach(() => { cy.runServer([]); cy.visit('/'); - cy.waitServer(); }); it('show today if the range is not selected', () => { diff --git a/cypress/support/commands.js b/cypress/support/commands.js index 17c3034..c1c5a81 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -1,4 +1,4 @@ -/// +/// import { format } from 'date-fns'; @@ -50,5 +50,4 @@ Cypress.Commands.add('setDateRange', setDateRange); Cypress.Commands.add('setDate', setDate); Cypress.Commands.add('runServer', runFakeServer); - Cypress.Commands.add('waitServer', waitFakeServer); \ No newline at end of file diff --git a/src/App.js b/src/App.js index 788dc14..3b00aaf 100644 --- a/src/App.js +++ b/src/App.js @@ -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 (

{count} 일

); } -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 ( + 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 ( -
-

날짜 수 세기

- this.update(item)} - moveRangeOnFirstSelection={false} - ranges={[range]} - dateDisplayFormat={'yyyy/MM/dd'} - /> - -
- ); - } + return ( +
+

날짜 수 세기

+ + +
+ ); } export default App; \ No newline at end of file diff --git a/src/cache/holidayCache.js b/src/cache/holidayCache.js new file mode 100644 index 0000000..ceb0e62 --- /dev/null +++ b/src/cache/holidayCache.js @@ -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]; + } +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index fc4114c..921cd6b 100644 --- a/src/index.js +++ b/src/index.js @@ -3,11 +3,12 @@ import React from 'react'; import App from './App'; import WeekdayCounter from './domain/weekdayCounter'; import * as HerokuHolidayDataSource from './dataSource/herokuHolidayDataSource'; +import HolidayCache from './cache/holidayCache'; ReactDOM.render( , document.getElementById('root')