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

feat: add mobx-strapi #209

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 9 additions & 6 deletions models/Activity/Award.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { User } from '@authing/native-js-ui-components';
import { ListModel, Stream, toggle } from 'mobx-restful';
import { Stream, toggle } from 'mobx-restful';
import { StrapiListModel } from 'mobx-strapi';

import { Base, createListStream, InputData, Media } from '../Base';
import sessionStore from '../User/Session';
import sessionStore, { strapiClient } from '../User/Session';
luojiyin1987 marked this conversation as resolved.
Show resolved Hide resolved
import { Team } from './Team';

export interface Award
Expand All @@ -22,8 +23,8 @@ export interface AwardAssignment
award: Award;
}

export class AwardModel extends Stream<Award>(ListModel) {
client = sessionStore.client;
Copy link
Member

Choose a reason for hiding this comment

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

此处不要改动,已经配置好登录验证机制的 API 客户端就是放在这个 store 上的。

Copy link
Author

Choose a reason for hiding this comment

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

这我也看不懂了

Copy link
Member

Choose a reason for hiding this comment

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

这我也看不懂了

先说说你个人的理解。

Copy link
Author

Choose a reason for hiding this comment

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

原来25 26 行代码是否要改动? 我已经迷糊了。

Copy link
Member

Choose a reason for hiding this comment

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

原来25 26 行代码是否要改动? 我已经迷糊了。

Screenshot_2024-05-22-21-41-41-623_com.github.android-edit.jpg

Copy link
Author

Choose a reason for hiding this comment

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

现在遇到 类型“AwardModel”上不存在属性“baseURI 的报错

Copy link
Member

Choose a reason for hiding this comment

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

此处不要改动,已经配置好登录验证机制的 API 客户端就是放在这个 store 上的。

关于 client 的问题是我昏头了,你改为 strapiClient 是对的,是我在迁移阶段还没把 client 移到 sessionStore 上。

Copy link
Member

Choose a reason for hiding this comment

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

现在遇到 类型“AwardModel”上不存在属性“baseURI 的报错

非流式加载的列表必须声明对应数据结构 API 根路径:
image

参考:https://github.com/idea2app/MobX-RESTful/tree/main/wrapper/Strapi#modelarticlets

export class AwardModel extends StrapiListModel<Award> {
client = strapiClient;
currentAssignment?: AwardAssignmentModel;

constructor(baseURI: string) {
Expand Down Expand Up @@ -55,8 +56,10 @@ export class AwardModel extends Stream<Award>(ListModel) {
}
}

export class AwardAssignmentModel extends Stream<AwardAssignment>(ListModel) {
client = sessionStore.client;
export class AwardAssignmentModel extends Stream<AwardAssignment>(
StrapiListModel,
) {
client = strapiClient;

constructor(baseURI: string) {
super();
Expand Down
9 changes: 5 additions & 4 deletions models/Activity/Enrollment.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { computed, observable } from 'mobx';
import { ListModel, Statistic, Stream, toggle } from 'mobx-restful';
import { Statistic, Stream, toggle } from 'mobx-restful';
import { StrapiListModel } from 'mobx-strapi';
import { buildURLData, countBy, groupBy } from 'web-utility';

import { Base, createListStream, Filter } from '../Base';
import { i18n } from '../Base/Translation';
import { User } from '../User';
import sessionStore from '../User/Session';
import sessionStore, { strapiClient } from '../User/Session';

const { t } = i18n;

Expand All @@ -32,15 +33,15 @@ export interface EnrollmentStatistic
}

export class EnrollmentModel extends Stream<Enrollment, EnrollmentFilter>(
ListModel,
StrapiListModel,
) {
constructor(baseURI: string) {
super();

this.baseURI = `${baseURI}/enrollment`;
}

client = sessionStore.client;
client = strapiClient;
indexKey = 'userId' as const;

@observable
Expand Down
9 changes: 5 additions & 4 deletions models/Activity/Log.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { IDType, ListModel, Stream } from 'mobx-restful';
import { IDType, Stream } from 'mobx-restful';
import { StrapiListModel } from 'mobx-strapi';

import { Base, createListStream } from '../Base';
import sessionStore from '../User/Session';
import sessionStore, { strapiClient } from '../User/Session';

export interface Log extends Base {
operatorId: IDType;
Expand All @@ -10,8 +11,8 @@ export interface Log extends Base {
activityLogType: string;
}

export class LogModel extends Stream<Log>(ListModel) {
client = sessionStore.client;
export class LogModel extends Stream<Log>(StrapiListModel) {
client = strapiClient;

constructor(baseURI: string) {
super();
Expand Down
11 changes: 7 additions & 4 deletions models/Activity/Message.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { IDType, ListModel, Stream, toggle } from 'mobx-restful';
import { IDType, Stream, toggle } from 'mobx-restful';
import { StrapiListModel } from 'mobx-strapi';
import { buildURLData } from 'web-utility';

import { Base, createListStream, Filter, InputData } from '../Base';
import { i18n } from '../Base/Translation';
import sessionStore from '../User/Session';
import sessionStore, { strapiClient } from '../User/Session';

const { t } = i18n;

Expand All @@ -22,8 +23,10 @@ export interface Message extends Base {

export type MessageFilter = Filter<Message>;

export class MessageModel extends Stream<Message, MessageFilter>(ListModel) {
client = sessionStore.client;
export class MessageModel extends Stream<Message, MessageFilter>(
StrapiListModel,
) {
client = strapiClient;

constructor(baseURI: string) {
super();
Expand Down
9 changes: 5 additions & 4 deletions models/Activity/Organization.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { computed } from 'mobx';
import { IDType, ListModel, Stream, toggle } from 'mobx-restful';
import { IDType, Stream, toggle } from 'mobx-restful';
import { StrapiListModel } from 'mobx-strapi';
import { groupBy } from 'web-utility';

import { Base, createListStream, InputData, Media } from '../Base';
import { i18n } from '../Base/Translation';
import sessionStore from '../User/Session';
import sessionStore, { strapiClient } from '../User/Session';

const { t } = i18n;

Expand Down Expand Up @@ -32,8 +33,8 @@ export interface Organization extends Base {
url?: string;
}

export class OrganizationModel extends Stream<Organization>(ListModel) {
client = sessionStore.client;
export class OrganizationModel extends Stream<Organization>(StrapiListModel) {
client = strapiClient;

constructor(public baseURI: string) {
super();
Expand Down
9 changes: 5 additions & 4 deletions models/Activity/Staff.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { computed } from 'mobx';
import { ListModel, Stream, toggle } from 'mobx-restful';
import { Stream, toggle } from 'mobx-restful';
import { StrapiListModel } from 'mobx-strapi';
import { groupBy, mergeStream } from 'web-utility';

import { Base, createListStream, InputData } from '../Base';
import { User } from '../User';
import sessionStore from '../User/Session';
import sessionStore, { strapiClient } from '../User/Session';

export interface HackathonAdmin
extends Base,
Expand All @@ -16,8 +17,8 @@ export interface Staff extends HackathonAdmin {
type: 'admin' | 'judge' | 'member';
}

export class StaffModel extends Stream<Staff>(ListModel) {
client = sessionStore.client;
export class StaffModel extends Stream<Staff>(StrapiListModel) {
client = strapiClient;
indexKey = 'userId' as const;

constructor(public baseURI: string) {
Expand Down
23 changes: 13 additions & 10 deletions models/Activity/Team.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { action, computed, observable } from 'mobx';
import { ListModel, Stream, toggle } from 'mobx-restful';
import { Stream, toggle } from 'mobx-restful';
import { StrapiListModel } from 'mobx-strapi';
import { buildURLData } from 'web-utility';

import {
Expand All @@ -11,7 +12,7 @@ import {
} from '../Base';
import { WorkspaceModel } from '../Git';
import { User } from '../User';
import sessionStore from '../User/Session';
import sessionStore, { strapiClient } from '../User/Session';
import { AwardAssignment } from './Award';
import { NameAvailability } from './index';

Expand Down Expand Up @@ -67,14 +68,14 @@ export interface JoinTeamReqBody extends Pick<TeamMember, 'role'> {
description?: string;
}

export class TeamModel extends Stream<Team, TeamFilter>(ListModel) {
export class TeamModel extends Stream<Team, TeamFilter>(StrapiListModel) {
constructor(baseURI: string) {
super();

this.baseURI = `${baseURI}/team`;
}

client = sessionStore.client;
client = strapiClient;
currentMember?: TeamMemberModel;
currentWork?: TeamWorkModel;
currentWorkspace?: WorkspaceModel;
Expand Down Expand Up @@ -175,14 +176,14 @@ export class TeamModel extends Stream<Team, TeamFilter>(ListModel) {
}

export class TeamMemberModel extends Stream<TeamMember, Filter<TeamMember>>(
ListModel,
StrapiListModel,
) {
constructor(baseURI: string) {
super();
this.baseURI = `${baseURI}/member`;
}

client = sessionStore.client;
client = strapiClient;

@observable
accessor sessionOne: TeamMember | undefined;
Expand Down Expand Up @@ -225,8 +226,8 @@ export class TeamMemberModel extends Stream<TeamMember, Filter<TeamMember>>(
}
}

export class TeamWorkModel extends Stream<TeamWork>(ListModel) {
client = sessionStore.client;
export class TeamWorkModel extends Stream<TeamWork>(StrapiListModel) {
client = strapiClient;

constructor(baseURI: string) {
super();
Expand All @@ -251,8 +252,10 @@ export class TeamWorkModel extends Stream<TeamWork>(ListModel) {
}
}

export class TeamAssignmentModel extends Stream<AwardAssignment>(ListModel) {
client = sessionStore.client;
export class TeamAssignmentModel extends Stream<AwardAssignment>(
StrapiListModel,
) {
client = strapiClient;

constructor(baseURI: string) {
super();
Expand Down
11 changes: 7 additions & 4 deletions models/Activity/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { action, observable } from 'mobx';
import { ListModel, Stream, toggle } from 'mobx-restful';
import { Stream, toggle } from 'mobx-restful';
import { StrapiListModel } from 'mobx-strapi';
import { buildURLData } from 'web-utility';

import { Base, createListStream, Filter, InputData, Media } from '../Base';
import { GitModel } from '../Git';
import { GitTemplateModal } from '../TemplateRepo';
import platformAdmin from '../User/PlatformAdmin';
import sessionStore from '../User/Session';
import sessionStore, { strapiClient } from '../User/Session';
import { AwardModel } from './Award';
import { Enrollment, EnrollmentModel } from './Enrollment';
import { LogModel } from './Log';
Expand Down Expand Up @@ -73,8 +74,10 @@ export interface Questionnaire extends Base {
hackathonName: string;
}

export class ActivityModel extends Stream<Activity, ActivityFilter>(ListModel) {
client = sessionStore.client;
export class ActivityModel extends Stream<Activity, ActivityFilter>(
StrapiListModel,
) {
client = strapiClient;
baseURI = 'hackathon';
indexKey = 'name' as const;

Expand Down
4 changes: 2 additions & 2 deletions models/Base/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HTTPError, Request, request } from 'koajax';
import { DataObject, toggle } from 'mobx-restful';
import { FileModel } from 'mobx-restful-table';

import sessionStore from '../User/Session';
import sessionStore, { strapiClient } from '../User/Session';
import { ErrorBaseData, UploadUrl } from './index';

export class AzureFileModel extends FileModel {
Expand Down Expand Up @@ -38,7 +38,7 @@ export class AzureFileModel extends FileModel {
async upload(file: File) {
const { type, name } = file;

const { body } = await sessionStore.client.post<UploadUrl>(
const { body } = await strapiClient.post<UploadUrl>(
`user/generateFileUrl`,
{
filename: name,
Expand Down
9 changes: 5 additions & 4 deletions models/Git.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { components } from '@octokit/openapi-types';
import { HTTPClient } from 'koajax';
import { memoize } from 'lodash';
import { ListModel, Stream, toggle } from 'mobx-restful';
import { Stream, toggle } from 'mobx-restful';
import { StrapiListModel } from 'mobx-strapi';
import { averageOf } from 'web-utility';

import { TeamWork, TeamWorkType } from './Activity/Team';
import { Base, createListStream } from './Base';
import sessionStore from './User/Session';
import sessionStore, { strapiClient } from './User/Session';

type Repository = components['schemas']['repository'];

Expand Down Expand Up @@ -84,7 +85,7 @@ const getGitRepository = memoize(
},
);

export class GitModel extends Stream<GitRepository>(ListModel) {
export class GitModel extends Stream<GitRepository>(StrapiListModel) {
baseURI = 'repos';
client = gitClient;

Expand Down Expand Up @@ -133,7 +134,7 @@ const DefaultTemplates = [
];

export class WorkspaceModel extends GitModel {
client = sessionStore.client;
client = strapiClient;

constructor(baseURI: string) {
super();
Expand Down
9 changes: 5 additions & 4 deletions models/TemplateRepo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ListModel, Stream, toggle } from 'mobx-restful';
import { Stream, toggle } from 'mobx-restful';
import { StrapiListModel } from 'mobx-strapi';

import { Base, createListStream, InputData } from './Base';
import sessionStore from './User/Session';
import sessionStore, { strapiClient } from './User/Session';

export interface GitTemplate extends Base {
url: string;
Expand All @@ -13,8 +14,8 @@ export interface GitTemplate extends Base {
description?: string;
}

export class GitTemplateModal extends Stream<GitTemplate>(ListModel) {
client = sessionStore.client;
export class GitTemplateModal extends Stream<GitTemplate>(StrapiListModel) {
client = strapiClient;

constructor(public baseURI: string) {
super();
Expand Down
9 changes: 5 additions & 4 deletions models/User/PlatformAdmin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { observable } from 'mobx';
import { IDType, ListModel, Stream, toggle } from 'mobx-restful';
import { IDType, Stream, toggle } from 'mobx-restful';
import { StrapiListModel } from 'mobx-strapi';

import { HackathonAdmin } from '../Activity/Staff';
import { createListStream, Filter } from '../Base';
import sessionStore from './Session';
import sessionStore, { strapiClient } from './Session';

export type PlatformAdmin = HackathonAdmin;

Expand All @@ -12,8 +13,8 @@ export type PlatformAdminFilter = Filter<PlatformAdmin>;
export class PlatformAdminModel extends Stream<
PlatformAdmin,
PlatformAdminFilter
>(ListModel) {
client = sessionStore.client;
>(StrapiListModel) {
client = strapiClient;
baseURI = 'platform/admin';
indexKey = 'userId' as const;

Expand Down
9 changes: 5 additions & 4 deletions models/User/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ListModel, Stream } from 'mobx-restful';
import { Stream } from 'mobx-restful';
import { StrapiListModel } from 'mobx-strapi';
import { buildURLData } from 'web-utility';

import { Base, createListStream, Filter, ListData } from '../Base';
import sessionStore from './Session';
import sessionStore, { strapiClient } from './Session';

export interface UserBase {
username: string;
Expand Down Expand Up @@ -125,8 +126,8 @@ export interface UserFilter extends Filter<User> {
keyword?: string;
}

export class UserModel extends Stream<User, UserFilter>(ListModel) {
client = sessionStore.client;
export class UserModel extends Stream<User, UserFilter>(StrapiListModel) {
client = strapiClient;
baseURI = 'user';

async getUserTopList() {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"mobx-react-helper": "^0.3.1",
"mobx-restful": "^0.7.0-rc.0",
"mobx-restful-table": "^2.0.0-rc.0",
"mobx-strapi": "^0.5.0",
"next": "^14.1.0",
"next-ssr-middleware": "^0.7.0",
"open-react-map": "^0.8.0",
Expand Down
Loading
Loading