[FWA-5] Home Create and List

This commit is contained in:
2024-07-03 08:57:50 +00:00
parent 1c205c69ac
commit b938f296c1
33 changed files with 414 additions and 93 deletions

16
frontend/src/api/house.js Normal file
View File

@ -0,0 +1,16 @@
import { protocol } from './index'
import { GET_HOUSES_LIST, POST_HOUSE_CREATE } from './url'
export const postCreateHouse = (payload) => {
return protocol.post(POST_HOUSE_CREATE, payload)
}
export const getAllHouse = ({ page, pageSize }) => {
return protocol.get(
GET_HOUSES_LIST({
page: page(),
pageSize: pageSize(),
}),
{},
)
}

View File

@ -3,3 +3,6 @@ export const POST_LOGOUT = '/api/auth/logout'
export const POST_REFRESH = '/api/auth/refresh'
export const GET_USER_PROFILE = '/api/user/me'
export const PUT_UPDATE_USER_PROFILE = '/api/user/update-profile'
export const POST_HOUSE_CREATE = '/api/house/create'
export const GET_HOUSES_LIST = ({ page, pageSize }) =>
`/api/house/all?page=${page}&pageSize=${pageSize}`

View File

@ -4,7 +4,6 @@ import Textarea from '@components/common/Textarea'
import { createForm } from '@felte/solid'
import { validator } from '@felte/validator-yup'
import useLanguage from '@hooks/useLanguage'
import useToast from '@hooks/useToast'
import {
IconAddressBook,
IconCirclePlus,
@ -33,7 +32,6 @@ export default function AreaAdd(props) {
const [openModal, setOpenModal] = createSignal(false)
const [data, setData] = createSignal([])
const { language, isRequired } = useLanguage()
const notify = useToast()
const { form, reset, errors } = createForm({
extend: [validator({ schema: areaSchema(language, isRequired) })],
onSubmit: async (values) => {
@ -54,8 +52,6 @@ export default function AreaAdd(props) {
setData((prev) => [...prev.slice(0, index), ...prev.slice(index + 1)])
}
// console.log(error())
return (
<div class="form-control mb-3">
<div class="join join-vertical">
@ -95,7 +91,7 @@ export default function AreaAdd(props) {
{(item, index) => (
<AreaItem
{...item}
name={props.name}
formName={props.name}
key={index()}
onDelete={onDeleteAreaItem}
/>

View File

@ -9,12 +9,12 @@ export default function AreaItem(props) {
<p class="text-xs">{props.description}</p>
<input
type="hidden"
name={`${props.name}.${props.key}.name`}
name={`${props.formName}.${props.key}.name`}
value={props.name}
/>
<input
type="hidden"
name={`${props.name}.${props.key}.desc`}
name={`${props.formName}.${props.key}.desc`}
value={props.description}
/>
</div>

View File

@ -5,6 +5,7 @@ import useAuth from '@hooks/useAuth'
import useToast from '@hooks/useToast'
import { A } from '@solidjs/router'
import { IconLogout, IconMenuDeep, IconUserCircle } from '@tabler/icons-solidjs'
import { Helpers } from '@utils/helper'
import { Show, onMount } from 'solid-js'
export default function Header() {
@ -71,7 +72,7 @@ export default function Header() {
</span>
</li>
<li class="mb-1">
<A href="/me">
<A href={Helpers.getRoutePath('profile')}>
<IconUserCircle size={15} />
Profile
</A>

View File

@ -5,9 +5,10 @@ import { A } from '@solidjs/router'
import {
IconBuildingWarehouse,
IconDashboard,
IconHome,
IconMapPin,
IconTriangle,
} from '@tabler/icons-solidjs'
import { Helpers } from '@utils/helper'
import { For, Show, mergeProps } from 'solid-js'
import { Dynamic } from 'solid-js/web'
import './navbar.scss'
@ -16,19 +17,19 @@ const { language } = useLanguage()
export const NAV_ITEM = (admin = false) => [
{
path: '/dashboard',
pathName: 'dashboard',
show: admin,
icon: IconDashboard,
text: language?.ui.dashboard,
},
{
path: '/house',
pathName: 'location',
show: true,
icon: IconHome,
icon: IconMapPin,
text: language?.ui.house,
},
{
path: '/warehouse',
pathName: 'warehouse',
show: true,
icon: IconBuildingWarehouse,
text: language?.ui.location,
@ -39,7 +40,7 @@ function NavbarItem(props) {
const merged = mergeProps({ active: true }, props)
return (
<Show
when={merged.active && merged.path}
when={merged.active && merged.pathName}
fallback={
<>
<Dynamic component={merged.icon} />
@ -47,7 +48,10 @@ function NavbarItem(props) {
</>
}
>
<A class="hover:text-fu-black" href={merged.path}>
<A
class="hover:text-fu-black"
href={Helpers.getRoutePath(merged.pathName)}
>
<Dynamic component={merged.icon} />
{merged.text}
</A>
@ -126,7 +130,7 @@ export default function Navbar() {
</span>
</li>
<li class="mb-1">
<A href="/me">Profile</A>
<A href={Helpers.getRoutePath('profile')}>Profile</A>
</li>
<li>
<a onClick={logOut}>Logout</a>

View File

@ -0,0 +1,64 @@
import { DOTS, usePagination } from '@hooks/usePagination'
import { For, Show, splitProps } from 'solid-js'
export default function Pagination(props) {
const [localProps, onEvents] = splitProps(
props,
['currentPage', 'totalCount', 'pageSize'],
['onPageChange'],
)
const paginationRange = usePagination(localProps)
const onNext = () => {
onEvents.onPageChange(localProps.currentPage() + 1)
}
const onPrevious = () => {
onEvents.onPageChange(localProps.currentPage() - 1)
}
return (
<Show when={localProps.currentPage !== 0 || paginationRange().length > 1}>
<div class="pagination join">
<button
class="join-item btn btn-sm border border-gray-300"
onClick={onPrevious}
>
«
</button>
<For each={paginationRange()}>
{(page) => {
if (page === DOTS) {
return (
<button
class="join-item btn btn-sm hidden lg:block border !border-gray-300"
disabled
>
...
</button>
)
}
return (
<button
class="join-item btn btn-sm hidden lg:block border border-gray-300"
classList={{
'!block btn-primary': page === localProps.currentPage(),
}}
onClick={[onEvents.onPageChange, page]}
>
{page}
</button>
)
}}
</For>
<button
class="join-item btn btn-sm border border-gray-300"
onClick={onNext}
>
»
</button>
</div>
</Show>
)
}

View File

@ -0,0 +1,2 @@
export * from './Pagination'
export { default } from './Pagination'

View File

@ -0,0 +1,60 @@
import { createMemo } from 'solid-js'
export const DOTS = '...'
const range = (start, end) => {
let length = end - start + 1
return Array.from({ length }, (_, idx) => idx + start)
}
export const usePagination = ({
totalCount,
pageSize,
currentPage,
siblingCount = 1,
}) => {
const paginationRange = createMemo(() => {
const totalPageCount = Math.ceil(totalCount / pageSize())
const totalPageNumbers = siblingCount + 5
if (totalPageNumbers >= totalPageCount) {
return range(1, totalPageCount)
}
const leftSiblingIndex = Math.max(currentPage() - siblingCount, 1)
const rightSiblingIndex = Math.min(
currentPage() + siblingCount,
totalPageCount,
)
const shouldShowLeftDots = leftSiblingIndex > 2
const shouldShowRightDots = rightSiblingIndex < totalPageCount - 2
const firstPageIndex = 1
const lastPageIndex = totalPageCount
if (!shouldShowLeftDots && shouldShowRightDots) {
let leftItemCount = 3 + 2 * siblingCount
let leftRange = range(1, leftItemCount)
return [...leftRange, DOTS, totalPageCount]
}
if (shouldShowLeftDots && !shouldShowRightDots) {
let rightItemCount = 3 + 2 * siblingCount
let rightRange = range(
totalPageCount - rightItemCount + 1,
totalPageCount,
)
return [firstPageIndex, DOTS, ...rightRange]
}
if (shouldShowLeftDots && shouldShowRightDots) {
let middleRange = range(leftSiblingIndex, rightSiblingIndex)
return [firstPageIndex, DOTS, ...middleRange, DOTS, lastPageIndex]
}
})
return paginationRange
}

View File

@ -9,17 +9,17 @@
"changeInfo": "Thông tin tài khoản",
"save": "Lưu",
"clear": "Xóa",
"house": "Nhà",
"house": "Địa điểm",
"action": "Thao Tác",
"createNew": "Tạo mới",
"location": "Nhà kho",
"displayName": "Tên hiển thị",
"newPassword": "Mật khẩu mới",
"confirmNewPassword": "Nhập lại mật khẩu",
"newHouse": "Tạo nhà mới",
"houseName": "Tên nhà",
"houseIcon": "Ký tự nhà",
"houseAddress": "Địa chỉ nhà",
"newHouse": "Tạo địa điểm mới",
"houseName": "Tên địa điểm",
"houseIcon": "Ký tự",
"houseAddress": "Địa chỉ",
"areas": "Khu vực",
"areaName": "Tên khu vực",
"areaDesc": "Mô tả",
@ -30,7 +30,12 @@
"confirm": "Xác nhận",
"cancel": "Huỷ",
"findIconHere": "Tìm ở đây",
"empty": "Trống"
"empty": "Trống",
"error": "lỗi!",
"success": "Thành công!",
"info": "Thông tin",
"loading": "Đang tải...",
"showing": "Hiển thị"
},
"table": {
"columnName": {
@ -42,10 +47,17 @@
}
},
"message": {
"CREATED_USER": "Username already registered!",
"LOGIN_WRONG": "Your username or password input is wrong!",
"USER_LOCK": "Your Account was locked",
"IS_REQUIRED": "%s là bắt buộc",
"PASSWORD_MUSTMATCH": "Cần nhập trùng với mật khẩu"
"CREATE_USER_SUCCESS": "Tạo tài khoản thành công!",
"CREATED_USER": "Tên tài khoản đã có người sử dụng!",
"LOGIN_WRONG": "Bạn nhập sai tên người dùng hoặc mật khẩu.",
"USER_LOCK": "Tài khoản bạn hiện đang bị khóa.",
"IS_REQUIRED": "%s là bắt buộc.",
"PASSWORD_MUSTMATCH": "Cần nhập trùng với mật khẩu.",
"UPDATE_SUCCESS": "Cập nhật thành công!",
"UPDATE_PROFILE_SUCCESS": "Cập nhật hồ sơ thành công!",
"UPDATE_FAIL": "Cập nhật thất bại!",
"API_CALL_FAIL": "Call API có vẫn đề!",
"CREATE_HOUSE_FAIL": "Tạo địa điểm mới thất bại!",
"CREATE_HOUSE_SUCCESS": "Tạo địa điểm mới thành công!"
}
}

View File

@ -1,6 +1,7 @@
import { NAV_ITEM } from '@components/Navbar'
import { useSiteContext } from '@context/SiteContext'
import { useNavigate } from '@solidjs/router'
import { Helpers } from '@utils/helper'
import { createEffect } from 'solid-js'
function getFirstItem(array) {
@ -18,7 +19,12 @@ export default function Home() {
createEffect(() => {
if (store?.userInfo?.isAdmin) {
const first = getFirstItem(NAV_ITEM(store?.userInfo?.isAdmin))
navigate(first ? first.path : '/me', { replace: true })
navigate(
first
? Helpers.getRoutePath(first.pathName)
: Helpers.getRoutePath('profile'),
{ replace: true },
)
}
})

View File

@ -1,8 +1,10 @@
import ViewSwitch, { VIEWDATA } from '@components/ViewSwitch'
import useLanguage from '@hooks/useLanguage'
import { IconHome } from '@tabler/icons-solidjs'
import { createSignal } from 'solid-js'
import { For, createEffect, createResource, createSignal } from 'solid-js'
import { getAllHouse } from '@api/house'
import Pagination from '@components/common/Pagination'
import { A } from '@solidjs/router'
import {
IconHome2,
@ -13,9 +15,28 @@ import {
} from '@tabler/icons-solidjs'
import './house.scss'
const PAGE_SIZE = [10, 50, 100]
const fetchHouses = async ({ page, pageSize }) => {
const response = await getAllHouse({ page, pageSize })
return response
}
export default function House() {
const { language } = useLanguage()
const [pageSize, setPageSize] = createSignal(PAGE_SIZE[0])
const [currentPage, setCurrentPage] = createSignal(1)
const [view, setView] = createSignal(VIEWDATA['list'])
const [houses] = createResource(
{ page: currentPage, pageSize: pageSize },
fetchHouses,
)
createEffect(() => {
if (houses()) {
console.log(houses()?.data)
}
})
const onEdit = () => {
console.log('edit')
@ -25,6 +46,11 @@ export default function House() {
console.log('delete')
}
const onSetPageSize = (pageSize) => {
setPageSize(pageSize)
setCurrentPage(1)
}
return (
<div class="house">
<div class="flex items-center gap-2 mb-5 text-xl">
@ -36,7 +62,7 @@ export default function House() {
<div class="page-topbar flex justify-between mb-4">
<ViewSwitch switchView={setView} />
<A
href="/house/create"
href="/location/create"
class="btn btn-success text-white hover:text-white btn-sm"
>
<IconSquareRoundedPlus size={15} />
@ -104,6 +130,42 @@ export default function House() {
</div>
</div>
</div>
<div class="page-botbar flex max-xs:flex-col justify-between items-center gap-2 mt-5">
<div class="bar-left flex gap-2 justify-start items-center">
<div class="py-2 px-3 border rounded-lg border-gray-300 leading-none">
<span>{language.ui.showing}: 1 - 10 / 100</span>
</div>
<div class="dropdown dropdown-top dropdown-end">
<div
tabindex="0"
role="button"
class="btn btn-sm border border-gray-300"
>
{pageSize()}
</div>
<ul
tabindex="0"
class="dropdown-content menu bg-base-100 rounded-box z-[1] w-32 p-2 shadow mb-2"
>
<For each={PAGE_SIZE.reverse()}>
{(pageSize) => (
<li>
<a onClick={[onSetPageSize, pageSize]}>{pageSize}</a>
</li>
)}
</For>
</ul>
</div>
</div>
<div class="bar-right">
<Pagination
currentPage={currentPage}
totalCount={1000}
pageSize={pageSize}
onPageChange={setCurrentPage}
/>
</div>
</div>
</div>
)
}

View File

@ -1,15 +1,18 @@
import { postCreateHouse } from '@api/house'
import AreaAdd from '@components/AreaAdd'
import TextInput from '@components/common/TextInput'
import { createForm } from '@felte/solid'
import { validator } from '@felte/validator-yup'
import useLanguage from '@hooks/useLanguage'
import { A } from '@solidjs/router'
import useToast from '@hooks/useToast'
import { A, useNavigate } from '@solidjs/router'
import {
IconAddressBook,
IconHomePlus,
IconIcons,
IconMapPinPlus,
IconTag,
} from '@tabler/icons-solidjs'
import { Helpers } from '@utils/helper'
import * as yup from 'yup'
const houseSchema = (language, isRequired) =>
@ -17,15 +20,41 @@ const houseSchema = (language, isRequired) =>
icon: yup.string().required(isRequired(language.ui.houseIcon)),
name: yup.string().required(isRequired(language.ui.houseName)),
address: yup.string().required(isRequired(language.ui.houseAddress)),
areas: yup.array().required(isRequired(language.ui.areas)),
areas: yup
.array()
.min(1, isRequired(language.ui.areas))
.required(isRequired(language.ui.areas)),
})
export default function HouseCreate() {
const { language, isRequired } = useLanguage()
const notify = useToast()
const navigate = useNavigate()
const { form, errors } = createForm({
extend: [validator({ schema: houseSchema(language, isRequired) })],
onSubmit: async (values) => {
console.log(values)
const resp = await postCreateHouse(values)
return resp
},
onSuccess: (resp) => {
if (resp.status === 200) {
notify.success({
title: language.ui.success,
description:
language.message[resp.data] ||
language.message['CREATE_HOUSE_SUCCESS'],
})
navigate(Helpers.getRoutePath('location'), { replace: true })
}
},
onError: (error) => {
notify.error({
title: language.ui.error,
description:
language.message[error.data] || language.message['API_CALL_FAIL'],
closable: true,
})
},
})
@ -34,14 +63,14 @@ export default function HouseCreate() {
<div class="text-sm breadcrumbs mb-2">
<ul>
<li>
<A href="/house">{language.ui.house}</A>
<A href={Helpers.getRoutePath('location')}>{language.ui.house}</A>
</li>
<li>{language.ui.newHouse}</li>
</ul>
</div>
<div class="flex items-center gap-2 mb-5 text-xl">
<span class="text-secondary">
<IconHomePlus size={30} />
<IconMapPinPlus size={30} />
</span>
{language.ui.newHouse}
</div>
@ -90,7 +119,13 @@ export default function HouseCreate() {
/>
</div>
<div class="col-auto lg:col-span-2">
<AreaAdd name="areas" error={errors('areas')} />
<AreaAdd
name="areas"
error={
errors('areas') &&
Helpers.clearArrayWithNullObject(errors('areas'))
}
/>
</div>
</div>
<div class="card-actions">

View File

@ -21,7 +21,7 @@ export default function Layout(props) {
<div class="drawer lg:drawer-open">
<input id="nav-menu" type="checkbox" class="drawer-toggle" />
<div class="drawer-content flex flex-col">
<main class="main-content p-3">{props.children}</main>
<main class="main-content p-3 pb-5">{props.children}</main>
</div>
<Navbar />
</div>

View File

@ -46,16 +46,17 @@ export default function Profile() {
setUser(resp.data)
reset()
notify.success({
title: 'Update profile success!',
description: 'Your profile has been updated!',
title: language.ui.success,
description:
language.message[resp.data] ||
language.message['CREATE_USER_SUCCESS'],
})
}
} catch (error) {
notify.error({
title: 'Update profile fail!',
description: error?.data
? language.message[error.data]
: 'Your username or password input is wrong!',
title: language.ui.error,
description:
language.message[error?.data] || language.message['API_CALL_FAIL'],
closable: true,
})
}

View File

@ -2,36 +2,42 @@ import { lazy } from 'solid-js'
export const ROUTES = [
{
name: 'home',
path: '/',
components: lazy(() => import('@pages/Home')),
filter: {},
show: true,
},
{
name: 'profile',
path: '/me',
components: lazy(() => import('@pages/Profile')),
filter: {},
show: true,
},
{
name: 'dashboard',
path: '/dashboard',
components: lazy(() => import('@pages/Dashboard')),
filter: {},
show: true,
},
{
path: '/house',
name: 'location',
path: '/location',
components: lazy(() => import('@pages/House')),
filter: {},
show: true,
},
{
path: '/house/create',
name: 'create-location',
path: '/location/create',
components: lazy(() => import('@pages/HouseCreate')),
filter: {},
show: true,
},
{
name: 'warehouse',
path: '/warehouse',
components: lazy(() => import('@pages/WareHouse')),
filter: {},

View File

@ -1,3 +1,4 @@
import { ROUTES } from '@routes/routes'
import { AES, enc } from 'crypto-js'
import { LOGIN_KEY, SECRET_KEY, STORE_KEY } from './enum'
@ -58,16 +59,18 @@ export class Helpers {
}
static clearArrayWithNullObject = (array) => {
console.log(array)
array.forEach((element, i) => {
const obk = this.clearObject(element)
if (obk) array.splice(i, 1)
})
// for (let i = 0; i < array.length; i++) {
// if (array[i] === null || array[i] === undefined) {
// array.splice(i, 1)
// }
// }
if (array instanceof Array) {
array.forEach((element, i) => {
if (element instanceof Object) {
const obk = this.clearObject(element)
if (obk) array.splice(i, 1)
}
})
}
return array.length > 0 ? array : null
}
static getRoutePath = (pathName) =>
ROUTES.filter((r) => r.name === pathName)[0].path
}

View File

@ -6,6 +6,9 @@ export default {
content: ['./src/**/*.{js,jsx}'],
theme: {
extend: {
screens: {
xs: '400px',
},
colors: {
fu: {
white: '#fff',