Skip to content

Commit

Permalink
Merge pull request #426 from factly/feat/meiliReindex
Browse files Browse the repository at this point in the history
Feat/meili reindex
  • Loading branch information
shreeharsha-factly authored Aug 23, 2021
2 parents 97bc7e2 + 8f71ce9 commit 22e16d9
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 15 deletions.
2 changes: 1 addition & 1 deletion server/service/reindex/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func Router() chi.Router {
r := chi.NewRouter()

r.With(middlewarex.CheckSuperOrganisation("dega", util.GetOrganisation)).Post("/all", all)
r.Post("/space", space)
r.Post("/space/{space_id}", space)

return r
}
21 changes: 12 additions & 9 deletions server/service/reindex/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ import (
"fmt"
"log"
"net/http"
"strconv"

"github.com/factly/dega-server/config"
"github.com/factly/dega-server/service/core/model"
"github.com/factly/dega-server/util"
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/meilisearchx"
"github.com/factly/x/middlewarex"
"github.com/factly/x/renderx"
"github.com/go-chi/chi"
"github.com/meilisearch/meilisearch-go"
)

func space(w http.ResponseWriter, r *http.Request) {

oID, err := util.GetOrganisation(r.Context())
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.Unauthorized()))
return
}
spaceID := chi.URLParam(r, "space_id")
sID, err := strconv.Atoi(spaceID)

uID, err := middlewarex.GetUser(r.Context())
if err != nil {
Expand All @@ -30,14 +30,17 @@ func space(w http.ResponseWriter, r *http.Request) {
return
}

err = util.CheckSpaceKetoPermission("create", uint(oID), uint(uID))
space := model.Space{}
space.ID = uint(sID)

err = config.DB.Model(&model.Space{}).First(&space).Error
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.Unauthorized()))
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}

sID, err := middlewarex.GetSpace(r.Context())
err = util.CheckSpaceKetoPermission("create", uint(space.OrganisationID), uint(uID))
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.Unauthorized()))
Expand Down
29 changes: 29 additions & 0 deletions studio/src/actions/meiliReindex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import axios from 'axios';
import { MEILI_REINDEX_API } from '../constants/meiliReindex';
import { addErrorNotification, addSuccessNotification } from './notifications';
import getError from '../utils/getError';

export const reindexSpace = (id) => {
return (dispatch) => {
return axios
.post(MEILI_REINDEX_API + '/space/' + id)
.then((response) => {
if (response.status === 200) dispatch(addSuccessNotification('Successfully Reindexed'));
})
.catch((error) => {
dispatch(addErrorNotification(getError(error)));
});
};
};
export const reindex = () => {
return (dispatch) => {
return axios
.post(MEILI_REINDEX_API + '/all')
.then((response) => {
if (response.status === 200) dispatch(addSuccessNotification('Successfully Reindexed'));
})
.catch((error) => {
dispatch(addErrorNotification(getError(error)));
});
};
};
8 changes: 8 additions & 0 deletions studio/src/config/routesConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ import EditWebhook from '../pages/webhooks/EditWebhook';
//profile
import Profile from '../pages/profile';

//Reindex
import Reindex from '../pages/spaces/Reindex';

