-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip: full ts * wip * progress.. * fix some ts erros * more ts * ts * disable ts * chore: install typescript-coverage-report, reenable TS in workflow * fix: add a11y label to jump button * Update js/src/forum/components/SelectBestAnswerNotification.ts Co-authored-by: Davide Iadeluca <[email protected]> * Update js/src/forum/components/SolutionSearchItem.tsx Co-authored-by: Davide Iadeluca <[email protected]> * Update js/src/forum/components/SolutionSearchItem.tsx Co-authored-by: Davide Iadeluca <[email protected]> * Update js/src/forum/components/SolutionSearchItem.tsx Co-authored-by: Davide Iadeluca <[email protected]> * Update js/src/forum/extend.ts Co-authored-by: Davide Iadeluca <[email protected]> * Update js/src/forum/extenders/extendDiscussionComposer.tsx Co-authored-by: Davide Iadeluca <[email protected]> * Update js/src/forum/extenders/extendDiscussionSearchSource.tsx Co-authored-by: Davide Iadeluca <[email protected]> * Update js/src/forum/components/AwardedBestAnswerNotification.ts Co-authored-by: Davide Iadeluca <[email protected]> * Update js/src/forum/components/BestAnswerInDiscussionNotification.ts Co-authored-by: Davide Iadeluca <[email protected]> * chore: remove unneeded tsignore * Update js/src/forum/addBestAnswerCountToUsers.tsx Co-authored-by: Davide Iadeluca <[email protected]> * Update js/src/@types/shims.d.ts Co-authored-by: Davide Iadeluca <[email protected]> * Update js/src/@types/shims.d.ts Co-authored-by: Davide Iadeluca <[email protected]> * Update js/src/forum/extenders/extendIndexPage.tsx Co-authored-by: Davide Iadeluca <[email protected]> * Update js/src/forum/extenders/extendIndexPage.tsx Co-authored-by: Davide Iadeluca <[email protected]> * import type PaginatedListParams * chore: format * chore: add dummy imports * chore: format --------- Co-authored-by: Davide Iadeluca <[email protected]> Co-authored-by: Davide Iadeluca <[email protected]>
- Loading branch information
1 parent
52cecc7
commit 95aa3c6
Showing
21 changed files
with
915 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,39 @@ | ||
import Post from 'flarum/common/models/Post'; | ||
import User from 'flarum/common/models/User'; | ||
import 'flarum/common/models/Discussion'; | ||
import 'flarum/tags/common/models/Tag'; | ||
import 'flarum/forum/states/DiscussionListState'; | ||
import 'flarum/common/models/User'; | ||
|
||
import type Post from 'flarum/common/models/Post'; | ||
import type Discussion from 'flarum/common/models/Discussion'; | ||
import type User from 'flarum/common/models/User'; | ||
import type Tag from 'flarum/tags/common/models/Tag'; | ||
import type DiscussionListState from 'flarum/forum/states/DiscussionListState'; | ||
|
||
declare module 'flarum/common/models/Discussion' { | ||
export default interface Discussion { | ||
hasBestAnswer(): boolean; | ||
bestAnswerPost(): Post; | ||
bestAnswerUser(): User; | ||
hasBestAnswer(): boolean | undefined; | ||
bestAnswerPost(): Post | null; | ||
bestAnswerUser(): User | null; | ||
canSelectBestAnswer(): boolean; | ||
bestAnswerSetAt(): Date; | ||
bestAnswerSetAt(): Date | null; | ||
} | ||
} | ||
|
||
declare module 'flarum/tags/models/Tag' { | ||
declare module 'flarum/tags/common/models/Tag' { | ||
export default interface Tag { | ||
isQnA(): boolean; | ||
reminders(): boolean; | ||
} | ||
} | ||
|
||
declare module 'flarum/forum/states/DiscussionListState' { | ||
export default interface DiscussionListState { | ||
bestAnswer: string | undefined; | ||
} | ||
} | ||
|
||
declare module 'flarum/common/models/User' { | ||
export default interface User { | ||
bestAnswerCount(): number; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import app from 'flarum/forum/app'; | ||
import { extend } from 'flarum/common/extend'; | ||
import Button from 'flarum/common/components/Button'; | ||
import PostControls from 'flarum/forum/utils/PostControls'; | ||
import DiscussionPage from 'flarum/forum/components/DiscussionPage'; | ||
import CommentPost from 'flarum/forum/components/CommentPost'; | ||
import Discussion from 'flarum/common/models/Discussion'; | ||
import Post from 'flarum/common/models/Post'; | ||
import extractText from 'flarum/common/utils/extractText'; | ||
|
||
export default function addBestAnswerAction() { | ||
const ineligible = (discussion: Discussion, post: Post) => { | ||
return post.isHidden() || post.number() === 1 || !discussion.canSelectBestAnswer() || !app.session.user; | ||
}; | ||
|
||
const blockSelectOwnPost = (post: Post): boolean => { | ||
const user = post.user(); | ||
return !app.forum.attribute<boolean>('canSelectBestAnswerOwnPost') && user !== false && user.id() === app.session.user?.id(); | ||
}; | ||
|
||
const isThisBestAnswer = (discussion: Discussion, post: Post): boolean => { | ||
const bestAnswerPost = discussion.bestAnswerPost(); | ||
const hasBestAnswer = discussion.hasBestAnswer(); | ||
return hasBestAnswer !== undefined && hasBestAnswer && bestAnswerPost !== null && bestAnswerPost.id() === post.id(); | ||
}; | ||
|
||
const actionLabel = (isBestAnswer: boolean): string => { | ||
return extractText(app.translator.trans(isBestAnswer ? 'fof-best-answer.forum.remove_best_answer' : 'fof-best-answer.forum.this_best_answer')); | ||
}; | ||
|
||
const saveDiscussion = (discussion: Discussion, isBestAnswer: boolean, post: Post) => | ||
discussion | ||
.save( | ||
{ | ||
bestAnswerPostId: isBestAnswer ? post.id() : 0, | ||
bestAnswerUserId: app.session.user?.id(), | ||
relationships: isBestAnswer ? { bestAnswerPost: post, bestAnswerUser: app.session.user } : { bestAnswerPost: null }, | ||
}, | ||
{ | ||
params: { | ||
include: 'tags', | ||
}, | ||
} | ||
) | ||
.then(() => { | ||
if (!isBestAnswer) { | ||
if (discussion.data.relationships) { | ||
delete discussion.data.relationships.bestAnswerPost; | ||
delete discussion.data.relationships.bestAnswerUser; | ||
} | ||
} | ||
|
||
if (app.current.matches(DiscussionPage)) { | ||
app.current.get('stream').update(); | ||
} | ||
|
||
m.redraw(); | ||
|
||
if (isBestAnswer) { | ||
m.route.set(app.route.discussion(discussion)); | ||
} | ||
}); | ||
|
||
extend(PostControls, 'moderationControls', function (items, post) { | ||
if (app.forum.attribute('useAlternativeBestAnswerUi')) return; | ||
|
||
const discussion = post.discussion(); | ||
let isBestAnswer = isThisBestAnswer(discussion, post); | ||
|
||
post.pushAttributes({ isBestAnswer }); | ||
|
||
if (post.contentType() !== 'comment') return; | ||
|
||
if (ineligible(discussion, post) || blockSelectOwnPost(post) || !app.current.matches(DiscussionPage)) return; | ||
|
||
items.add( | ||
'bestAnswer', | ||
<Button | ||
icon={`fa${isBestAnswer ? 's' : 'r'} fa-comment-dots`} | ||
onclick={() => { | ||
isBestAnswer = !isBestAnswer; | ||
|
||
saveDiscussion(discussion, isBestAnswer, post).finally(() => { | ||
isBestAnswer = isThisBestAnswer(discussion, post); | ||
}); | ||
}} | ||
> | ||
{actionLabel(isBestAnswer)} | ||
</Button> | ||
); | ||
}); | ||
|
||
extend(CommentPost.prototype, 'actionItems', function (items) { | ||
if (!app.forum.attribute('useAlternativeBestAnswerUi')) return; | ||
|
||
const post = this.attrs.post; | ||
const discussion = this.attrs.post.discussion(); | ||
let isBestAnswer = isThisBestAnswer(discussion, post); | ||
let hasBestAnswer = discussion.bestAnswerPost() !== null; | ||
|
||
post.pushAttributes({ isBestAnswer }); | ||
|
||
if (ineligible(discussion, post) || blockSelectOwnPost(post) || !app.current.matches(DiscussionPage)) return; | ||
|
||
items.add( | ||
'bestAnswer', | ||
<Button | ||
className={`Button Button--${!hasBestAnswer || isBestAnswer ? 'primary' : 'link'}`} | ||
onclick={() => { | ||
hasBestAnswer = !hasBestAnswer; | ||
isBestAnswer = !isBestAnswer; | ||
|
||
saveDiscussion(discussion, isBestAnswer, post).finally(() => { | ||
hasBestAnswer = !!discussion.hasBestAnswer() && discussion.bestAnswerPost() !== null; | ||
isBestAnswer = isThisBestAnswer(discussion, post); | ||
}); | ||
}} | ||
> | ||
{actionLabel(isBestAnswer)} | ||
</Button> | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.