2022-06-05 17:09:36 +02:00
|
|
|
import { useState, useEffect } from 'react'
|
2022-06-02 19:39:59 +02:00
|
|
|
import { useTranslation } from 'next-i18next'
|
|
|
|
import { configs } from 'shared/designs/index.js'
|
2023-01-29 16:44:02 +01:00
|
|
|
import { DesignIcon } from 'shared/components/icons.mjs'
|
2022-06-05 17:09:36 +02:00
|
|
|
import Worm from 'shared/components/worm.js'
|
|
|
|
import { strapiHost } from 'shared/config/freesewing.mjs'
|
|
|
|
import Link from 'next/link'
|
|
|
|
|
|
|
|
const colors = {
|
|
|
|
1: 'bg-pink-300',
|
|
|
|
2: 'bg-green-400',
|
|
|
|
3: 'bg-yellow-400',
|
|
|
|
4: 'bg-orange-400',
|
2022-12-24 16:45:11 +01:00
|
|
|
5: 'bg-red-400',
|
2022-06-05 17:09:36 +02:00
|
|
|
}
|
2022-12-24 16:45:11 +01:00
|
|
|
const Difficulty = ({ score = 1 }) => {
|
2022-06-05 17:09:36 +02:00
|
|
|
const icons = []
|
2022-12-24 16:45:11 +01:00
|
|
|
for (let i = 0; i < score; i++) {
|
2022-06-05 17:09:36 +02:00
|
|
|
icons.push(<DesignIcon className={`w-8 h-8 -ml-3 text-gray-900`} />)
|
|
|
|
}
|
|
|
|
|
|
|
|
return icons
|
|
|
|
}
|
|
|
|
|
2022-07-10 16:35:05 +02:00
|
|
|
const loadDesign = async (design, setExamples) => {
|
|
|
|
// Strapi filtering syntax
|
2022-12-24 16:45:11 +01:00
|
|
|
const url =
|
|
|
|
`${strapiHost}/showcaseposts?_locale=en&_sort=date:DESC` +
|
2022-07-10 16:35:05 +02:00
|
|
|
`&_where[_or][0][design1_eq]=${design}` +
|
|
|
|
`&_where[_or][1][design2_eq]=${design}` +
|
|
|
|
`&_where[_or][2][design3_eq]=${design}` +
|
|
|
|
`&_limit=5`
|
|
|
|
await fetch(url)
|
2022-12-24 16:45:11 +01:00
|
|
|
.then((response) => response.json())
|
|
|
|
.then((data) =>
|
|
|
|
setExamples(
|
|
|
|
data.map((post) => ({
|
|
|
|
slug: `/showcase/${post.slug}`,
|
|
|
|
img: `${strapiHost}${post.image.formats.thumbnail.url}`,
|
|
|
|
title: post.title,
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.catch((err) => console.log(err))
|
2022-07-10 16:35:05 +02:00
|
|
|
}
|
2022-06-02 19:39:59 +02:00
|
|
|
|
|
|
|
const Design = ({ design }) => {
|
|
|
|
const { t } = useTranslation(['patterns'])
|
2022-12-24 16:45:11 +01:00
|
|
|
const { difficulty = 3 } = configs[design]
|
2022-06-05 17:09:36 +02:00
|
|
|
|
|
|
|
const [examples, setExamples] = useState([])
|
|
|
|
|
2022-07-10 16:35:05 +02:00
|
|
|
useEffect(() => {
|
|
|
|
loadDesign(design, setExamples)
|
2022-12-24 16:45:11 +01:00
|
|
|
}, [design, setExamples])
|
2022-06-02 19:39:59 +02:00
|
|
|
|
|
|
|
return (
|
2022-12-24 16:45:11 +01:00
|
|
|
<div
|
|
|
|
className={`
|
2022-06-02 19:39:59 +02:00
|
|
|
my-8
|
2022-06-07 21:35:01 +02:00
|
|
|
w-full sm:w-96
|
2022-06-05 17:09:36 +02:00
|
|
|
shadow-lg
|
|
|
|
border
|
2022-06-02 19:39:59 +02:00
|
|
|
rounded-lg
|
2022-06-05 17:09:36 +02:00
|
|
|
flex flex-col
|
|
|
|
text-center
|
|
|
|
overflow-clip
|
|
|
|
relative
|
2022-06-07 21:35:01 +02:00
|
|
|
`}
|
|
|
|
>
|
2022-06-05 17:09:36 +02:00
|
|
|
{/* Link over the entire card */}
|
2022-12-24 22:51:03 +01:00
|
|
|
<Link
|
|
|
|
href={`/designs/${design}`}
|
|
|
|
className="absolute top-0 right-0 w-full h-full"
|
|
|
|
title={t(`${design}.t`)}
|
|
|
|
>
|
2022-12-24 16:45:11 +01:00
|
|
|
{' '}
|
2022-06-05 17:09:36 +02:00
|
|
|
</Link>
|
|
|
|
{/* Slanted corner ribbon with the difficulty */}
|
|
|
|
<div className="absolute top-0 right-0">
|
2022-12-24 16:45:11 +01:00
|
|
|
<div
|
|
|
|
className={`w-96 -mr-36 mt-8 pl-2 flex flex-row justify-center ${colors[difficulty]}`}
|
|
|
|
style={{ transform: 'rotate(45deg)' }}
|
2022-06-05 17:09:36 +02:00
|
|
|
>
|
|
|
|
<Difficulty score={difficulty} />
|
2022-06-02 19:39:59 +02:00
|
|
|
</div>
|
2022-06-05 17:09:36 +02:00
|
|
|
</div>
|
2022-06-07 21:35:01 +02:00
|
|
|
<h6 className="text-base-content m-0 pt-4 pl-4 text-left capitalize">{design}</h6>
|
2022-12-24 16:45:11 +01:00
|
|
|
<div
|
|
|
|
className={`grow aspect-[9/16] -mt-4
|
|
|
|
`}
|
|
|
|
style={{
|
|
|
|
backgroundImage: `url('/img/designs/${design}.png')`,
|
|
|
|
backgroundSize: 'contain',
|
|
|
|
backgroundPosition: '50% 50%',
|
|
|
|
backgroundRepeat: 'no-repeat',
|
|
|
|
}}
|
|
|
|
></div>
|
2022-06-07 21:35:01 +02:00
|
|
|
<div className="px-4 text-base-content">
|
|
|
|
<h2 className="text-base-content m-0 p-0 z-30">{t(`${design}.t`)}</h2>
|
2022-12-26 17:10:14 +01:00
|
|
|
<div className="text-base-content m-0 p-0 font-bold text-base">{t(`${design}.d`)}</div>
|
2022-06-07 21:35:01 +02:00
|
|
|
</div>
|
|
|
|
<div className="py-4 z-10">
|
2022-12-24 16:45:11 +01:00
|
|
|
<Worm list={examples} fixed />
|
2022-06-02 19:39:59 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Design
|