Browse Source

navbar: update state updating and use http for GetSite

unoptim
eiknat 2 years ago
parent
commit
53e83fbf2c
  1. 5
      package.json
  2. 87
      src/components/navbar.tsx

5
package.json

@ -131,16 +131,13 @@
"engineStrict": true,
"husky": {
"hooks": {
"pre-commit": "cargo clippy --manifest-path ../server/Cargo.toml --all-targets --workspace -- -D warnings && tsc --noEmit && lint-staged"
"pre-commit": "tsc --noEmit && lint-staged"
}
},
"lint-staged": {
"*.{ts,tsx,js}": [
"eslint --fix"
],
"../server/src/**/*.rs": [
"rustfmt --config-path ../server/.rustfmt.toml"
],
"package.json": [
"sortpack"
]

87
src/components/navbar.tsx

@ -95,10 +95,8 @@ class UnwrappedNavbar extends Component<any, NavbarState> {
if (res !== undefined) {
this.requestNotificationPermission();
} else {
this.state.isLoggedIn = false;
this.setState({ isLoggedIn: false });
}
WebSocketService.Instance.getSite();
this.setState(this.state);
});
this.wsSub = WebSocketService.Instance.subject
@ -114,17 +112,19 @@ class UnwrappedNavbar extends Component<any, NavbarState> {
});
this.searchTextField = createRef();
this.fetchSite();
}
handleSearchParam(i: UnwrappedNavbar, event: any) {
i.state.searchParam = event.target.value;
i.setState(i.state);
this.setState({ searchParam: event.target.value });
}
updateUrl() {
const searchParam = this.state.searchParam;
this.setState({ searchParam: '' });
this.setState({ toggleSearch: false });
this.setState({
searchParam: '',
toggleSearch: false,
});
if (searchParam === '') {
this.props.history.push(`/search/`);
} else {
@ -152,8 +152,7 @@ class UnwrappedNavbar extends Component<any, NavbarState> {
handleSearchBlur(i: UnwrappedNavbar, event: any) {
if (!(event.relatedTarget && event.relatedTarget.name !== 'search-btn')) {
i.state.toggleSearch = false;
i.setState(i.state);
this.setState({ toggleSearch: false });
}
}
@ -345,9 +344,6 @@ class UnwrappedNavbar extends Component<any, NavbarState> {
py={0}
pt="2px"
px={this.state.toggleSearch ? 2 : 0}
// className={`btn btn-link ${
// this.state.toggleSearch ? 'px-2' : 'px-0'
// }`}
style={{ border: 0 }}
>
<Icon name="search" />
@ -444,13 +440,11 @@ class UnwrappedNavbar extends Component<any, NavbarState> {
}
expandNavbar(i: UnwrappedNavbar) {
i.state.expanded = !i.state.expanded;
i.setState(i.state);
this.setState({ expanded: !i.state.expanded });
}
showCommunityDropdown(i: UnwrappedNavbar) {
i.state.communityDropdownShown = !i.state.communityDropdownShown;
i.setState(i.state);
this.setState({ communityDropdownShown: !i.state.communityDropdownShown });
}
parseMessage(msg: WebSocketJsonResponse) {
@ -469,9 +463,10 @@ class UnwrappedNavbar extends Component<any, NavbarState> {
if (this.state.isLoggedIn) {
if (data.recipient_ids.includes(UserService.Instance.user.id)) {
this.state.replies.push(data.comment);
this.state.unreadCount++;
this.setState(this.state);
this.setState(state => ({
replies: state.replies.concat(data.comment),
unreadCount: state.unreadCount + 1,
}));
this.sendUnreadCount();
this.notify(data.comment);
}
@ -481,38 +476,42 @@ class UnwrappedNavbar extends Component<any, NavbarState> {
if (this.state.isLoggedIn) {
if (data.message.recipient_id == UserService.Instance.user.id) {
this.state.messages.push(data.message);
this.state.unreadCount++;
this.setState(this.state);
this.setState(state => ({
messages: state.messages.concat(data.message),
unreadCount: state.unreadCount + 1,
}));
this.sendUnreadCount();
this.notify(data.message);
}
}
} else if (res.op == UserOperation.GetSite) {
let data = res.data as GetSiteResponse;
if (data.site && !this.state.siteName) {
this.state.siteName = data.site.name;
this.state.admins = data.admins;
this.state.creatingCommunitiesEnabled =
data.site.enable_create_communities;
}
if (data.my_user) {
UserService.Instance.setUser(data.my_user);
}
}
if (this.state.isLoggedIn == false) {
this.requestNotificationPermission();
this.fetchUnreads();
setTheme(data.my_user.theme, true);
}
async fetchSite(): Promise<void> {
const params = new URLSearchParams({});
if (UserService.Instance.auth) {
params.append('auth', UserService.Instance.auth);
}
const res = await api.get(`/site?${params.toString()}`);
const data = res.data as GetSiteResponse;
this.state.isLoggedIn = true;
if (data.my_user) {
UserService.Instance.setUser(data.my_user);
if (this.state.isLoggedIn == false) {
this.requestNotificationPermission();
this.fetchUnreads();
setTheme(data.my_user.theme, true);
}
this.state.siteLoading = false;
this.setState(this.state);
this.setState({ isLoggedIn: true });
}
this.setState({
siteName: data.site.name,
admins: data.admins,
sitemods: data.sitemods,
creatingCommunitiesEnabled: data.site.enable_create_communities,
siteLoading: false,
});
}
async fetchUnreads() {
@ -535,7 +534,7 @@ class UnwrappedNavbar extends Component<any, NavbarState> {
}
sendUnreadCount() {
UserService.Instance.unreadCountSub.next(this.state.unreadCount);
// UserService.Instance.unreadCountSub.next(this.state.unreadCount);
}
get canAdmin(): boolean {

Loading…
Cancel
Save