mirror of
https://github.com/muerwre/orchidmap-front.git
synced 2025-04-25 02:56:41 +07:00
enabling / disabling editor from url
This commit is contained in:
parent
b35a063cd7
commit
e3e209ff65
5 changed files with 70 additions and 38 deletions
|
@ -9,7 +9,7 @@ import { DEFAULT_USER } from '$constants/auth';
|
|||
import { getGuestToken, checkUserToken, getStoredMap } from '$utils/api';
|
||||
import { storeData, getData } from '$utils/storage';
|
||||
import { UserPanel } from '$components/panels/UserPanel';
|
||||
import { getPath } from '$utils/history';
|
||||
import { getUrlData, replacePath } from '$utils/history';
|
||||
|
||||
export class App extends React.Component {
|
||||
state = {
|
||||
|
@ -26,12 +26,34 @@ export class App extends React.Component {
|
|||
|
||||
componentDidMount() {
|
||||
this.authInit();
|
||||
this.mapInit();
|
||||
}
|
||||
|
||||
mapInit = () => {
|
||||
const path = getPath();
|
||||
if (path) getStoredMap({ name: path, callback: this.setDataOnLoad });
|
||||
const { path, mode } = getUrlData();
|
||||
|
||||
if (path) {
|
||||
getStoredMap({ name: path })
|
||||
.then(this.setDataOnLoad)
|
||||
.then(() => {
|
||||
if (mode && mode === 'edit') {
|
||||
this.editor.startEditing();
|
||||
} else {
|
||||
this.editor.stopEditing();
|
||||
}
|
||||
})
|
||||
.catch(this.hideLoader);
|
||||
} else {
|
||||
// this.hideLoader();
|
||||
this.startEmptyEditor();
|
||||
}
|
||||
};
|
||||
|
||||
startEmptyEditor = () => {
|
||||
const { user } = this.state;
|
||||
if (!user || !user.random_url) return;
|
||||
|
||||
replacePath(`/${user.random_url}/edit`);
|
||||
this.hideLoader();
|
||||
};
|
||||
|
||||
setDataOnLoad = data => {
|
||||
|
@ -80,17 +102,18 @@ export class App extends React.Component {
|
|||
const user = this.getUserData();
|
||||
|
||||
const { id, token } = (user || {});
|
||||
const fallback = () => getGuestToken({ callback: this.setUser });
|
||||
|
||||
if (id && token) {
|
||||
checkUserToken({
|
||||
callback: this.setUser,
|
||||
fallback,
|
||||
id,
|
||||
token
|
||||
});
|
||||
})
|
||||
.then(this.setUser)
|
||||
.then(this.mapInit);
|
||||
} else {
|
||||
getGuestToken({ callback: fallback });
|
||||
getGuestToken()
|
||||
.then(this.setUser)
|
||||
.then(this.mapInit);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -181,5 +181,13 @@ export class Editor {
|
|||
}));
|
||||
}
|
||||
this.map.map.fitBounds(this.poly.poly.getBounds());
|
||||
};
|
||||
|
||||
startEditing = () => {
|
||||
this.poly.poly.enableEdit();
|
||||
};
|
||||
|
||||
stopEditing = () => {
|
||||
this.poly.poly.disableEdit();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,25 +4,19 @@ import { API } from '$constants/api';
|
|||
|
||||
const report = console.warn;
|
||||
|
||||
export const checkUserToken = ({
|
||||
callback, fallback, id, token
|
||||
}) => (
|
||||
axios.get(API.CHECK_TOKEN, {
|
||||
params: { action: 'check_token', id, token }
|
||||
})
|
||||
.then(result => (result && result.data))
|
||||
.then(data => ({ ...data, id, token }))
|
||||
.then(callback)
|
||||
.catch(fallback)
|
||||
);
|
||||
export const getGuestToken = ({ callback }) => (
|
||||
axios.get(API.GET_GUEST, {
|
||||
params: { action: 'gen_guest_token' }
|
||||
})
|
||||
.then(result => (result && result.data))
|
||||
.then(callback)
|
||||
.catch(report)
|
||||
);
|
||||
export const checkUserToken = ({ id, token }) => axios.get(API.CHECK_TOKEN, {
|
||||
params: {
|
||||
id,
|
||||
token,
|
||||
action: 'check_token',
|
||||
}
|
||||
}).then(result => (result && result.data && { ...result.data, id, token }))
|
||||
|
||||
export const getGuestToken = () => axios.get(API.GET_GUEST, {
|
||||
params: {
|
||||
action: 'gen_guest_token'
|
||||
}
|
||||
}).then(result => (result && result.data));
|
||||
|
||||
export const getMergedImage = ({ placement, callback }) => (
|
||||
axios.get(API.COMPOSE, {
|
||||
|
@ -32,12 +26,9 @@ export const getMergedImage = ({ placement, callback }) => (
|
|||
.catch(report)
|
||||
);
|
||||
|
||||
export const getStoredMap = ({ name, callback }) => (
|
||||
axios.get(API.GET_MAP, {
|
||||
params: { name, action: 'load' }
|
||||
})
|
||||
.then(result => (result && result.data &&result.data.data))
|
||||
.then(data => ({ ...data }))
|
||||
.then(callback)
|
||||
.catch(report)
|
||||
)
|
||||
export const getStoredMap = ({ name }) => axios.get(API.GET_MAP, {
|
||||
params: {
|
||||
name,
|
||||
action: 'load'
|
||||
}
|
||||
}).then(result => (result && result.data && result.data.data));
|
||||
|
|
|
@ -1,2 +1,12 @@
|
|||
export const getPath = () => (window.location && window.location.pathname &&
|
||||
window.location.pathname.replace(/^\//, ''));
|
||||
|
||||
export const replacePath = url => window.history.replaceState(url, 'Редактирование маршрута', url);
|
||||
|
||||
export const getUrlData = () => {
|
||||
const url = getPath();
|
||||
|
||||
const [path, mode] = url.split('/');
|
||||
|
||||
return { path, mode };
|
||||
};
|
||||
|
|
|
@ -105,10 +105,10 @@ module.exports = () => {
|
|||
resolve,
|
||||
plugins,
|
||||
entry: {
|
||||
// loader: './src/loader.js',
|
||||
app: './src/index.js',
|
||||
},
|
||||
output: {
|
||||
publicPath: '/',
|
||||
filename: '[name].bundle.[githash].js',
|
||||
},
|
||||
optimization: {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue