Skip to content

Commit

Permalink
feat: update to latest remote openapi schema
Browse files Browse the repository at this point in the history
  • Loading branch information
merely04 committed Jul 28, 2024
1 parent c5827de commit 75f0e91
Show file tree
Hide file tree
Showing 16 changed files with 879 additions and 482 deletions.
2 changes: 1 addition & 1 deletion .openapirc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"file": "./src/shared/api/openapi.yaml",
"file": "https://bump.sh/undrcrxwn/doc/crowdparlay.yaml",
"outputDir": "./src/shared/api/",
"templateFileNameCode": "client.ts",
"presets": [
Expand Down
46 changes: 24 additions & 22 deletions src/pages/discussion/model.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import * as typed from 'typed-contracts';
import {chainRoute} from 'atomic-router';
import {createEvent, createStore, sample} from 'effector';
import {attach} from 'effector/compat';
import {attach, createEvent, createStore, sample} from 'effector';
import {produce} from 'immer';

import {
apiV1CommentsGet,
apiV1CommentsGetFx,
apiV1CommentsGetOk,
ApiV1CommentsParentCommentIdRepliesGet,
apiV1CommentsParentCommentIdRepliesGet,
apiV1CommentsParentCommentIdRepliesGetFx,
apiV1CommentsParentCommentIdRepliesGetOk,
ApiV1CommentsParentCommentIdRepliesPost,
apiV1CommentsParentCommentIdRepliesPost,
apiV1CommentsParentCommentIdRepliesPostFx,
ApiV1CommentsPost,
apiV1CommentsPost,
apiV1DiscussionsDiscussionIdGet,
apiV1CommentsPostFx,
apiV1DiscussionsDiscussionIdGetFx,
apiV1DiscussionsDiscussionIdGetOk,
} from '~/shared/api';
import {routes} from '~/shared/routes';

const getDiscussionFx = attach({effect: apiV1DiscussionsDiscussionIdGet});
const getCommentsFx = attach({effect: apiV1CommentsGet});
const commentDiscussionFx = attach({effect: apiV1CommentsPost});
const getRepliesFx = attach({effect: apiV1CommentsParentCommentIdRepliesGet});
const commentReplyFx = attach({effect: apiV1CommentsParentCommentIdRepliesPost});
const getDiscussionFx = attach({effect: apiV1DiscussionsDiscussionIdGetFx});
const getCommentsFx = attach({effect: apiV1CommentsGetFx});
const commentDiscussionFx = attach({effect: apiV1CommentsPostFx});
const getRepliesFx = attach({effect: apiV1CommentsParentCommentIdRepliesGetFx});
const commentReplyFx = attach({effect: apiV1CommentsParentCommentIdRepliesPostFx});

export const currentRoute = routes.discussion;

Expand All @@ -45,10 +44,13 @@ export const $discussion = createStore<typed.Get<typeof apiV1DiscussionsDiscussi
null,
);

export const $comments = createStore<typed.Get<typeof apiV1CommentsGetOk>>([]);
export const $comments = createStore<typed.Get<typeof apiV1CommentsGetOk>['items']>([]);
export const commentFormSubmit = createEvent<ApiV1CommentsPost>();

export type Replies = Record<string, typed.Get<typeof apiV1CommentsParentCommentIdRepliesGetOk>>;
export type Replies = Record<
string,
typed.Get<typeof apiV1CommentsParentCommentIdRepliesGetOk>['items']
>;
export const $replies = createStore<Replies>({});
export const replyClicked = createEvent<ApiV1CommentsParentCommentIdRepliesGet>();
export const replyFormSubmit = createEvent<ApiV1CommentsParentCommentIdRepliesPost>();
Expand All @@ -64,16 +66,16 @@ sample({
fn: ({answer}) => ({
query: {
discussionId: answer.id!,
size: 100,
page: 0,
offset: 0,
count: 20,
},
}),
target: getCommentsFx,
});

sample({
clock: getCommentsFx.doneData,
fn: (x) => x.answer,
fn: (x) => x.answer.items,
target: $comments,
});

