Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ElapsedDaysをReactから素のJSに置き換えた #8371

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 0 additions & 78 deletions app/javascript/components/ElapsedDays.jsx

This file was deleted.

22 changes: 16 additions & 6 deletions app/javascript/components/Products.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react'
import React, { useRef, useEffect } from 'react'
import useSWR from 'swr'
import Pagination from './Pagination'
import LoadingListPlaceholder from './LoadingListPlaceholder'
import UnconfirmedLink from './UnconfirmedLink'
import Product from './Product'
import fetcher from '../fetcher'
import ElapsedDays from './ElapsedDays'
import elapsedDays from '../elapsed-days.js'
import usePage from './hooks/usePage'

export default function Products({
Expand Down Expand Up @@ -93,6 +93,19 @@ export default function Products({
return `${elapsedDays}days-elapsed`
}

const elapsedDaysRef = useRef(null)

useEffect(() => {
if (elapsedDaysRef.current) {
const elapsedDaysElement = elapsedDays({
countProductsGroupedBy,
productDeadlineDay
})
elapsedDaysRef.current.innerHTML = ''
elapsedDaysRef.current.appendChild(elapsedDaysElement)
}
}, [countProductsGroupedBy, productDeadlineDay])

if (error) return <>エラーが発生しました。</>

if (!data) {
Expand Down Expand Up @@ -225,10 +238,7 @@ export default function Products({
<UnconfirmedLink label={unconfirmedLinksName()} />
</div>

<ElapsedDays
countProductsGroupedBy={countProductsGroupedBy}
productDeadlineDay={productDeadlineDay}
/>
<div className="page-body__column is-sub" ref={elapsedDaysRef}></div>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

元々のコードに合わせて、下記のようにした方が良いかなーと思いました!

Suggested change
<div className="page-body__column is-sub" ref={elapsedDaysRef}></div>
<nav className="page-body__column is-sub" ref={elapsedDaysRef}></nav>

</div>
</div>
)
Expand Down
94 changes: 94 additions & 0 deletions app/javascript/elapsed-days.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
export default function elapsedDays({
countProductsGroupedBy,
productDeadlineDay
}) {
const activeClass = (quantity) => (quantity ? 'is-active' : 'is-inactive')

const createNavigationItem = ({
elapsedDays,
additionalClass = '',
todayProducts
}) => {
const li = createList(elapsedDays, additionalClass)
const a = createAnchor(elapsedDays)
const span = createSpan(elapsedDays, todayProducts)

a.appendChild(span)
li.appendChild(a)
return li
}

const createList = (elapsedDays, additionalClass) => {
const li = document.createElement('li')
li.className = `page-nav__item ${additionalClass} ${activeClass(
countProductsGroupedBy(elapsedDays)
)}`.trim()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

教えてください🙇ここのtrim()がなぜ必要なのかがわからなかったです💦

return li
}

const createAnchor = (elapsedDays) => {
const a = document.createElement('a')
a.className = 'page-nav__item-link'
a.href = `#${elapsedDays}days-elapsed`
return a
}

const createSpan = (elapsedDays, todayProducts) => {
const span = document.createElement('span')
span.className = 'page-nav__item-link-inner'
span.textContent =
todayProducts ||
`${elapsedDays}日${
elapsedDays >= 6 ? '以上' : ''
}経過 (${countProductsGroupedBy(elapsedDays)})`
return span
}

const nav = document.createElement('nav')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらのコメントのようにすると、navは不要になりそうだと思いました。


const div = document.createElement('div')
div.className = 'page-nav a-card'

const ol = document.createElement('ol')
ol.className = 'page-nav__items elapsed-days'

ol.appendChild(
createNavigationItem({
elapsedDays: productDeadlineDay + 2,
additionalClass: 'is-reply-deadline border-b-0'
})
)
ol.appendChild(
createNavigationItem({
elapsedDays: productDeadlineDay + 1,
additionalClass: 'is-reply-alert border-b-0'
})
)
ol.appendChild(
createNavigationItem({
elapsedDays: productDeadlineDay,
additionalClass: 'is-reply-warning border-b-0'
})
)

Array.from({ length: productDeadlineDay - 1 }, (_, index) => index + 1)
.reverse()
.forEach((passedDay) =>
ol.appendChild(
createNavigationItem({
elapsedDays: passedDay
})
)
)

ol.appendChild(
createNavigationItem({
elapsedDays: 0,
todayProducts: `今日提出 (${countProductsGroupedBy(0)})`
})
)

div.appendChild(ol)
nav.appendChild(div)
return nav
}