Compare commits
2 Commits
feat-persi
...
224-editab
| Author | SHA1 | Date | |
|---|---|---|---|
| d1184bf939 | |||
| a39b526d64 |
67
frontend/src/components/TemplateTextField.tsx
Normal file
67
frontend/src/components/TemplateTextField.tsx
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
import { FC, useState } from 'react'
|
||||||
|
|
||||||
|
import DeleteIcon from '@mui/icons-material/Delete'
|
||||||
|
import EditIcon from '@mui/icons-material/Edit'
|
||||||
|
import {
|
||||||
|
Button,
|
||||||
|
Grid,
|
||||||
|
TextField
|
||||||
|
} from '@mui/material'
|
||||||
|
import { useI18n } from '../hooks/useI18n'
|
||||||
|
import { CustomTemplate } from '../types'
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
template: CustomTemplate
|
||||||
|
onChange: (template: CustomTemplate) => void
|
||||||
|
onDelete: (id: string) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
const TemplateTextField: FC<Props> = ({ template, onChange, onDelete }) => {
|
||||||
|
const { i18n } = useI18n()
|
||||||
|
|
||||||
|
const [editedTemplate, setEditedTemplate] = useState(template)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Grid
|
||||||
|
container
|
||||||
|
spacing={2}
|
||||||
|
justifyContent="center"
|
||||||
|
alignItems="center"
|
||||||
|
key={template.id}
|
||||||
|
sx={{ mt: 1 }}
|
||||||
|
>
|
||||||
|
<Grid item xs={3}>
|
||||||
|
<TextField
|
||||||
|
fullWidth
|
||||||
|
label={i18n.t('templatesEditorNameLabel')}
|
||||||
|
defaultValue={template.name}
|
||||||
|
onChange={(e) => setEditedTemplate({ ...editedTemplate, name: e.target.value })}
|
||||||
|
/>
|
||||||
|
</Grid>
|
||||||
|
<Grid item xs={9}>
|
||||||
|
<TextField
|
||||||
|
fullWidth
|
||||||
|
label={i18n.t('templatesEditorContentLabel')}
|
||||||
|
defaultValue={template.content}
|
||||||
|
onChange={(e) => setEditedTemplate({ ...editedTemplate, content: e.target.value })}
|
||||||
|
InputProps={{
|
||||||
|
endAdornment: <div style={{ display: 'flex', gap: 2 }}>
|
||||||
|
<Button
|
||||||
|
variant='contained'
|
||||||
|
onClick={() => onChange(editedTemplate)}>
|
||||||
|
<EditIcon />
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant='contained'
|
||||||
|
onClick={() => onDelete(editedTemplate.id)}>
|
||||||
|
<DeleteIcon />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default TemplateTextField
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import AddIcon from '@mui/icons-material/Add'
|
import AddIcon from '@mui/icons-material/Add'
|
||||||
import CloseIcon from '@mui/icons-material/Close'
|
import CloseIcon from '@mui/icons-material/Close'
|
||||||
import DeleteIcon from '@mui/icons-material/Delete'
|
import DeleteIcon from '@mui/icons-material/Delete'
|
||||||
|
import EditIcon from '@mui/icons-material/Edit'
|
||||||
import {
|
import {
|
||||||
Alert,
|
Alert,
|
||||||
AppBar,
|
AppBar,
|
||||||
@@ -26,6 +27,7 @@ import { useI18n } from '../hooks/useI18n'
|
|||||||
import { ffetch } from '../lib/httpClient'
|
import { ffetch } from '../lib/httpClient'
|
||||||
import { CustomTemplate } from '../types'
|
import { CustomTemplate } from '../types'
|
||||||
import { useAtomValue } from 'jotai'
|
import { useAtomValue } from 'jotai'
|
||||||
|
import TemplateTextField from './TemplateTextField'
|
||||||
|
|
||||||
const Transition = forwardRef(function Transition(
|
const Transition = forwardRef(function Transition(
|
||||||
props: TransitionProps & {
|
props: TransitionProps & {
|
||||||
@@ -55,11 +57,11 @@ const TemplatesEditor: React.FC<Props> = ({ open, onClose }) => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (open) {
|
if (open) {
|
||||||
getTemplates()
|
fetchTemplates()
|
||||||
}
|
}
|
||||||
}, [open])
|
}, [open])
|
||||||
|
|
||||||
const getTemplates = async () => {
|
const fetchTemplates = async () => {
|
||||||
const task = ffetch<CustomTemplate[]>(`${serverAddr}/api/v1/template/all`)
|
const task = ffetch<CustomTemplate[]>(`${serverAddr}/api/v1/template/all`)
|
||||||
const either = await task()
|
const either = await task()
|
||||||
|
|
||||||
@@ -89,7 +91,7 @@ const TemplatesEditor: React.FC<Props> = ({ open, onClose }) => {
|
|||||||
(l) => pushMessage(l, 'warning'),
|
(l) => pushMessage(l, 'warning'),
|
||||||
() => {
|
() => {
|
||||||
pushMessage('Added template')
|
pushMessage('Added template')
|
||||||
getTemplates()
|
fetchTemplates()
|
||||||
setTemplateName('')
|
setTemplateName('')
|
||||||
setTemplateContent('')
|
setTemplateContent('')
|
||||||
}
|
}
|
||||||
@@ -97,6 +99,26 @@ const TemplatesEditor: React.FC<Props> = ({ open, onClose }) => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const updateTemplate = async (template: CustomTemplate) => {
|
||||||
|
const task = ffetch<CustomTemplate>(`${serverAddr}/api/v1/template`, {
|
||||||
|
method: 'PATCH',
|
||||||
|
body: JSON.stringify(template)
|
||||||
|
})
|
||||||
|
|
||||||
|
const either = await task()
|
||||||
|
|
||||||
|
pipe(
|
||||||
|
either,
|
||||||
|
matchW(
|
||||||
|
(l) => pushMessage(l, 'warning'),
|
||||||
|
(r) => {
|
||||||
|
pushMessage(`Updated template ${r.name}`)
|
||||||
|
fetchTemplates()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const deleteTemplate = async (id: string) => {
|
const deleteTemplate = async (id: string) => {
|
||||||
const task = ffetch<unknown>(`${serverAddr}/api/v1/template/${id}`, {
|
const task = ffetch<unknown>(`${serverAddr}/api/v1/template/${id}`, {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
@@ -110,7 +132,7 @@ const TemplatesEditor: React.FC<Props> = ({ open, onClose }) => {
|
|||||||
(l) => pushMessage(l, 'warning'),
|
(l) => pushMessage(l, 'warning'),
|
||||||
() => {
|
() => {
|
||||||
pushMessage('Deleted template')
|
pushMessage('Deleted template')
|
||||||
getTemplates()
|
fetchTemplates()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -188,38 +210,12 @@ const TemplatesEditor: React.FC<Props> = ({ open, onClose }) => {
|
|||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
{templates.map(template => (
|
{templates.map(template => (
|
||||||
<Grid
|
<TemplateTextField
|
||||||
container
|
|
||||||
spacing={2}
|
|
||||||
justifyContent="center"
|
|
||||||
alignItems="center"
|
|
||||||
key={template.id}
|
key={template.id}
|
||||||
sx={{ mt: 1 }}
|
template={template}
|
||||||
>
|
onChange={updateTemplate}
|
||||||
<Grid item xs={3}>
|
onDelete={deleteTemplate}
|
||||||
<TextField
|
|
||||||
fullWidth
|
|
||||||
label={i18n.t('templatesEditorNameLabel')}
|
|
||||||
value={template.name}
|
|
||||||
/>
|
/>
|
||||||
</Grid>
|
|
||||||
<Grid item xs={9}>
|
|
||||||
<TextField
|
|
||||||
fullWidth
|
|
||||||
label={i18n.t('templatesEditorContentLabel')}
|
|
||||||
value={template.content}
|
|
||||||
InputProps={{
|
|
||||||
endAdornment: <Button
|
|
||||||
variant='contained'
|
|
||||||
onClick={() => {
|
|
||||||
startTransition(() => { deleteTemplate(template.id) })
|
|
||||||
}}>
|
|
||||||
<DeleteIcon />
|
|
||||||
</Button>
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</Grid>
|
|
||||||
</Grid>
|
|
||||||
))}
|
))}
|
||||||
</Paper>
|
</Paper>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ export const useI18n = () => {
|
|||||||
const instance = useAtomValue(i18nBuilderState)
|
const instance = useAtomValue(i18nBuilderState)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
i18n: instance
|
i18n: instance,
|
||||||
|
t: instance.t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -34,6 +34,7 @@ func ApplyRouter(args *ContainerArgs) func(chi.Router) {
|
|||||||
r.Post("/cookies", h.SetCookies())
|
r.Post("/cookies", h.SetCookies())
|
||||||
r.Delete("/cookies", h.DeleteCookies())
|
r.Delete("/cookies", h.DeleteCookies())
|
||||||
r.Post("/template", h.AddTemplate())
|
r.Post("/template", h.AddTemplate())
|
||||||
|
r.Patch("/template", h.UpdateTemplate())
|
||||||
r.Get("/template/all", h.GetTemplates())
|
r.Get("/template/all", h.GetTemplates())
|
||||||
r.Delete("/template/{id}", h.DeleteTemplate())
|
r.Delete("/template/{id}", h.DeleteTemplate())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,9 +34,9 @@ func (h *Handler) Exec() http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode(id)
|
if err := json.NewEncoder(w).Encode(id); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -61,6 +61,7 @@ func (h *Handler) ExecPlaylist() http.HandlerFunc {
|
|||||||
|
|
||||||
if err := json.NewEncoder(w).Encode("ok"); err != nil {
|
if err := json.NewEncoder(w).Encode("ok"); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,13 +76,14 @@ func (h *Handler) ExecLivestream() http.HandlerFunc {
|
|||||||
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
h.service.ExecLivestream(req)
|
h.service.ExecLivestream(req)
|
||||||
|
|
||||||
err := json.NewEncoder(w).Encode("ok")
|
if err := json.NewEncoder(w).Encode("ok"); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -98,9 +100,9 @@ func (h *Handler) Running() http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode(res)
|
if err := json.NewEncoder(w).Encode(res); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -134,21 +136,19 @@ func (h *Handler) SetCookies() http.HandlerFunc {
|
|||||||
|
|
||||||
req := new(internal.SetCookiesRequest)
|
req := new(internal.SetCookiesRequest)
|
||||||
|
|
||||||
err := json.NewDecoder(r.Body).Decode(req)
|
if err := json.NewDecoder(r.Body).Decode(req); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = h.service.SetCookies(r.Context(), req.Cookies)
|
if err := h.service.SetCookies(r.Context(), req.Cookies); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode("ok")
|
if err := json.NewEncoder(w).Encode("ok"); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -157,15 +157,14 @@ func (h *Handler) DeleteCookies() http.HandlerFunc {
|
|||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
err := h.service.SetCookies(r.Context(), "")
|
if err := h.service.SetCookies(r.Context(), ""); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode("ok")
|
if err := json.NewEncoder(w).Encode("ok"); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -178,8 +177,7 @@ func (h *Handler) AddTemplate() http.HandlerFunc {
|
|||||||
|
|
||||||
req := new(internal.CustomTemplate)
|
req := new(internal.CustomTemplate)
|
||||||
|
|
||||||
err := json.NewDecoder(r.Body).Decode(req)
|
if err := json.NewDecoder(r.Body).Decode(req); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -189,15 +187,14 @@ func (h *Handler) AddTemplate() http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = h.service.SaveTemplate(r.Context(), req)
|
if err := h.service.SaveTemplate(r.Context(), req); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode("ok")
|
if err := json.NewEncoder(w).Encode("ok"); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -221,6 +218,33 @@ func (h *Handler) GetTemplates() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Handler) UpdateTemplate() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
req := &internal.CustomTemplate{}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(req); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := h.service.UpdateTemplate(r.Context(), req)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewEncoder(w).Encode(res); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Handler) DeleteTemplate() http.HandlerFunc {
|
func (h *Handler) DeleteTemplate() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
@@ -229,15 +253,14 @@ func (h *Handler) DeleteTemplate() http.HandlerFunc {
|
|||||||
|
|
||||||
id := chi.URLParam(r, "id")
|
id := chi.URLParam(r, "id")
|
||||||
|
|
||||||
err := h.service.DeleteTemplate(r.Context(), id)
|
if err := h.service.DeleteTemplate(r.Context(), id); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode("ok")
|
if err := json.NewEncoder(w).Encode("ok"); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -266,6 +289,7 @@ func (h *Handler) GetVersion() http.HandlerFunc {
|
|||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(res); err != nil {
|
if err := json.NewEncoder(w).Encode(res); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -133,6 +133,22 @@ func (s *Service) GetTemplates(ctx context.Context) (*[]internal.CustomTemplate,
|
|||||||
return &templates, nil
|
return &templates, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Service) UpdateTemplate(ctx context.Context, t *internal.CustomTemplate) (*internal.CustomTemplate, error) {
|
||||||
|
conn, err := s.db.Conn(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
_, err = conn.ExecContext(ctx, "UPDATE templates SET name = ?, content = ? WHERE id = ?", t.Name, t.Content, t.Id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return t, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Service) DeleteTemplate(ctx context.Context, id string) error {
|
func (s *Service) DeleteTemplate(ctx context.Context, id string) error {
|
||||||
conn, err := s.db.Conn(ctx)
|
conn, err := s.db.Conn(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user