Skip to content

Commit

Permalink
finish a couple more edge cases with image uploads and draft retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
Southclaws committed Jul 22, 2023
1 parent f0add10 commit 79847c5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
6 changes: 5 additions & 1 deletion app/resources/thread/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func (d *database) Update(ctx context.Context, id post.PostID, opts ...Option) (
WithAuthor().
WithCategory().
WithTags().
WithAssets().
Only(ctx)
if err != nil {
return nil, fault.Wrap(err, fctx.With(ctx), ftag.With(ftag.Internal))
Expand Down Expand Up @@ -186,7 +187,8 @@ func (d *database) List(
Where(filters...).
Limit(max).
WithCategory().
WithAuthor()
WithAuthor().
WithAssets()

for _, fn := range opts {
fn(query)
Expand Down Expand Up @@ -222,12 +224,14 @@ func (d *database) Get(ctx context.Context, threadID post.PostID) (*Thread, erro
}).
WithReacts().
WithAuthor().
WithAssets().
Order(ent.Asc(post_model.FieldCreatedAt))
}).
WithAuthor().
WithCategory().
WithTags().
WithReacts().
WithAssets().
Only(ctx)
if err != nil {
if ent.IsNotFound(err) {
Expand Down
1 change: 1 addition & 0 deletions app/transports/openapi/bindings/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func serialiseThreadReference(t *thread.Thread) openapi.ThreadReference {
PostCount: utils.Ref(len(t.Posts)),
Reacts: reacts(t.Reacts),
Tags: t.Tags,
Assets: dt.Map(t.Assets, serialiseAssetReference),
}
}

Expand Down
28 changes: 26 additions & 2 deletions web/src/screens/compose/components/ComposeForm/useComposeForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { useForm } from "react-hook-form";
import { z } from "zod";

import { useCategoryList } from "src/api/openapi/categories";
import { Asset, Thread, ThreadStatus } from "src/api/openapi/schemas";
import {
Asset,
Thread,
ThreadInitialProps,
ThreadStatus,
} from "src/api/openapi/schemas";
import { threadCreate, threadUpdate } from "src/api/openapi/threads";
import { errorToast } from "src/components/ErrorBanner";

Expand Down Expand Up @@ -42,8 +47,13 @@ export function useComposeForm({ initialDraft, editing }: Props) {
});

const doSave = async (data: FormShape) => {
const payload = {
const payload: ThreadInitialProps = {
...data,

// When saving a new draft, these are optional but must be explicitly set.
title: data.title ?? "",
body: data.body ?? "",

status: ThreadStatus.draft,
};

Expand Down Expand Up @@ -85,7 +95,20 @@ export function useComposeForm({ initialDraft, editing }: Props) {
};

await doSave(newState).catch(errorToast(toast));
formContext.setValue("assets", newAssets);
};

const onAssetDelete = async (asset: Asset) => {
const state: FormShape = formContext.getValues();

const newAssets = state.assets.filter((a) => a !== asset.id);

const newState = {
...state,
assets: newAssets,
};

await doSave(newState).catch(errorToast(toast));
formContext.setValue("assets", newAssets);
};

Expand All @@ -106,6 +129,7 @@ export function useComposeForm({ initialDraft, editing }: Props) {
onSave,
onPublish,
onAssetUpload,
onAssetDelete,
formContext,
};
}

0 comments on commit 79847c5

Please sign in to comment.