const routes = {
dashboard: {
path: '/dashboard',
Expand Down Expand Up @@ -162,6 +165,11 @@ const routes = {
action: 'update',
},
},
reindex: {
path: '/reindex',
Component: Reindex,
title: 'Reindex',
},
episodes: {
path: '/episodes',
Component: Episodes,
Expand Down
2 changes: 2 additions & 0 deletions studio/src/constants/meiliReindex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//API
export const MEILI_REINDEX_API = '/reindex';
143 changes: 143 additions & 0 deletions studio/src/pages/spaces/Reindex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import React from 'react';
import { Row, Col, Button, Form, Select, Typography, Space } from 'antd';
import { useSelector, useDispatch } from 'react-redux';
import { reindex, reindexSpace } from '../../actions/meiliReindex';

const Reindex = () => {
const { Title } = Typography;

const [form] = Form.useForm();
const dispatch = useDispatch();
const [spaceOption, setSpaceOption] = React.useState();
const [status, setStatus] = React.useState();

const superOrg = useSelector(({ admin }) => {
return admin.organisation;
});

const { orgId, orgs, spaces } = useSelector((state) => {
const { selected, orgs, loading } = state.spaces;
if (selected > 0) {
const space = state.spaces.details[selected];
const orgId = space.organisation_id;
const org = orgs.find((org) => org.id === orgId);
const role = org.permission.role;

return {
loading: loading,
role: role,
orgId: orgId,
spaces: state.spaces.details,
orgs: superOrg.is_admin ? state.spaces.orgs : [org],
};
}
return { orgId: 0, orgs: [], spaces: [] };
});
const [organisationId, setOrganisationId] = React.useState(orgId);

React.useEffect(() => {
if (orgs.length > 0) {
const org = orgs.find((org) => org.id === organisationId);
const newSpaces = org.spaces.map((space) => spaces[space]);
setSpaceOption(newSpaces);
}
}, [organisationId]);

const handleorgChange = (orgId) => {
form.setFieldsValue({
space_id: null,
});
setOrganisationId(orgId);
};
const handleReindex = () => {
dispatch(reindex());
};
const handleSpaceReindex = (id) => {
if (id) {
dispatch(reindexSpace(id));
} else {
setStatus('error');
}
};
return (
<Space direction="vertical">
<Title level={3}>Reindex</Title>
<Form form={form} name="reindex" layout="vertical">
<Row justify="center" style={{ maxWidth: '1200px', width: '100%', margin: '0 auto' }}>
<Col span={24}>
<Row
gutter={40}
justify="space-around"
style={{ background: '#f0f2f5', padding: '1.25rem', marginBottom: '1rem' }}
>
<Col span={12}>
{orgs && orgs.length > 0 ? (
<Row gutter={40}>
<Col md={{ span: 16 }}>
<Form.Item name="organisation_id" label="Organisation">
<Select
allowClear
bordered
listHeight={128}
defaultValue={[orgId]}
onChange={(value) => handleorgChange(value)}
>
{orgs.map((item) => (
<Select.Option value={item.id} key={item.id}>
{item.title}
</Select.Option>
))}
</Select>
</Form.Item>
</Col>
</Row>
) : null}

{spaceOption && spaceOption.length > 0 ? (
<Row gutter={40}>
<Col md={{ span: 16 }}>
<Form.Item name="space_id" label="Space" validateStatus={status}>
<Select
allowClear
bordered
listHeight={128}
defaultValue={[]}
placeholder="Select Space"
onChange={() => setStatus()}
>
{spaceOption.map((item) => (
<Select.Option value={item.id} key={item.id}>
{item.name}
</Select.Option>
))}
</Select>
</Form.Item>
</Col>
<Col
md={{ span: 5 }}
style={{ alignItems: 'center', justifyContent: 'center', display: 'flex' }}
>
<Button
type="primary"
onClick={() => handleSpaceReindex(form.getFieldValue('space_id'))}
>
Reindex
</Button>
</Col>
</Row>
) : null}
{superOrg.is_admin ? (
<Button type="primary" onClick={() => handleReindex()}>
Reindex Instance
</Button>
) : null}
</Col>
</Row>
</Col>
</Row>
</Form>
</Space>
);
};

export default Reindex;
34 changes: 29 additions & 5 deletions studio/src/pages/spaces/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
import React from 'react';
import { Space, Button, Row } from 'antd';
import { Space, Button, Row, Col } from 'antd';
import { Link } from 'react-router-dom';
import SpaceList from './components/SpaceList';
import { useSelector } from 'react-redux';

function Spaces() {
const { role } = useSelector((state) => {
const { selected, orgs } = state.spaces;

if (selected > 0) {
const space = state.spaces.details[selected];
const orgId = space.organisation_id;
const org = orgs.find((org) => org.id === orgId);
const role = org.permission.role;
return {
role: role,
};
}
return { role: 'member' };
});

return (
<Space direction="vertical">
<Row gutter={16} justify="end">
<Link key="1" to="/spaces/create">
<Button type="primary">New Space</Button>
</Link>
{role === 'owner' ? (
<Col>
<Link key="2" to="/reindex">
<Button>Reindex</Button>
</Link>
</Col>
) : null}
<Col>
<Link key="1" to="/spaces/create">
<Button type="primary">New Space</Button>
</Link>
</Col>
</Row>

<SpaceList />
</Space>
);
Expand Down

0 comments on commit 22e16d9

Please sign in to comment.