Files
link-free/src/components/Dash/index.tsx

124 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-12-13 00:36:38 -03:00
import { isEqual } from 'lodash'
2021-12-13 17:12:51 -03:00
import { useRouter } from 'next/router'
2021-12-13 00:36:38 -03:00
import { useEffect, useState } from 'react'
2021-12-13 17:12:51 -03:00
import { useData } from '../../hooks/useData'
2021-12-13 15:34:37 -03:00
import { api } from '../../services/api'
2021-12-14 14:18:50 -03:00
import type { Fonts } from '../../styles/stitches.config'
2021-12-13 17:12:51 -03:00
import type { Data } from '../../types/Data'
2021-12-13 00:36:38 -03:00
import {
getLocalStorageData,
removeLocalStorageData,
setLocalStorageData
} from '../../utils/localStorage'
2021-12-14 14:18:50 -03:00
import { ButtonVariantProps } from '../Button'
2021-12-13 00:36:38 -03:00
import { Home } from '../Home'
2021-12-14 14:18:50 -03:00
import { ColorSelect } from './ColorSelect'
2021-12-13 00:36:38 -03:00
import { DescriptionInput } from './DescriptionInput'
import { NameInput } from './NameInput'
import { Content, Preview, Wrapper } from './styles'
2021-12-13 17:12:51 -03:00
type DashProps = {
initialData: Data
}
export function Dash({ initialData }: DashProps) {
const { push } = useRouter()
const { data, setData } = useData()
2021-12-13 00:36:38 -03:00
const [isLoading, setIsLoading] = useState(true)
const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false)
useEffect(() => {
2021-12-13 17:12:51 -03:00
if (isLoading) {
const storageData = getLocalStorageData()
if (storageData) setData(storageData)
else setData(initialData)
2021-12-13 00:36:38 -03:00
2021-12-13 17:12:51 -03:00
setIsLoading(false)
2021-12-13 00:36:38 -03:00
}
2021-12-13 17:12:51 -03:00
if (!isLoading && !isEqual(initialData, data)) {
2021-12-13 00:36:38 -03:00
setHasUnsavedChanges(true)
setLocalStorageData(data)
} else {
setHasUnsavedChanges(false)
}
2021-12-13 17:12:51 -03:00
}, [data, initialData, isLoading, setData])
2021-12-13 00:36:38 -03:00
2021-12-13 15:34:37 -03:00
const handleSave = async () => {
try {
await api.put('data', { data })
removeLocalStorageData()
2021-12-13 17:12:51 -03:00
setHasUnsavedChanges(false)
push('/')
2021-12-13 15:34:37 -03:00
} catch (error) {
console.error(error)
}
}
2021-12-14 14:18:50 -03:00
const handleChangeButtonsSchema = (schema: ButtonVariantProps['styleSchema']) => {
2021-12-13 00:36:38 -03:00
setData({ ...data, settings: { ...data.settings, buttonsSchema: schema } })
}
2021-12-13 17:12:51 -03:00
const handleChangeOutline = (outline: ButtonVariantProps['outline']) => {
2021-12-13 00:36:38 -03:00
setData({ ...data, settings: { ...data.settings, outline } })
}
2021-12-13 17:12:51 -03:00
const handleChangeFont = (font: Fonts) => {
2021-12-13 00:36:38 -03:00
setData({ ...data, settings: { ...data.settings, font } })
}
return (
<Wrapper>
<Content>
2021-12-13 15:34:37 -03:00
<h1>
Dash{' '}
{hasUnsavedChanges && (
<>
- has unsaved changes <button onClick={() => handleSave()}>save</button>
</>
)}
</h1>
2021-12-13 00:36:38 -03:00
<NameInput />
<DescriptionInput />
2021-12-13 17:12:51 -03:00
<select
value={data.settings.font}
onChange={e => handleChangeFont(e.target.value as Fonts)}
>
2021-12-13 00:36:38 -03:00
<option value="square">Baloo</option>
<option value="montserrat">Montserrat</option>
<option value="roboto">Roboto</option>
</select>
2021-12-14 14:18:50 -03:00
<ColorSelect prop="texts" />
<ColorSelect prop="socialLinks" />
<ColorSelect prop="buttonLinks" />
<ColorSelect prop="background" />
2021-12-13 00:36:38 -03:00
<select
2021-12-13 17:12:51 -03:00
value={data.settings.buttonsSchema as string}
2021-12-14 14:18:50 -03:00
onChange={e =>
handleChangeButtonsSchema(e.target.value as ButtonVariantProps['styleSchema'])
}
2021-12-13 00:36:38 -03:00
>
<option value="square">Square</option>
<option value="rounded">Rounded</option>
<option value="pill">Pill</option>
</select>
<input
type="checkbox"
2021-12-13 17:12:51 -03:00
defaultChecked={data.settings.outline as boolean}
onChange={() =>
handleChangeOutline(!data.settings.outline as ButtonVariantProps['outline'])
}
2021-12-13 00:36:38 -03:00
/>
</Content>
<Preview>
<Home />
</Preview>
</Wrapper>
)
}