Skip to content

Commit e0938da

Browse files
authored
feat: redirect to the feed page after creation (#155)
1 parent 5f527b5 commit e0938da

File tree

5 files changed

+39
-19
lines changed

5 files changed

+39
-19
lines changed

api/feed.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ func (f feedAPI) Create(c echo.Context) error {
5252
return err
5353
}
5454

55-
if err := f.srv.Create(c.Request().Context(), &req); err != nil {
55+
resp, err := f.srv.Create(c.Request().Context(), &req)
56+
if err != nil {
5657
return err
5758
}
5859

59-
return c.NoContent(http.StatusCreated)
60+
return c.JSON(http.StatusCreated, resp)
6061
}
6162

6263
func (f feedAPI) CheckValidity(c echo.Context) error {

frontend/src/lib/api/feed.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ export type FeedCreateForm = {
4747
};
4848

4949
export async function createFeed(data: FeedCreateForm) {
50-
return await api.post('feeds', {
51-
timeout: 20000,
52-
json: data
53-
});
50+
return await api
51+
.post('feeds', {
52+
timeout: 20000,
53+
json: data
54+
})
55+
.json<{ ids: number[] }>();
5456
}
5557

5658
export type FeedUpdateForm = {

frontend/src/lib/components/FeedActionImportManually.svelte

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { invalidateAll } from '$app/navigation';
2+
import { goto } from '$app/navigation';
33
import { checkValidity, createFeed, type FeedCreateForm } from '$lib/api/feed';
44
import { allGroups } from '$lib/api/group';
55
import type { Group } from '$lib/api/model';
@@ -59,18 +59,19 @@
5959
}
6060
6161
async function handleContinue() {
62+
loading = true;
6263
if (!form.feeds[0].name) {
6364
form.feeds[0].name = new URL(form.feeds[0].link).hostname;
6465
}
6566
try {
66-
await createFeed(form);
67-
toast.success(t('state.success'));
67+
const resp = await createFeed(form);
6868
doneCallback();
69+
goto('/feeds/' + resp.ids[0], { invalidateAll: true });
70+
toast.success(t('state.success'));
6971
} catch (e) {
7072
formError = (e as Error).message;
7173
}
7274
loading = false;
73-
invalidateAll();
7475
}
7576
</script>
7677

@@ -158,6 +159,11 @@
158159
</label>
159160
{/each}
160161
</fieldset>
161-
<button type="submit" class="btn btn-primary mt-4 ml-auto">{t('common.confirm')}</button>
162+
<button type="submit" disabled={loading} class="btn btn-primary mt-4 ml-auto">
163+
{#if loading}
164+
<span class="loading loading-spinner loading-sm"></span>
165+
{/if}
166+
{t('common.confirm')}
167+
</button>
162168
</form>
163169
{/if}

server/feed.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (f Feed) Get(ctx context.Context, req *ReqFeedGet) (*RespFeedGet, error) {
7979
}, nil
8080
}
8181

82-
func (f Feed) Create(ctx context.Context, req *ReqFeedCreate) error {
82+
func (f Feed) Create(ctx context.Context, req *ReqFeedCreate) (*RespFeedCreate, error) {
8383
feeds := make([]*model.Feed, 0, len(req.Feeds))
8484
for _, r := range req.Feeds {
8585
feeds = append(feeds, &model.Feed{
@@ -91,16 +91,23 @@ func (f Feed) Create(ctx context.Context, req *ReqFeedCreate) error {
9191
GroupID: req.GroupID,
9292
})
9393
}
94-
if len(feeds) == 0 {
95-
return nil
96-
}
9794

9895
if err := f.repo.Create(feeds); err != nil {
99-
return err
96+
return nil, err
97+
}
98+
99+
// GORM assigns the ID to the model after Create
100+
ids := make([]uint, 0, len(feeds))
101+
for _, v := range feeds {
102+
ids = append(ids, v.ID)
103+
}
104+
105+
resp := &RespFeedCreate{
106+
IDs: ids,
100107
}
101108

102109
puller := pull.NewPuller(repo.NewFeed(repo.DB), repo.NewItem(repo.DB))
103-
if len(feeds) >= 1 {
110+
if len(feeds) > 1 {
104111
go func() {
105112
routinePool := make(chan struct{}, 10)
106113
defer close(routinePool)
@@ -118,9 +125,9 @@ func (f Feed) Create(ctx context.Context, req *ReqFeedCreate) error {
118125
}
119126
wg.Wait()
120127
}()
121-
return nil
128+
return resp, nil
122129
}
123-
return puller.PullOne(ctx, feeds[0].ID)
130+
return resp, puller.PullOne(ctx, feeds[0].ID)
124131
}
125132

126133
func (f Feed) CheckValidity(ctx context.Context, req *ReqFeedCheckValidity) (*RespFeedCheckValidity, error) {

server/feed_form.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ type ReqFeedCreate struct {
5656
GroupID uint `json:"group_id" validate:"required"`
5757
}
5858

59+
type RespFeedCreate struct {
60+
IDs []uint `json:"ids"`
61+
}
62+
5963
type ReqFeedUpdate struct {
6064
ID uint `param:"id" validate:"required"`
6165
Name *string `json:"name"`

0 commit comments

Comments
 (0)