-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0e6865
commit edf03c0
Showing
12 changed files
with
215 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
import network from "@/utils/network"; | ||
import { AuthRequest, AuthResponse } from "./auth.types"; | ||
import { AuthRequest, AuthResponse } from "./user.types"; | ||
import { UserInfo } from "@/stores/auh.types"; | ||
|
||
const getUserInfo = async (): Promise<UserInfo> => network.get('/users/info') | ||
const getUserList = async (): Promise<UserInfo[]> => network.get('/users') | ||
const loginUser = async (request: AuthRequest): Promise<AuthResponse> => network.post('/users/auth', JSON.stringify(request)) | ||
const registerUser = async (request: AuthRequest): Promise<AuthResponse> => network.post('/users', JSON.stringify(request)) | ||
const updateUser = async (request: UserInfo) => network.put("/users", JSON.stringify(request)) | ||
|
||
export default { | ||
getUserInfo, | ||
getUserList, | ||
loginUser, | ||
registerUser, | ||
updateUser, | ||
|
||
QUERY_KEY_GET_USER_INFO: "getUserInfo" | ||
QUERY_KEY_GET_USER_INFO: "getUserInfo", | ||
QUERY_KEY_GET_USER_LIST: "getUserList" | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import Modal from "@/components/modal" | ||
import { Card, Chip, Grid, Option, Select, Typography } from "@mui/joy" | ||
import { Box, TextField } from "@mui/material" | ||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" | ||
import { createLazyRoute } from "@tanstack/react-router" | ||
|
||
import settingAPI from '@/apis/setting' | ||
import userAPI from '@/apis/user' | ||
import { useState } from "react" | ||
import { Actuator } from "@/apis/setting.types" | ||
import { UserInfo } from "@/stores/auh.types" | ||
import useNotification from "@/stores/notification" | ||
|
||
type itemState = Actuator | UserInfo | null | ||
|
||
const isUserInfo = (item: itemState): boolean => !item || "user_id" in item | ||
|
||
const renderModalTitle = (item: itemState): string => { | ||
if (!item) return "Empty" | ||
if (isUserInfo(item)) return "Edit User" | ||
return "Edit Panel" | ||
} | ||
|
||
const renderModalBody = (item: itemState, mutator: (item: itemState) => void): JSX.Element => { | ||
if (!item) return <></> | ||
if (isUserInfo(item)) { | ||
const user = item as UserInfo | ||
return ( | ||
<Grid container justifyContent='space-between' flexDirection='column' gap={2}> | ||
<TextField label="Name" value={item.name} onChange={e => mutator({ ...item, name: e.target.value })} /> | ||
<Select value={user.is_active ? "true" : "false"} onChange={(_, val) => mutator({ ...item, is_active: (val as string) == "true" })}> | ||
<Option key="true" value="true">Active</Option> | ||
<Option key="false" value="false">Not Active</Option> | ||
</Select> | ||
<Select value={user.role} onChange={(_, val) => mutator({ ...item, role: Number(val) })}> | ||
<Option key={2} value={2}>User</Option> | ||
<Option key={99} value={99}>Admin</Option> | ||
</Select> | ||
</Grid> | ||
) | ||
} | ||
|
||
const actuator = item as Actuator | ||
return ( | ||
<Grid container flexDirection='column' gap={2}> | ||
<TextField label="Name" value={actuator.name} onChange={e => mutator({ ...item, name: e.target.value })} /> | ||
<Grid container justifyContent='space-between' direction='row' > | ||
<TextField label="Pin Number" value={actuator.pin_number} onChange={e => mutator({ ...item, pin_number: Number(e.target.value) })} /> | ||
<TextField label="Terminal Number" value={actuator.terminal_number} onChange={e => mutator({ ...item, terminal_number: Number(e.target.value) })} /> | ||
</Grid> | ||
</Grid > | ||
) | ||
} | ||
|
||
const Admin = () => { | ||
const queryClient = useQueryClient() | ||
const notification = useNotification() | ||
const usersQuery = useQuery({ | ||
queryKey: [userAPI.QUERY_KEY_GET_USER_LIST], | ||
queryFn: userAPI.getUserList | ||
}) | ||
|
||
const actuatorsQuery = useQuery({ | ||
queryKey: [settingAPI.QUERY_KEY_GET_ACTUATORS], | ||
queryFn: () => settingAPI.getActuators(1) | ||
}) | ||
|
||
const userMutation = useMutation({ | ||
mutationFn: (request: UserInfo) => userAPI.updateUser(request), | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: [userAPI.QUERY_KEY_GET_USER_LIST] }) | ||
notification.fire("User Updated") | ||
resetModal() | ||
} | ||
}) | ||
|
||
const actuatorMutation = useMutation({ | ||
mutationFn: (request: Actuator) => settingAPI.updateActuator(request), | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: [settingAPI.QUERY_KEY_GET_ACTUATORS] }) | ||
notification.fire("Panel Updated") | ||
resetModal() | ||
} | ||
}) | ||
|
||
const [selectedItem, setSelectedItem] = useState<itemState>(null) | ||
const [isModalOpened, setIsModalOpened] = useState(false) | ||
const resetModal = () => { | ||
setSelectedItem(null) | ||
setIsModalOpened(false) | ||
} | ||
|
||
const handleCardClick = (item: Actuator | UserInfo) => { | ||
setSelectedItem(item) | ||
setIsModalOpened(true) | ||
} | ||
|
||
const handleUpdateItem = () => { | ||
if (isUserInfo(selectedItem)) { | ||
userMutation.mutate(selectedItem as UserInfo) | ||
return | ||
} | ||
|
||
actuatorMutation.mutate(selectedItem as Actuator) | ||
} | ||
|
||
return ( | ||
<> | ||
<Box sx={{ px: 2 }} className="fade"> | ||
<Typography sx={{ mt: 4 }} level="h2" fontWeight='500'>Admin Panel</Typography> | ||
<Typography sx={{ mt: 2 }} level="h4">Users</Typography> | ||
<Grid container sx={{ mt: 1 }} flexDirection='column' gap={2}> | ||
{usersQuery.data?.map(user => ( | ||
<Card key={user.user_id} onClick={() => handleCardClick(user)}> | ||
<Box display='flex' justifyContent='space-between'> | ||
<Box> | ||
{user.name} | ||
{user.role == 99 && <Chip sx={{ ml: 2 }} color="danger">Admin</Chip>} | ||
</Box> | ||
<Chip color={user.is_active ? 'success' : 'danger'}>{user.is_active ? "Active" : "Not Active"}</Chip> | ||
</Box> | ||
</Card> | ||
))} | ||
</Grid> | ||
<Typography sx={{ mt: 2 }} level="h4">Panels</Typography> | ||
<Grid container sx={{ mt: 1 }} flexDirection='column' gap={2}> | ||
{actuatorsQuery.data?.map(actuator => ( | ||
<Card key={actuator.id} onClick={() => handleCardClick(actuator)}> | ||
<Box display='flex' justifyContent='space-between'> | ||
{actuator.name} | ||
<Grid container gap={1}> | ||
<Chip color="primary">Pin: {actuator.pin_number}</Chip> | ||
<Chip color="warning">{actuator.terminal_number}</Chip> | ||
</Grid> | ||
</Box> | ||
</Card> | ||
))} | ||
</Grid> | ||
</Box > | ||
<Modal | ||
title={renderModalTitle(selectedItem)} | ||
isOpen={!!isModalOpened} | ||
onClose={() => setIsModalOpened(false)} | ||
buttonActions={[ | ||
{ | ||
label: 'Save', | ||
variant: 'solid', | ||
color: 'primary', | ||
loading: false, | ||
onClick: handleUpdateItem, | ||
}, | ||
{ | ||
label: 'Cancel', | ||
variant: 'outlined', | ||
color: 'danger', | ||
onClick: () => setIsModalOpened(false) | ||
} | ||
]} | ||
>{renderModalBody(selectedItem, setSelectedItem)}</Modal > | ||
</> | ||
) | ||
} | ||
|
||
export const AdminRoute = createLazyRoute('/admin')({ | ||
component: Admin | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.