-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handle overlay editing and creation logic
- Loading branch information
Showing
13 changed files
with
255 additions
and
25 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
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,70 @@ | ||
package server | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/gempir/gempbot/internal/api" | ||
"github.com/gempir/gempbot/internal/store" | ||
"github.com/google/uuid" | ||
"github.com/teris-io/shortid" | ||
) | ||
|
||
func (a *Api) OverlayHandler(w http.ResponseWriter, r *http.Request) { | ||
authResp, _, apiErr := a.authClient.AttemptAuth(r, w) | ||
if apiErr != nil { | ||
return | ||
} | ||
userID := authResp.Data.UserID | ||
|
||
if r.URL.Query().Get("managing") != "" { | ||
userID, apiErr = a.userAdmin.CheckEditor(r, a.userAdmin.GetUserConfig(userID)) | ||
if apiErr != nil { | ||
http.Error(w, apiErr.Error(), apiErr.Status()) | ||
return | ||
} | ||
} | ||
|
||
if r.Method == http.MethodGet { | ||
if r.URL.Query().Get("roomId") != "" { | ||
overlay := a.db.GetOverlayByRoomId(r.URL.Query().Get("roomId")) | ||
api.WriteJson(w, overlay, http.StatusOK) | ||
return | ||
} | ||
|
||
if r.URL.Query().Get("id") != "" { | ||
overlay := a.db.GetOverlay(r.URL.Query().Get("id"), userID) | ||
api.WriteJson(w, overlay, http.StatusOK) | ||
return | ||
} | ||
|
||
overlays := a.db.GetOverlays(userID) | ||
api.WriteJson(w, overlays, http.StatusOK) | ||
} else if r.Method == http.MethodPost { | ||
overlay := store.Overlay{} | ||
overlay.OwnerTwitchID = userID | ||
overlay.ID = shortid.MustGenerate() | ||
// long string so you cant read addressbar easily | ||
var roomID []string | ||
for i := 0; i < 16; i++ { | ||
roomID = append(roomID, uuid.New().String()) | ||
} | ||
overlay.RoomID = strings.Join(roomID, "-") | ||
|
||
err := a.db.SaveOverlay(overlay) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
api.WriteJson(w, overlay, http.StatusCreated) | ||
|
||
} else if r.Method == http.MethodDelete { | ||
if r.URL.Query().Get("id") == "" { | ||
http.Error(w, "missing id", http.StatusBadRequest) | ||
} | ||
|
||
a.db.DeleteOverlay(r.URL.Query().Get("id")) | ||
w.WriteHeader(http.StatusOK) | ||
} | ||
} |
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,45 @@ | ||
package store | ||
|
||
import "gorm.io/gorm/clause" | ||
|
||
type Overlay struct { | ||
ID string `gorm:"primaryKey"` | ||
OwnerTwitchID string `gorm:"index"` | ||
RoomID string `gorm:"index"` | ||
} | ||
|
||
func (db *Database) GetOverlays(userID string) []Overlay { | ||
var overlays []Overlay | ||
|
||
db.Client.Where("owner_twitch_id = ?", userID).Find(&overlays) | ||
|
||
return overlays | ||
} | ||
|
||
func (db *Database) GetOverlay(ID string, userID string) Overlay { | ||
var overlay Overlay | ||
|
||
db.Client.Where("id = ? AND owner_twitch_id = ?", ID, userID).First(&overlay) | ||
|
||
return overlay | ||
} | ||
|
||
func (db *Database) GetOverlayByRoomId(roomID string) Overlay { | ||
var overlay Overlay | ||
|
||
db.Client.Where("room_id = ?", roomID).First(&overlay) | ||
|
||
return overlay | ||
} | ||
|
||
func (db *Database) DeleteOverlay(ID string) { | ||
db.Client.Delete(&Overlay{}, "id = ?", ID) | ||
} | ||
|
||
func (db *Database) SaveOverlay(overlay Overlay) error { | ||
update := db.Client.Clauses(clause.OnConflict{ | ||
UpdateAll: true, | ||
}).Create(&overlay) | ||
|
||
return update.Error | ||
} |
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,15 @@ | ||
const Editor = dynamic(async () => (await import('./Editor')).Editor, { ssr: false }) | ||
import dynamic from "next/dynamic"; | ||
import { useOverlay } from "../../hooks/useOverlays"; | ||
import { useParams } from "next/navigation"; | ||
|
||
export function OverlayEditPage() { | ||
const params = useParams<{ overlayId: string }>(); | ||
const [overlay] = useOverlay(params.overlayId); | ||
|
||
console.log("Joining", overlay?.RoomID); | ||
|
||
return <div className="relative w-full h-[100vh]"> | ||
{overlay?.RoomID && <Editor roomId={overlay.RoomID} />} | ||
</div>; | ||
} |
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,14 +1,29 @@ | ||
import dynamic from "next/dynamic"; | ||
import { useUserConfig } from "../../hooks/useUserConfig"; | ||
const Editor = dynamic(async () => (await import('./Editor')).Editor, { ssr: false }) | ||
import Link from "next/link"; | ||
import { useOverlays } from "../../hooks/useOverlays"; | ||
|
||
export function OverlaysPage() { | ||
const [userCfg, setUserConfig, , loading, errorMessage] = useUserConfig(); | ||
if (!userCfg) { | ||
return null; | ||
} | ||
const [overlays, addOverlay, deleteOverlay, errorMessage, loading] = useOverlays(); | ||
|
||
return <div className="relative w-full h-[100vh]"> | ||
Table with overlays | ||
|
||
return <div className="relative w-full h-[100vh] p-4"> | ||
<div className="p-4 bg-gray-800 rounded shadow max-w-[800px]"> | ||
<button onClick={addOverlay} className="bg-green-700 hover:bg-green-600 p-2 rounded shadow block cursor-pointer">Add Overlay</button> | ||
<div className="mt-5"> | ||
{overlays.map(overlay => <div key={overlay.ID} className="flex items-center justify-between p-4 bg-gray-900"> | ||
<div> | ||
<button className="bg-red-700 hover:bg-red-600 p-2 rounded shadow block cursor-pointer" onClick={() => { | ||
confirm("Are you sure you want to delete this overlay?") && deleteOverlay(overlay.ID) | ||
}}>Delete</button> | ||
</div> | ||
<div>{overlay.ID}</div> | ||
<div> | ||
<Link href={`/overlay/edit/${overlay.ID}`} className="bg-blue-700 hover:bg-blue-600 p-2 rounded shadow block cursor-pointer">Edit</Link> | ||
</div> | ||
<div> | ||
<input type="text" value={`${window?.location?.href}/${overlay.RoomID}`} readOnly className="bg-gray-900" /> | ||
</div> | ||
</div>)} | ||
</div> | ||
</div> | ||
</div>; | ||
} |
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,81 @@ | ||
import { useEffect, useState } from "react"; | ||
import { Method, doFetch } from "../service/doFetch"; | ||
import { useStore } from "../store"; | ||
|
||
type Overlay = { | ||
ID: string; | ||
RoomID: string; | ||
} | ||
|
||
export function useOverlays(): [Overlay[], () => void, (id: string) => void, string | null, boolean] { | ||
const [overlays, setOverlays] = useState<Overlay[]>([]); | ||
const [errorMessage, setErrorMessage] = useState<string | null>(null); | ||
const [loading, setLoading] = useState(false); | ||
const managing = useStore(state => state.managing); | ||
const apiBaseUrl = useStore(state => state.apiBaseUrl); | ||
const scToken = useStore(state => state.scToken); | ||
|
||
const fetchOverlays = () => { | ||
setLoading(true); | ||
const endPoint = "/api/overlay"; | ||
|
||
doFetch({ apiBaseUrl, managing, scToken }, Method.GET, endPoint).then(setOverlays).catch(setErrorMessage).finally(() => setLoading(false)); | ||
} | ||
|
||
useEffect(fetchOverlays, []); | ||
|
||
const addOverlay = () => { | ||
setLoading(true); | ||
const endPoint = "/api/overlay"; | ||
|
||
doFetch({ apiBaseUrl, managing, scToken }, Method.POST, endPoint).then(fetchOverlays).catch(setErrorMessage).finally(() => setLoading(false)); | ||
} | ||
|
||
const deleteOverlay = (id: string) => { | ||
setLoading(true); | ||
const endPoint = "/api/overlay"; | ||
|
||
doFetch({ apiBaseUrl, managing, scToken }, Method.DELETE, endPoint, new URLSearchParams({id})).then(() => setErrorMessage(null)).then(fetchOverlays).catch(setErrorMessage).finally(() => setLoading(false)); | ||
} | ||
|
||
return [overlays, addOverlay, deleteOverlay, errorMessage, loading]; | ||
} | ||
|
||
|
||
export function useOverlay(id: string): [Overlay|null, boolean] { | ||
const [overlay, setOverlay] = useState<Overlay | null>(null); | ||
const [loading, setLoading] = useState(false); | ||
const managing = useStore(state => state.managing); | ||
const apiBaseUrl = useStore(state => state.apiBaseUrl); | ||
const scToken = useStore(state => state.scToken); | ||
|
||
const fetchOverlay = () => { | ||
setLoading(true); | ||
const endPoint = "/api/overlay"; | ||
|
||
doFetch({ apiBaseUrl, managing, scToken }, Method.GET, endPoint, new URLSearchParams({id})).then(setOverlay).finally(() => setLoading(false)); | ||
} | ||
|
||
useEffect(fetchOverlay, [id]); | ||
|
||
return [overlay, loading]; | ||
} | ||
|
||
export function useOverlayByRoomId(roomId: string): [Overlay|null, boolean] { | ||
const [overlay, setOverlay] = useState<Overlay | null>(null); | ||
const [loading, setLoading] = useState(false); | ||
const managing = useStore(state => state.managing); | ||
const apiBaseUrl = useStore(state => state.apiBaseUrl); | ||
const scToken = useStore(state => state.scToken); | ||
|
||
const fetchOverlay = () => { | ||
setLoading(true); | ||
const endPoint = "/api/overlay"; | ||
|
||
doFetch({ apiBaseUrl, managing, scToken }, Method.GET, endPoint, new URLSearchParams({roomId})).then(setOverlay).finally(() => setLoading(false)); | ||
} | ||
|
||
useEffect(fetchOverlay, [roomId]); | ||
|
||
return [overlay, loading]; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,8 @@ | ||
'use client'; | ||
|
||
import dynamic from "next/dynamic"; | ||
import { useParams } from "next/navigation"; | ||
const Editor = dynamic(async () => (await import('../../../components/Overlay/Editor')).Editor, { ssr: false }) | ||
import { OverlayEditPage } from "../../../components/Overlay/OverlayEditPage"; | ||
import { initializeStore } from "../../../service/initializeStore"; | ||
|
||
export default function OverlaysEditPage() { | ||
const params = useParams<{ overlayId: string }>(); | ||
return <OverlayEditPage /> | ||
} | ||
|
||
return <div className="relative w-full h-[100vh]"> | ||
<Editor overlayId={params.overlayId} /> | ||
</div>; | ||
} | ||
export const getServerSideProps = initializeStore; |