1
0
Fork 0
forked from 0x2E/fusion

feat: redirect to the feed page after creation (#155)

This commit is contained in:
Yuan 2025-04-26 16:59:15 +08:00 committed by GitHub
parent 5f527b57a7
commit e0938da00b
Signed by: github
GPG key ID: B5690EEEBB952194
5 changed files with 39 additions and 19 deletions

View file

@ -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 {

View file

@ -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 = {

View file

@ -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';
@ -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>
@ -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}

View file

@ -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{
@ -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 {
go func() {
routinePool := make(chan struct{}, 10)
defer close(routinePool)
@ -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)
}
func (f Feed) CheckValidity(ctx context.Context, req *ReqFeedCheckValidity) (*RespFeedCheckValidity, error) {

View file

@ -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"`