Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/components/licenses/editable-license.tsx
Views: 687
import { Alert, Input } from "antd";1import { useEffect, useState } from "react";2import apiPost from "lib/api/post";3import { capitalize } from "@cocalc/util/misc";45export function EditableTitle({6license_id,7title,8onChange,9}: {10license_id: string;11title: string;12onChange?: () => void;13}) {14return (15<EditableTextField16license_id={license_id}17field="title"18value={title}19onChange={onChange}20/>21);22}2324export function EditableDescription({25license_id,26description,27onChange,28}: {29license_id: string;30description: string;31onChange?: () => void;32}) {33return (34<EditableTextField35license_id={license_id}36field="description"37value={description}38rows={3}39onChange={onChange}40/>41);42}4344function EditableTextField({45license_id,46field,47value,48rows,49onChange,50}: {51license_id: string;52field: "title" | "description";53value?: string;54rows?: number;55onChange?: () => void;56}) {57const [edit, setEdit] = useState<boolean>(false);58const [value2, setValue] = useState<string>(value ?? "");59const [error, setError] = useState<string>("");6061useEffect(() => {62setValue(value ?? "");63setEdit(false);64setError("");65}, [value]);6667async function save(value: string): Promise<void> {68setEdit(false);69setError("");70const query = { manager_site_licenses: { id: license_id, [field]: value } };71try {72await apiPost("/user-query", { query });73onChange?.();74} catch (err) {75setError(err.message);76}77}7879return (80<div style={{ cursor: "pointer" }} onClick={() => setEdit(true)}>81{error && (82<Alert type="error" message={`Error saving ${field} - ${error}`} />83)}84{capitalize(field)}:{" "}85{edit &&86(rows ? (87<Input.TextArea88autoFocus89value={value2}90onChange={(e) => setValue(e.target.value)}91onBlur={() => save(value2)}92rows={rows}93/>94) : (95<Input96autoFocus97value={value2}98onChange={(e) => setValue(e.target.value)}99onBlur={() => save(value2)}100onPressEnter={() => save(value2)}101/>102))}103{!edit && <>{value2.trim() ? value2 : `(set ${field}...)`}</>}104</div>105);106}107108109