Skip to content

Change form actions to fetch for submit review box #25219

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 33 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f774943
save
HesterG Jun 12, 2023
3c56833
change to fetch
HesterG Jun 12, 2023
984a30b
update
HesterG Jun 12, 2023
2b71734
change to post
HesterG Jun 12, 2023
7d3683b
Merge branch 'go-gitea:main' into change-actions-fetch
HesterG Jun 12, 2023
2318bb3
change redirect
HesterG Jun 12, 2023
183dc8e
update
HesterG Jun 12, 2023
ed54590
add loading
HesterG Jun 12, 2023
8f57fc1
add comments
HesterG Jun 12, 2023
0dc11e1
update js
HesterG Jun 12, 2023
4602642
update
HesterG Jun 12, 2023
7fa9e1e
Merge branch 'go-gitea:main' into change-actions-fetch
HesterG Jun 13, 2023
9db8855
comment
HesterG Jun 13, 2023
acdd3cd
comment
HesterG Jun 13, 2023
f029cfb
change to resirect
HesterG Jun 13, 2023
8f382f2
Merge branch 'main' into change-actions-fetch
HesterG Jun 13, 2023
cf540cd
Merge branch 'main' into change-actions-fetch
HesterG Jun 13, 2023
bfe1c6b
update to dataform
HesterG Jun 13, 2023
1a8472f
add method
HesterG Jun 13, 2023
c3d5bfb
Merge branch 'main' into change-actions-fetch
HesterG Jun 13, 2023
2d7d5d0
rename and append submitter value
HesterG Jun 13, 2023
f4da606
comment
HesterG Jun 13, 2023
b37a7db
comment
HesterG Jun 13, 2023
74e330d
fix lint
HesterG Jun 13, 2023
a5c5145
Merge branch 'main' into change-actions-fetch
HesterG Jun 13, 2023
c45a1e1
fine tune (wip)
wxiaoguang Jun 13, 2023
3b271d7
quick fix for submitterName and value
HesterG Jun 13, 2023
6023a45
Merge branch 'main' into change-actions-fetch
wxiaoguang Jun 13, 2023
b8f64ca
improve
wxiaoguang Jun 13, 2023
722d0ed
improve
wxiaoguang Jun 14, 2023
9f428a3
improve
wxiaoguang Jun 14, 2023
f43cdc6
Merge branch 'main' into change-actions-fetch
HesterG Jun 14, 2023
713feda
Merge branch 'main' into change-actions-fetch
GiteaBot Jun 14, 2023
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
4 changes: 4 additions & 0 deletions modules/context/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ func (b *Base) JSON(status int, content interface{}) {
}
}

func (b *Base) JSONRedirect(redirect string) {
b.JSON(http.StatusOK, map[string]any{"redirect": redirect})
}

// RemoteAddr returns the client machine ip address
func (b *Base) RemoteAddr() string {
return b.Req.RemoteAddr
Expand Down
9 changes: 4 additions & 5 deletions routers/web/repo/pull_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func SubmitReview(ctx *context.Context) {
}
if ctx.HasError() {
ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
ctx.JSONRedirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
return
}

Expand All @@ -214,7 +214,7 @@ func SubmitReview(ctx *context.Context) {
}

ctx.Flash.Error(translated)
ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
ctx.JSONRedirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
return
}
}
Expand All @@ -228,14 +228,13 @@ func SubmitReview(ctx *context.Context) {
if err != nil {
if issues_model.IsContentEmptyErr(err) {
ctx.Flash.Error(ctx.Tr("repo.issues.review.content.empty"))
ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
ctx.JSONRedirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
} else {
ctx.ServerError("SubmitReview", err)
}
return
}

ctx.Redirect(fmt.Sprintf("%s/pulls/%d#%s", ctx.Repo.RepoLink, issue.Index, comm.HashTag()))
ctx.JSONRedirect(fmt.Sprintf("%s/pulls/%d#%s", ctx.Repo.RepoLink, issue.Index, comm.HashTag()))
}

