u
This commit is contained in:
@@ -182,6 +182,23 @@ function connect() {
|
||||
|
||||
const es = new EventSource(url.href, { withCredentials: true });
|
||||
|
||||
addTrackedListener(es, 'user', (event) => {
|
||||
try {
|
||||
const payload = JSON.parse(event.data || '{}');
|
||||
const nextCompanyId = payload.selectedCompanyId ?? null;
|
||||
|
||||
if (authAutomat.state.user?.selectedCompanyId !== nextCompanyId) {
|
||||
authAutomat.setState((prev) => ({
|
||||
...prev,
|
||||
user: prev.user ? { ...prev.user, selectedCompanyId: nextCompanyId } : prev.user,
|
||||
hasCompany: Boolean((prev.hasAuth ?? (prev.status === 'authed' && prev.user)) && nextCompanyId != null),
|
||||
}));
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[sse] failed to handle user event', err);
|
||||
}
|
||||
});
|
||||
|
||||
addTrackedListener(es, 'company', () => {
|
||||
const comp = Automat.get('company');
|
||||
if (comp?.actions?.reload) {
|
||||
|
||||
@@ -157,6 +157,9 @@ class CompanySetupDialog extends PureComponent {
|
||||
|
||||
componentDidMount() {
|
||||
this.unsubscribe = companyAutomat.subscribe(this, mapCompanyState);
|
||||
this.unsubscribeAuth = authAutomat.subscribe(this, (state) => ({
|
||||
selectedCompanyId: state.user?.selectedCompanyId ?? null,
|
||||
}));
|
||||
this.setState(mapCompanyState(companyAutomat.state));
|
||||
if (companyAutomat.state.status !== 'succeeded') {
|
||||
void companyAutomat.actions.load().catch(() => {});
|
||||
@@ -165,6 +168,7 @@ class CompanySetupDialog extends PureComponent {
|
||||
|
||||
componentWillUnmount() {
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribeAuth?.();
|
||||
}
|
||||
|
||||
async handleSelect(companyId) {
|
||||
@@ -245,6 +249,10 @@ class CompanySetupDialog extends PureComponent {
|
||||
}
|
||||
|
||||
render() {
|
||||
if (authAutomat.state.user?.selectedCompanyId != null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const {
|
||||
companies,
|
||||
loading,
|
||||
|
||||
Reference in New Issue
Block a user