1
0
Fork 1
mirror of https://github.com/0x2E/fusion.git synced 2025-06-08 05:27:15 +09:00

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 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 err
} }
return c.NoContent(http.StatusCreated) return c.JSON(http.StatusCreated, resp)
} }
func (f feedAPI) CheckValidity(c echo.Context) error { func (f feedAPI) CheckValidity(c echo.Context) error {

View file

@ -47,10 +47,12 @@ export type FeedCreateForm = {
}; };
export async function createFeed(data: FeedCreateForm) { export async function createFeed(data: FeedCreateForm) {
return await api.post('feeds', { return await api
timeout: 20000, .post('feeds', {
json: data timeout: 20000,
}); json: data
})
.json<{ ids: number[] }>();
} }
export type FeedUpdateForm = { export type FeedUpdateForm = {

View file

@ -1,5 +1,5 @@
<script lang="ts"> <script lang="ts">
import { invalidateAll } from '$app/navigation'; import { goto } from '$app/navigation';
import { checkValidity, createFeed, type FeedCreateForm } from '$lib/api/feed'; import { checkValidity, createFeed, type FeedCreateForm } from '$lib/api/feed';
import { allGroups } from '$lib/api/group'; import { allGroups } from '$lib/api/group';
import type { Group } from '$lib/api/model'; import type { Group } from '$lib/api/model';
@ -59,18 +59,19 @@
} }
async function handleContinue() { async function handleContinue() {
loading = true;
if (!form.feeds[0].name) { if (!form.feeds[0].name) {
form.feeds[0].name = new URL(form.feeds[0].link).hostname; form.feeds[0].name = new URL(form.feeds[0].link).hostname;
} }
try { try {
await createFeed(form); const resp = await createFeed(form);
toast.success(t('state.success'));
doneCallback(); doneCallback();
goto('/feeds/' + resp.ids[0], { invalidateAll: true });
toast.success(t('state.success'));
} catch (e) { } catch (e) {
formError = (e as Error).message; formError = (e as Error).message;
} }
loading = false; loading = false;
invalidateAll();
} }
</script> </script>
@ -158,6 +159,11 @@
</label> </label>
{/each} {/each}
</fieldset> </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> </form>
{/if} {/if}

View file

@ -79,7 +79,7 @@ func (f Feed) Get(ctx context.Context, req *ReqFeedGet) (*RespFeedGet, error) {
}, nil }, 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)) feeds := make([]*model.Feed, 0, len(req.Feeds))
for _, r := range req.Feeds { for _, r := range req.Feeds {
feeds = append(feeds, &model.Feed{ feeds = append(feeds, &model.Feed{
@ -91,16 +91,23 @@ func (f Feed) Create(ctx context.Context, req *ReqFeedCreate) error {
GroupID: req.GroupID, GroupID: req.GroupID,
}) })
} }
if len(feeds) == 0 {
return nil
}
if err := f.repo.Create(feeds); err != 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)) puller := pull.NewPuller(repo.NewFeed(repo.DB), repo.NewItem(repo.DB))
if len(feeds) >= 1 { if len(feeds) > 1 {
go func() { go func() {
routinePool := make(chan struct{}, 10) routinePool := make(chan struct{}, 10)
defer close(routinePool) defer close(routinePool)
@ -118,9 +125,9 @@ func (f Feed) Create(ctx context.Context, req *ReqFeedCreate) error {
} }
wg.Wait() 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) { 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"` GroupID uint `json:"group_id" validate:"required"`
} }
type RespFeedCreate struct {
IDs []uint `json:"ids"`
}
type ReqFeedUpdate struct { type ReqFeedUpdate struct {
ID uint `param:"id" validate:"required"` ID uint `param:"id" validate:"required"`
Name *string `json:"name"` Name *string `json:"name"`