Skip to content

Instantly share code, notes, and snippets.

@undrcrxwn
Created February 1, 2024 18:02
Show Gist options
  • Save undrcrxwn/10c6eb31a664886a041b22b909220e77 to your computer and use it in GitHub Desktop.
Save undrcrxwn/10c6eb31a664886a041b22b909220e77 to your computer and use it in GitHub Desktop.
import * as typed from 'typed-contracts';
import {chainRoute} from 'atomic-router';
import {attach, createStore, sample} from 'effector';
import {
apiV1DiscussionsGet,
apiV1DiscussionsGetOk,
apiV1UsersResolveGet,
apiV1UsersResolveGetOk,
} from '~/shared/api';
import {routes} from '~/shared/routes';
const getUserFx = attach({effect: apiV1UsersResolveGet});
const getDiscussionsFx = attach({effect: apiV1DiscussionsGet});
export const currentRoute = routes.profile;
export const dataLoadedRoute = chainRoute({
route: currentRoute,
beforeOpen: {
effect: getUserFx,
mapParams: ({params}) => {
return {
query: {
username: params.username,
},
};
},
},
});
export const $user = createStore<typed.Get<typeof apiV1UsersResolveGetOk> | null>(null);
export const $discussions = createStore<typed.Get<typeof apiV1DiscussionsGetOk>>([]);
sample({
clock: getUserFx.doneData,
fn: (x) => x.answer,
target: $user,
});
sample({
clock: getUserFx.doneData,
fn: ({answer}) => ({
query: {
authorId: answer.id!,
},
}),
target: getDiscussionsFx,
});
sample({
clock: apiV1DiscussionsGet.doneData,
fn: (x) => x.answer,
target: $discussions,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment