Skip to content
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

Typescript for models #3174

Merged
merged 18 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Minor typefixes, fomat
  • Loading branch information
askvortsov1 committed Dec 12, 2021
commit f26ad3e32d0ac41e95293e866434753e333a8bd5
28 changes: 12 additions & 16 deletions js/src/common/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,12 @@ export default abstract class Model {
};

return app
.request<ApiPayloadSingle>(
{
method: this.exists ? 'PATCH' : 'POST',
url: app.forum.attribute('apiUrl') + this.apiEndpoint(),
body: request,
...options,
},
)
.request<ApiPayloadSingle>({
method: this.exists ? 'PATCH' : 'POST',
url: app.forum.attribute('apiUrl') + this.apiEndpoint(),
body: request,
...options,
})
.then(
// If everything went well, we'll make sure the store knows that this
// model exists now (if it didn't already), and we'll push the data that
Expand Down Expand Up @@ -238,14 +236,12 @@ export default abstract class Model {
if (!this.exists) return Promise.resolve();

return app
.request(
{
method: 'DELETE',
url: app.forum.attribute('apiUrl') + this.apiEndpoint(),
body,
...options,
},
)
.request({
method: 'DELETE',
url: app.forum.attribute('apiUrl') + this.apiEndpoint(),
body,
...options,
})
.then(() => {
this.exists = false;

Expand Down
14 changes: 6 additions & 8 deletions js/src/common/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,12 @@ export default class Store {
}

return app
.request<M extends Array<infer _T> ? ApiPayloadPlural : ApiPayloadSingle>(
{
method: 'GET',
url,
params,
...options,
}
)
.request<M extends Array<infer _T> ? ApiPayloadPlural : ApiPayloadSingle>({
method: 'GET',
url,
params,
...options,
})
.then((payload) => {
if (payloadIsPlural(payload)) {
return this.pushPayload<FlatArray<M, 1>[]>(payload);
Expand Down
4 changes: 2 additions & 2 deletions js/src/common/models/Discussion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class Discussion extends Model {
return computed<boolean, this>('unreadCount', (unreadCount) => !!unreadCount).call(this);
}
isRead() {
return computed<boolean, this>('unreadCount', (unreadCount) => app.session.user && !unreadCount).call(this);
return computed<boolean, this>('unreadCount', (unreadCount) => !!(app.session.user && !unreadCount)).call(this);
}

hiddenAt() {
Expand Down Expand Up @@ -112,7 +112,7 @@ export default class Discussion extends Model {
* user.
*/
unreadCount(): number {
const user: User = app.session.user;
const user = app.session.user;

if (user && (user.markedAllAsReadAt()?.getTime() ?? 0) < this.lastPostedAt()?.getTime()!) {
const unreadCount = Math.max(0, (this.lastPostNumber() ?? 0) - (this.lastReadPostNumber() || 0));
Expand Down
2 changes: 1 addition & 1 deletion js/src/forum/components/DiscussionsSearchSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class DiscussionsSearchSource implements SearchSource {

return (
<li className="DiscussionSearchResult" data-index={'discussions' + discussion.id()}>
<Link href={app.route.discussion(discussion, mostRelevantPost && mostRelevantPost.number())}>
<Link href={app.route.discussion(discussion, (mostRelevantPost && mostRelevantPost.number()) || 0)}>
<div className="DiscussionSearchResult-title">{highlight(discussion.title(), query)}</div>
{mostRelevantPost ? (
<div className="DiscussionSearchResult-excerpt">{highlight(mostRelevantPost.contentPlain() ?? '', query, 100)}</div>
Expand Down