Skip to content

Commit

Permalink
server/ui: associate user timing profile
Browse files Browse the repository at this point in the history
  • Loading branch information
NextFire committed Aug 31, 2024
1 parent d495c76 commit db8517f
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 3 deletions.
2 changes: 1 addition & 1 deletion server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func getUserScopesFromJwt(ctx context.Context, token string) (*User, *Scopes, er

db := GetDB(ctx)
user := User{ID: sub}
if err := db.First(&user).Error; err != nil {
if err := db.Preload(clause.Associations).First(&user).Error; err != nil {
return nil, nil, err
}

Expand Down
3 changes: 3 additions & 0 deletions server/karaberus.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func addRoutes(api huma.API) {
huma.Get(api, "/api/token", GetAllUserTokens, setSecurity(oidc_security))
huma.Post(api, "/api/token", CreateToken, setSecurity(oidc_security))
huma.Delete(api, "/api/token/{token}", DeleteToken, setSecurity(oidc_security))

huma.Get(api, "/api/me", GetMe, setSecurity(oidc_security))
huma.Put(api, "/api/me/author", UpdateMeAuthor, setSecurity(oidc_security))
}

func setSecurity(security []map[string][]string) func(o *huma.Operation) {
Expand Down
32 changes: 32 additions & 0 deletions server/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,35 @@ func getOrCreateUser(ctx context.Context, sub string, info *oidc.UserInfo) (*Use
}
return &user, nil
}

type GetMeOutput struct {
Body User
}

func GetMe(ctx context.Context, input *struct{}) (*GetMeOutput, error) {
user := getCurrentUser(ctx)
return &GetMeOutput{Body: *user}, nil
}

type UpdateMeAuthorInput struct {
Body struct {
Id *uint `json:"id"`
}
}

type UpdateMeAuthorOutput struct {
Status int
}

func UpdateMeAuthor(ctx context.Context, input *UpdateMeAuthorInput) (*UpdateMeAuthorOutput, error) {
tx := GetDB(ctx)
user := getCurrentUser(ctx)
if input.Body.Id != nil {
if _, err := GetAuthorById(tx, *input.Body.Id); err != nil {
return nil, DBErrToHumaErr(err)
}
}
user.TimingProfileID = input.Body.Id
err := tx.Model(&user).Select("timing_profile_id").Updates(&user).Error
return &UpdateMeAuthorOutput{Status: 204}, DBErrToHumaErr(err)
}
5 changes: 4 additions & 1 deletion ui/src/components/KaraEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default function KaraEditor(props: {
kara?: components["schemas"]["KaraInfoDB"];
onSubmit: (kara: components["schemas"]["KaraInfo"]) => void;
reset?: boolean;
me?: components["schemas"]["User"];
}) {
const { getModalRef, setModal, showToast } = useContext(Context)!;

Expand Down Expand Up @@ -53,13 +54,15 @@ export default function KaraEditor(props: {
//#endregion

//#region Signals
const meTimingProfile = props.me?.TimingProfile;

const [getTitle, setTitle] = createSignal(props.kara?.Title ?? "");
const [getExtraTitles, setExtraTitles] = createSignal(
props.kara?.ExtraTitles?.map((v) => v.Name).join("\n") ?? "",
);
const [getAuthors, setAuthors] = createSignal<
components["schemas"]["TimingAuthor"][]
>(props.kara?.Authors ?? []);
>(props.kara?.Authors ?? (meTimingProfile ? [meTimingProfile] : []));
const [getArtists, setArtists] = createSignal<
components["schemas"]["Artist"][]
>(props.kara?.Artists ?? []);
Expand Down
89 changes: 89 additions & 0 deletions ui/src/components/ProfileEditor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {
createResource,
createSignal,
Show,
useContext,
type JSX,
} from "solid-js";
import type { components } from "../utils/karaberus";
import { karaberus } from "../utils/karaberus-client";
import AuthorEditor from "./AuthorEditor";
import Autocomplete from "./Autocomplete";
import { Context } from "./Context";

export default function ProfileEditor(props: {
user: components["schemas"]["User"];
onSubmit: (data: { author?: components["schemas"]["TimingAuthor"] }) => void;
reset?: boolean;
}) {
const { getModalRef, setModal, showToast } = useContext(Context)!;

const [getAllAuthors, { refetch: refetchAuthors }] = createResource(
async () => {
const resp = await karaberus.GET("/api/tags/author");
return resp.data;
},
);

const [getAuthor, setAuthor] = createSignal<
components["schemas"]["TimingAuthor"] | undefined
>(props.user.TimingProfile);

const openAddAuthorModal: JSX.EventHandler<HTMLElement, MouseEvent> = (e) => {
e.preventDefault();
setModal(<AuthorEditor onSubmit={postAuthor} />);
getModalRef().showModal();
};

const postAuthor = async (author: components["schemas"]["AuthorInfo"]) => {
const resp = await karaberus.POST("/api/tags/author", { body: author });
if (resp.error) {
alert(resp.error.detail);
return;
}
showToast("Author added!");
refetchAuthors();
getModalRef().close();
};

const onsubmit: JSX.EventHandler<HTMLElement, SubmitEvent> = (e) => {
e.preventDefault();
props.onSubmit({
author: getAuthor(),
});
if (props.reset) {
(e.target as HTMLFormElement).reset();
}
};

return (
<form onsubmit={onsubmit} class="flex flex-col gap-y-2 w-full">
<label>
<div class="label">
<span class="label-text">Timing Author</span>
<span class="label-text-alt">
<a class="link" onclick={openAddAuthorModal}>
Can't find it?
</a>
</span>
</div>
<Show
when={getAllAuthors()}
fallback={<span class="loading loading-spinner loading-lg" />}
>
{(getAllAuthors) => (
<Autocomplete
items={getAllAuthors()}
getItemName={(author) => author.Name}
getState={getAuthor}
setState={setAuthor}
placeholder="bebou69"
/>
)}
</Show>
</label>

<input type="submit" class="btn btn-primary" />
</form>
);
}
13 changes: 12 additions & 1 deletion ui/src/pages/karaoke/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ import { useNavigate } from "@solidjs/router";
import KaraEditor from "../../components/KaraEditor";
import type { components } from "../../utils/karaberus";
import { karaberus } from "../../utils/karaberus-client";
import { createResource, Show } from "solid-js";

export default function KaraokeNew() {
const navigate = useNavigate();

const [getMe] = createResource(async () => {
const resp = await karaberus.GET("/api/me");
return resp.data;
});

const onSubmit = async (info: components["schemas"]["KaraInfo"]) => {
const resp = await karaberus.POST("/api/kara", {
body: info,
Expand All @@ -23,7 +29,12 @@ export default function KaraokeNew() {
<>
<h1 class="header">New Karaoke</h1>

<KaraEditor onSubmit={onSubmit} />
<Show
when={getMe()}
fallback={<span class="loading loading-spinner loading-lg" />}
>
{(getMe) => <KaraEditor onSubmit={onSubmit} me={getMe()} />}
</Show>
</>
);
}
39 changes: 39 additions & 0 deletions ui/src/pages/profile.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
import { createResource, Show, useContext } from "solid-js";
import { Context } from "../components/context";
import ProfileEditor from "../components/ProfileEditor";
import type { components } from "../utils/karaberus";
import { karaberus } from "../utils/karaberus-client";
import { getSessionInfos } from "../utils/session";

export default function Profile() {
const { showToast } = useContext(Context)!;

const [getMe, { refetch: refetchMe }] = createResource(async () => {
const resp = await karaberus.GET("/api/me");
return resp.data;
});

const infos = getSessionInfos();

const onSubmit = async (data: {
author?: components["schemas"]["TimingAuthor"];
}) => {
const resp = await karaberus.PUT("/api/me/author", {
body: {
id: data.author?.ID || null,
},
});
if (resp.error) {
alert(resp.error.detail);
return;
}
showToast("Your profile is updated!");
refetchMe();
};

return (
<>
<h1 class="header">Profile</h1>

<Show
when={getMe()}
fallback={<span class="loading loading-spinner loading-lg" />}
>
{(getMe) => <ProfileEditor user={getMe()} onSubmit={onSubmit} />}
</Show>

<h2 class="text-2xl font-semibold mt-4">User</h2>
<pre>{JSON.stringify(getMe(), undefined, 2)}</pre>

<h2 class="text-2xl font-semibold mt-4">JWT</h2>
<pre>{JSON.stringify(infos, undefined, 2)}</pre>
</>
);
Expand Down

0 comments on commit db8517f

Please sign in to comment.