Skip to content

feat: redirect to the feed page after creation #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions api/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ func (f feedAPI) Create(c echo.Context) error {
return err
}

if err := f.srv.Create(c.Request().Context(), &req); err != nil {
resp, err := f.srv.Create(c.Request().Context(), &req)
if err != nil {
return err
}

return c.NoContent(http.StatusCreated)
return c.JSON(http.StatusCreated, resp)
}

func (f feedAPI) CheckValidity(c echo.Context) error {
Expand Down
10 changes: 6 additions & 4 deletions frontend/src/lib/api/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ export type FeedCreateForm = {
};

export async function createFeed(data: FeedCreateForm) {
return await api.post('feeds', {
timeout: 20000,
json: data
});
return await api
.post('feeds', {
timeout: 20000,
json: data
})
.json<{ ids: number[] }>();
}

export type FeedUpdateForm = {
Expand Down
16 changes: 11 additions & 5 deletions frontend/src/lib/components/FeedActionImportManually.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { invalidateAll } from '$app/navigation';
import { goto } from '$app/navigation';
import { checkValidity, createFeed, type FeedCreateForm } from '$lib/api/feed';
import { allGroups } from '$lib/api/group';
import type { Group } from '$lib/api/model';
Expand Down Expand Up @@ -59,18 +59,19 @@
}

async function handleContinue() {
loading = true;
if (!form.feeds[0].name) {
form.feeds[0].name = new URL(form.feeds[0].link).hostname;
}
try {
await createFeed(form);
toast.success(t('state.success'));
const resp = await createFeed(form);
doneCallback();
goto('/feeds/' + resp.ids[0], { invalidateAll: true });
toast.success(t('state.success'));
} catch (e) {
formError = (e as Error).message;
}
loading = false;
invalidateAll();
}
</script>

Expand Down Expand Up @@ -158,6 +159,11 @@
</label>
{/each}
</fieldset>
<button type="submit" class="btn btn-primary mt-4 ml-auto">{t('common.confirm')}</button>
<button type="submit" disabled={loading} class="btn btn-primary mt-4 ml-auto">
{#if loading}
<span class="loading loading-spinner loading-sm"></span>
{/if}
{t('common.confirm')}
</button>
</form>
{/if}
23 changes: 15 additions & 8 deletions server/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (f Feed) Get(ctx context.Context, req *ReqFeedGet) (*RespFeedGet, error) {
}, nil
}

func (f Feed) Create(ctx context.Context, req *ReqFeedCreate) error {
func (f Feed) Create(ctx context.Context, req *ReqFeedCreate) (*RespFeedCreate, error) {
feeds := make([]*model.Feed, 0, len(req.Feeds))
for _, r := range req.Feeds {
feeds = append(feeds, &model.Feed{
Expand All @@ -91,16 +91,23 @@ func (f Feed) Create(ctx context.Context, req *ReqFeedCreate) error {
GroupID: req.GroupID,
})
}
if len(feeds) == 0 {
return nil
}

if err := f.repo.Create(feeds); err != nil {
return err
return nil, err
}

// GORM assigns the ID to the model after Create
ids := make([]uint, 0, len(feeds))
for _, v := range feeds {
ids = append(ids, v.ID)
}

resp := &RespFeedCreate{
IDs: ids,
}

puller := pull.NewPuller(repo.NewFeed(repo.DB), repo.NewItem(repo.DB))
if len(feeds) >= 1 {
if len(feeds) > 1 {
Copy link
Preview

Copilot AI Apr 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the condition for triggering asynchronous processing is intentional. If a single feed creation should be processed synchronously with a pull, confirm that using 'if len(feeds) > 1' is the correct logic for differentiating the two cases.

Copilot uses AI. Check for mistakes.

go func() {
routinePool := make(chan struct{}, 10)
defer close(routinePool)
Expand All @@ -118,9 +125,9 @@ func (f Feed) Create(ctx context.Context, req *ReqFeedCreate) error {
}
wg.Wait()
}()
return nil
return resp, nil
}
return puller.PullOne(ctx, feeds[0].ID)
return resp, puller.PullOne(ctx, feeds[0].ID)
Copy link
Preview

Copilot AI Apr 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning the result of puller.PullOne along with the response may lead to unintended error propagation. Consider handling the error from puller.PullOne separately to ensure a clear separation between a successful creation response and asynchronous processing errors.

Suggested change
return resp, puller.PullOne(ctx, feeds[0].ID)
go func() {
// NOTE: do not use the incoming ctx, as it will be Done() automatically
// by API timeout middleware
puller.PullOne(context.Background(), feeds[0].ID)
}()
return resp, nil

Copilot uses AI. Check for mistakes.

}

func (f Feed) CheckValidity(ctx context.Context, req *ReqFeedCheckValidity) (*RespFeedCheckValidity, error) {
Expand Down
4 changes: 4 additions & 0 deletions server/feed_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ type ReqFeedCreate struct {
GroupID uint `json:"group_id" validate:"required"`
}

type RespFeedCreate struct {
IDs []uint `json:"ids"`
}

type ReqFeedUpdate struct {
ID uint `param:"id" validate:"required"`
Name *string `json:"name"`
Expand Down