Skip to content

Commit

Permalink
fixes for redirect from /discussions route to particular discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
tima101 committed Sep 26, 2018
1 parent 920bcf2 commit f88de54
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 29 deletions.
8 changes: 4 additions & 4 deletions api/server/api/team-member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ router.use((req, res, next) => {
async function loadDiscussionsData(team, userId, body) {
const { discussionSlug } = body;

if (!discussionSlug) {
return {};
}

const { discussions } = await Discussion.getList({
userId,
teamId: team._id,
Expand All @@ -43,10 +47,6 @@ async function loadDiscussionsData(team, userId, body) {
}
}

if (discussionSlug) {
data.currentDiscussionSlug = discussionSlug;
}

return data;
}

Expand Down
12 changes: 9 additions & 3 deletions app/components/discussions/DiscussionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ import ActiveLink from '../common/ActiveLink';
import CreateDiscussionForm from './CreateDiscussionForm';
import DiscussionListItem from './DiscussionListItem';

import notify from '../../lib/notifier';

type Props = { store?: Store; team: Team };

class DiscussionList extends React.Component<Props> {
public state = {
discussionFormOpen: false,
};

public componentDidMount() {
this.props.team.loadDiscussions().catch(err => notify(err));
}

public componentDidUpdate(prevProps: Props) {
if (this.props.team._id !== prevProps.team._id) {
this.props.team.loadDiscussions();
this.props.team.loadDiscussions().catch(err => notify(err));
}
}

Expand All @@ -30,8 +36,8 @@ class DiscussionList extends React.Component<Props> {
hasIcon
linkText="Discussions"
href={`/discussion?teamSlug=${team.slug}`}
as={`/team/${team.slug}/d`}
highlighterSlug={`/${team.slug}/d`}
as={`/team/${team.slug}/discussions`}
highlighterSlug={`/team/${team.slug}/discussions`}
/>

<Tooltip title="Add Discussion" placement="right" disableFocusListener disableTouchListener>
Expand Down
17 changes: 6 additions & 11 deletions app/components/posts/PostForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import NProgress from 'nprogress';
import React from 'react';

import notify from '../../lib/notifier';
import { Post, Store, User } from '../../lib/store';
import { Discussion, Post, Store, User } from '../../lib/store';

import PostEditor from './PostEditor';

Expand All @@ -26,6 +26,7 @@ type MyProps = {
onFinished?: () => void;
open?: boolean;
classes: { paper: string };
discussion: Discussion;
};

type MyState = {
Expand Down Expand Up @@ -101,7 +102,7 @@ class PostForm extends React.Component<MyProps, MyState> {

const { content } = this.state;
const htmlContent = marked(he.decode(content));
const { post, onFinished, store } = this.props;
const { post, onFinished, store, discussion } = this.props;
const isEditing = !!post;

if (!content) {
Expand Down Expand Up @@ -133,26 +134,20 @@ class PostForm extends React.Component<MyProps, MyState> {

const { currentTeam } = store;
if (!currentTeam) {
notify('Team have not selected');
return;
}

const { currentDiscussion } = currentTeam;
if (!currentDiscussion) {
notify('Discussion have not selected');
notify('Team is not selected or does not exist.');
return;
}

NProgress.start();
this.setState({ disabled: true });

try {
await currentDiscussion.addPost(content);
await discussion.addPost(content);

this.setState({ content: '' });
NProgress.done();
this.setState({ disabled: false });
notify('You successfully published Post');
notify('You successfully published Post.');
} catch (error) {
console.log(error);
notify(error);
Expand Down
4 changes: 3 additions & 1 deletion app/lib/store/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class Team {

if (params.initialDiscussions) {
this.setInitialDiscussions(params.initialDiscussions);
} else {
this.loadDiscussions();
}
}

Expand Down Expand Up @@ -225,7 +227,7 @@ class Team {
`/team/${this.slug}/discussions/${d.slug}`,
);
} else {
Router.push(`/discussion?teamSlug=${this.slug}`, `/team/${this.slug}/d`);
Router.push(`/discussion?teamSlug=${this.slug}`, `/team/${this.slug}/discussions`);
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion app/lib/withAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function withAuth(
asUrl = '/create-team';
} else {
redirectUrl = `/discussion?teamSlug=${user.defaultTeamSlug}`;
asUrl = `/team/${user.defaultTeamSlug}/d`;
asUrl = `/team/${user.defaultTeamSlug}/discussions`;
}
}

Expand Down
4 changes: 2 additions & 2 deletions app/lib/withStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ export default function withStore(BaseComponent) {
Object.assign(props, (await BaseComponent.getInitialProps(ctx)) || {});
}

const { teamSlug, topicSlug, discussionSlug } = props;
const { teamSlug, discussionSlug } = props;
let initialData = {};

if (user) {
try {
initialData = await getInitialData({
request: ctx.req,
data: { teamSlug, topicSlug, discussionSlug },
data: { teamSlug, discussionSlug },
});
} catch (error) {
console.log(error);
Expand Down
1 change: 1 addition & 0 deletions app/pages/discussion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ class DiscussionComp extends React.Component<Props> {
<p />
<br />
<PostForm
discussion={discussion}
post={selectedPost}
open={drawerState}
members={discussion.members}
Expand Down
4 changes: 2 additions & 2 deletions app/pages/invitation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Invitation extends React.Component<{ store: Store; team: Team; token: stri
if (user && team) {
if (team.memberIds.includes(user._id)) {
removeInvitationIfMemberAdded(token);
Router.push(`/discussion?teamSlug=${team.slug}`, `/team/${team.slug}/d`);
Router.push(`/discussion?teamSlug=${team.slug}`, `/team/${team.slug}/discussions`);
} else {
Router.push('/');
}
Expand Down Expand Up @@ -78,7 +78,7 @@ class Invitation extends React.Component<{ store: Store; team: Team; token: stri
Join <b>{team.name}</b> by logging in with your Google account.
</p>
<br />
<LoginButton next={`/team/${team.slug}/d`} invitationToken={token} />
<LoginButton next={`/team/${team.slug}/discussions`} invitationToken={token} />
</div>
);
}
Expand Down
10 changes: 5 additions & 5 deletions app/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,22 @@ app.prepare().then(() => {
let redirectUrl = 'login';

if (req.user) {
if (!req.user.defaultTeamSlug) {
redirectUrl = 'settings/create-team';
if (!req.user.isAdmin && !req.user.defaultTeamSlug) {
redirectUrl = 'create-team';
} else {
redirectUrl = `team/${req.user.defaultTeamSlug}/d`;
redirectUrl = `team/${req.user.defaultTeamSlug}/discussions`;
}
}

res.redirect(`${ROOT_URL}/${redirectUrl}`);
});

routesWithSlug({ server, app });

server.get('/robots.txt', (_, res) => {
res.sendFile(path.join(__dirname, '../static', 'robots.txt'));
});

routesWithSlug({ server, app });

server.get('*', (req, res) => {
handle(req, res);
});
Expand Down

0 comments on commit f88de54

Please sign in to comment.