// DismissReview dismissing stale review by repo admin
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/diff/new_review.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</button>
<div class="review-box-panel tippy-target">
<div class="ui segment">
<form class="ui form" action="{{.Link}}/reviews/submit" method="post">
<form class="ui form form-fetch-action" action="{{.Link}}/reviews/submit" method="post">
{{.CsrfTokenHtml}}
<input type="hidden" name="commit_id" value="{{.AfterCommitID}}">
<div class="field gt-df gt-ac">
Expand Down
53 changes: 53 additions & 0 deletions web_src/js/features/common-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,57 @@ export function initGlobalButtonClickOnEnter() {
});
}

async function fetchFormAction(e) {
if (!e.target.classList.contains('form-fetch-action')) return;

e.preventDefault();
const formEl = e.target;
if (formEl.classList.contains('loading')) return;

// TODO: fine tune the UI feedback
formEl.classList.add('loading');
e.submitter?.classList.add('loading');

const formMethod = formEl.getAttribute('method') || 'get';
const formActionUrl = formEl.getAttribute('action');
const formData = new FormData(formEl);
const [submitterName, submitterValue] = [e.submitter?.getAttribute('name'), e.submitter?.getAttribute('value')];
if (submitterName) {
formData.append(submitterName, submitterValue);
}

const req = {method: formMethod.toUpperCase(), headers: {'X-Csrf-Token': csrfToken}};
if (formMethod === 'GET') {
// TODO: append to the action URL
} else {
req.body = formData;
}

const onError = () => {
// TODO: show error to end users
formEl.classList.remove('loading');
e.submitter?.classList.remove('loading');
};

try {
const resp = await fetch(formActionUrl, req);
if (resp.status === 200) {
const {redirect} = await resp.json();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const resp = await fetch(formActionUrl, req);
if (resp.status === 200) {
const {redirect} = await resp.json();
const res = await fetch(formActionUrl, req);
if (res.ok) {
const {redirect} = await res.json();

Copy link
Contributor

@wxiaoguang wxiaoguang Jun 14, 2023

Choose a reason for hiding this comment

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

If it is only a nit, I do not want to use res, which reads like result, I really don't like it and never used it.

If it is a must and most people prefer res than resp, please help to add a lint rule or write a guideline document, to avoid suggesting it again and again ....

Copy link
Member

Choose a reason for hiding this comment

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

My order of preference is res > response > resp.

Copy link
Contributor

Choose a reason for hiding this comment

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

My order of preference is resp > response , no res because I only use res for "results".

And all Golang backend code also uses resp/Resp.

Copy link
Member

Choose a reason for hiding this comment

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

I guess at some point, I will at some point make a fetch wrapper anyways that returns data, which will make this discussion obsolete.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am curious is $.post or $.ajax more preferred than fetch in gitea?

Copy link
Member

@silverwind silverwind Jun 14, 2023

Choose a reason for hiding this comment

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

No, fetch is preferred. I want to get rid of jquery ajax, it's pure legacy.

Copy link
Member

Choose a reason for hiding this comment

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

If resp is consistent with backend, I would be willing to accept a refactor that unifies them.

Copy link
Contributor

Choose a reason for hiding this comment

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

There are soooo many (grep "*.go") ....

image

Copy link
Member

@silverwind silverwind Jun 14, 2023

Choose a reason for hiding this comment

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

Yeah let's do it then. Consistency over personal preference.

if (redirect) {
window.location.href = redirect;
} else {
// TODO: remove areYouSure form check
window.reload();
}
} else {
// TODO: show error to end users
onError();
}
} catch {
onError();
}
}

export function initGlobalCommon() {
// Semantic UI modules.
const $uiDropdowns = $('.ui.dropdown');
Expand Down Expand Up @@ -114,6 +165,8 @@ export function initGlobalCommon() {
if (btn.classList.contains('loading')) return e.preventDefault();
btn.classList.add('loading');
});

document.addEventListener('submit', fetchFormAction);
}

export function initGlobalDropzone() {
Expand Down