Expand All @@ -89,8 +91,8 @@ sample({
fn: (discussion) => ({
query: {
discussionId: discussion.id!,
size: 100,
page: 0,
offset: 0,
count: 20,
},
}),
target: getCommentsFx,
Expand All @@ -107,7 +109,7 @@ sample({
fn: (replies, {params, result}) =>
produce(replies, (draft) => {
// @ts-ignore
draft[params.path.parentCommentId] = result.answer;
draft[params.path.parentCommentId] = result.answer.items;
}),
target: $replies,
});
Expand All @@ -124,8 +126,8 @@ sample({
parentCommentId: params.path.parentCommentId,
},
query: {
page: 0,
size: 100,
offset: 0,
count: 20,
},
}),
target: getRepliesFx,
Expand Down
31 changes: 2 additions & 29 deletions src/pages/discussion/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const buildReplyTree = (
canReply={true}
canReport={true}
onShownClick={(parentCommentId) =>
onReplyClicked({path: {parentCommentId}, query: {page: 0, size: 100}})
onReplyClicked({path: {parentCommentId}, query: {offset: 0, count: 100}})
}
onReplyFormSubmit={(payload) =>
onReplyFormSubmit({
Expand All @@ -72,33 +72,6 @@ export const DiscussionPage = () => {
model.replyFormSubmit,
]);

// const comments = useList(model.$comments, (comment) => (
// <Post
// key={comment.id!}
// id={comment.id!}
// author={{
// id: comment.author!.id!,
// username: comment.author!.username!,
// displayName: comment.author!.display_name!,
// avatarUrl: comment.author!.avatar_url!,
// }}
// date={new Date(comment.created_at!)}
// text={comment.content!}
// commentators={comment.first_replies_authors!.map((x) => ({
// id: x.id!,
// username: x.username!,
// displayName: x.display_name!,
// avatarUrl: x.avatar_url!,
// }))}
// commentsCount={comment.reply_count!}
// canReply={true}
// canReport={true}
// onShownClick={(parentCommentId) =>
// onReplyClicked({path: {parentCommentId}, query: {page: 0, size: 20}})
// }
// />
// ));

const items = comments.map((comment) => {
const children = buildReplyTree(comment.id!, replies, onReplyClicked, onReplyFormSubmit);

Expand All @@ -124,7 +97,7 @@ export const DiscussionPage = () => {
canReply={true}
canReport={true}
onShownClick={(parentCommentId) =>
onReplyClicked({path: {parentCommentId}, query: {page: 0, size: 100}})
onReplyClicked({path: {parentCommentId}, query: {offset: 0, count: 100}})
}
onReplyFormSubmit={(payload) =>
onReplyFormSubmit({
Expand Down
14 changes: 8 additions & 6 deletions src/pages/profile/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import {chainRoute} from 'atomic-router';
import {attach, createStore, sample} from 'effector';

import {
apiV1DiscussionsGet,
apiV1DiscussionsGetFx,
apiV1DiscussionsGetOk,
apiV1UsersResolveGet,
apiV1UsersResolveGetFx,
apiV1UsersResolveGetOk,
} from '~/shared/api';
import {routes} from '~/shared/routes';

const getUserFx = attach({effect: apiV1UsersResolveGet});
const getDiscussionsFx = attach({effect: apiV1DiscussionsGet});
const getUserFx = attach({effect: apiV1UsersResolveGetFx});
const getDiscussionsFx = attach({effect: apiV1DiscussionsGetFx});

export const currentRoute = routes.profile;

Expand All @@ -31,7 +31,7 @@ export const dataLoadedRoute = chainRoute({

export const $user = createStore<typed.Get<typeof apiV1UsersResolveGetOk> | null>(null);

export const $discussions = createStore<typed.Get<typeof apiV1DiscussionsGetOk>>([]);
export const $discussions = createStore<typed.Get<typeof apiV1DiscussionsGetOk>['items']>([]);

sample({
clock: getUserFx.doneData,
Expand All @@ -44,13 +44,15 @@ sample({
fn: ({answer}) => ({
query: {
authorId: answer.id!,
count: 20,
offset: 0,
},
}),
target: getDiscussionsFx,
});

sample({
clock: getDiscussionsFx.doneData,
fn: (x) => x.answer,
fn: (x) => x.answer.items,
target: $discussions,
});
2 changes: 2 additions & 0 deletions src/pages/profile/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {useList, useUnit} from 'effector-react';

import {NotFoundPage} from '~/pages/not-found/page';

import {
Avatar,
Button,
Expand Down
14 changes: 14 additions & 0 deletions src/pages/sign-in/assets/google.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 43 additions & 26 deletions src/pages/sign-in/model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {attach, createStore} from 'effector';
import {attach, createEvent, createStore} from 'effector';
import {sample} from 'effector';
import {createForm} from 'effector-forms';
import {createEffect} from 'effector/effector.umd';
import {or} from 'patronum';

import * as api from '~/shared/api';
import {LOCAL_STORAGE_ACCESS_TOKEN_KEY, LOCAL_STORAGE_REFRESH_TOKEN_KEY} from '~/shared/config';
import {routes} from '~/shared/routes';
import {ApiV1AuthenticationSignInPost, ApiV1AuthenticationSsoGoogleGet} from '~/shared/api';
import {router, routes} from '~/shared/routes';
import {rules} from '~/shared/rules';
import {chainAnonymous, sessionRequestFx} from '~/shared/session';

Expand All @@ -14,7 +15,15 @@ export const anonymousRoute = chainAnonymous(currentRoute, {
otherwise: routes.explore.open,
});

const signInFx = attach({effect: api.signInFx});
const signInFx = attach({effect: api.apiV1AuthenticationSignInPostFx});
const signInWithGoogleFx = attach({effect: api.apiV1AuthenticationSsoGoogleGetFx});
const navigateToUrlFx = createEffect((url: string) => {
console.log(url);
router.push({path: url, params: {}, query: {}, method: 'replace'});
});

export const signInWithGoogleClicked = createEvent();

export const $loading = or(signInFx.pending, sessionRequestFx.pending);

export const $form = createForm({
Expand Down Expand Up @@ -42,40 +51,48 @@ sample({

sample({
clock: $form.formValidated,
fn: (payload): ApiV1AuthenticationSignInPost => ({
body: {
usernameOrEmail: payload.username,
password: payload.password,
},
}),
target: [signInFx, $formError.reinit],
});

sample({
clock: signInFx.doneData,
filter: (res) => res.status >= 300 && res.status === 400 && Boolean(res.body.validation_errors),
clock: signInFx.failData,
fn: (res) => {
return Object.entries(res.body.validation_errors!).map(([field, errorText]) => {
return {
field,
rule: 'backend',
errorText: errorText as string,
};
});
if ('error_description' in res.error) {
return res.error.error_description!;
}
return 'Something went wrong. Try again later.';
},
target: $form.addErrors,
target: $formError,
});

sample({
clock: signInFx.doneData,
filter: (res) =>
res.status >= 300 && !(res.status === 400 && Boolean(res.body.validation_errors)),
fn: (res) => {
return res.body.error_description ?? 'Something went wrong. Try again later.';
},
target: $formError,
source: currentRoute.$query,
filter: (query) => query.returnUrl === undefined,
target: sessionRequestFx,
});

sample({
clock: signInFx.doneData,
filter: (res) => res.status < 300,
fn: (data) => {
localStorage.setItem(LOCAL_STORAGE_ACCESS_TOKEN_KEY, data.body.access_token);
localStorage.setItem(LOCAL_STORAGE_REFRESH_TOKEN_KEY, data.body.refresh_token);
},
target: sessionRequestFx,
source: currentRoute.$query,
filter: (query) => query.returnUrl !== undefined,
fn: (query) => query.returnUrl!,
target: navigateToUrlFx,
});

sample({
clock: signInWithGoogleClicked,
source: currentRoute.$query,
fn: (state): ApiV1AuthenticationSsoGoogleGet => ({
query: {
returnUrl: state.returnUrl,
},
}),
target: signInWithGoogleFx,
});
15 changes: 15 additions & 0 deletions src/pages/sign-in/page.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,18 @@
line-height: 77px;
color: var(--color-white);
}

.external {
margin-top: -27px;
display: flex;
flex-direction: column;
gap: 25px;
}

.or {
line-height: 19px;
}

.button {
gap: 10px;
}
Loading

0 comments on commit 75f0e91

Please sign in to comment.