import { Button, Input, Modal, ModalDialog, Radio, RadioGroup } from "@mui/joy"; import axios from "axios"; import { useState } from "react"; import { toast } from "react-hot-toast"; import useLoading from "../hooks/useLoading"; import useUserStore from "../stores/v1/user"; import Icon from "./Icon"; interface Props { onClose: () => void; onConfirm?: () => void; } const expirationOptions = [ { label: "1 hour", value: 3600, }, { label: "8 hours", value: 3600 * 8, }, { label: "1 week", value: 3600 * 24 * 7, }, { label: "Life time", value: 3600 * 24 * 365 * 100, }, ]; interface State { description: string; expiration: number; } const CreateAccessTokenDialog: React.FC = (props: Props) => { const { onClose, onConfirm } = props; const currentUser = useUserStore().getCurrentUser(); const [state, setState] = useState({ description: "", expiration: 3600, }); const requestState = useLoading(false); const setPartialState = (partialState: Partial) => { setState({ ...state, ...partialState, }); }; const handleDescriptionInputChange = (e: React.ChangeEvent) => { setPartialState({ description: e.target.value, }); }; const handleRoleInputChange = (e: React.ChangeEvent) => { setPartialState({ expiration: Number(e.target.value), }); }; const handleSaveBtnClick = async () => { if (!state.description) { toast.error("Description is required"); return; } try { await axios.post(`/api/v2/users/${currentUser.id}/access_tokens`, { description: state.description, expiresAt: new Date(Date.now() + state.expiration * 1000), }); if (onConfirm) { onConfirm(); } onClose(); } catch (error: any) { console.error(error); toast.error(error.response.data.message); } }; return (
Create Access Token
Description *
Expiration *
{expirationOptions.map((option) => ( ))}
); }; export default CreateAccessTokenDialog;