Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit dd0ad58

Browse files
authored
Create isAdmin bool value from cookie to avoid bug (#24)
1 parent 8911d48 commit dd0ad58

File tree

6 files changed

+45
-27
lines changed

6 files changed

+45
-27
lines changed

webportal_plugin/src/app/context.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export default React.createContext({
66
api: null,
77
user: null,
88
token: null,
9+
isAdmin: false,
910
history: null,
1011
});

webportal_plugin/src/app/index.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import MarketList from '../app/market_list';
99
import MarketDetail from '../app/market_detail';
1010

1111
const App = props => {
12-
const { api, user, token } = props;
12+
const { api, user, token, isAdmin } = props;
1313

1414
return (
1515
<Fabric style={{ height: '100%' }}>
@@ -22,6 +22,7 @@ const App = props => {
2222
api={api}
2323
user={user}
2424
token={token}
25+
isAdmin={isAdmin}
2526
routeProps={props}
2627
/>
2728
)}
@@ -33,6 +34,7 @@ const App = props => {
3334
api={api}
3435
user={user}
3536
token={token}
37+
isAdmin={isAdmin}
3638
routeProps={props}
3739
/>
3840
)}
@@ -46,6 +48,7 @@ App.propTypes = {
4648
api: PropTypes.string,
4749
user: PropTypes.string,
4850
token: PropTypes.string,
51+
isAdmin: PropTypes.bool,
4952
};
5053

5154
export default App;

webportal_plugin/src/app/market_detail/components/summary.jsx

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ const { spacing } = getTheme();
3434

3535
export default function Summary(props) {
3636
const { marketItem } = props;
37-
const { user } = useContext(Context);
38-
const admin = cookies.get('admin');
37+
const { user, isAdmin } = useContext(Context);
3938

4039
const [hideDialog, setHideDialog] = useState(true);
4140
const [hideApproveDialog, setHideApproveDialog] = useState(true);
@@ -56,6 +55,10 @@ export default function Summary(props) {
5655
fetchStarRelationWrapper();
5756
}, []);
5857

58+
const checkAuthorAdmin = useCallback(() => {
59+
return isAdmin || user === marketItem.author;
60+
});
61+
5962
const clickStar = useCallback(async () => {
6063
if (stared) {
6164
await deleteStar(user, marketItem.id);
@@ -190,24 +193,28 @@ export default function Summary(props) {
190193
}}
191194
onClick={clickSubmit}
192195
/>
193-
<DefaultButton
194-
text='Edit'
195-
styles={{
196-
root: {
197-
fontSize: 14,
198-
fontWeight: FontWeights.regular,
199-
},
200-
}}
201-
onClick={e => {
202-
setHideDialog(false);
203-
}}
204-
/>
205-
<EditMarketItem
206-
hideDialog={hideDialog}
207-
setHideDialog={setHideDialog}
208-
marketItem={marketItem}
209-
/>
210-
{admin && (
196+
{checkAuthorAdmin() && (
197+
<Stack>
198+
<DefaultButton
199+
text='Edit'
200+
styles={{
201+
root: {
202+
fontSize: 14,
203+
fontWeight: FontWeights.regular,
204+
},
205+
}}
206+
onClick={e => {
207+
setHideDialog(false);
208+
}}
209+
/>
210+
<EditMarketItem
211+
hideDialog={hideDialog}
212+
setHideDialog={setHideDialog}
213+
marketItem={marketItem}
214+
/>
215+
</Stack>
216+
)}
217+
{checkAuthorAdmin() && (
211218
<Stack>
212219
<DefaultButton
213220
text='Delete'

webportal_plugin/src/app/market_detail/index.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { SpinnerLoading } from 'App/components/loading';
1919
import { getItemById } from 'App/utils/marketplace_api';
2020

2121
const MarketDetail = props => {
22-
const { api, user, token, routeProps } = props;
22+
const { api, user, token, isAdmin, routeProps } = props;
2323

2424
const [loading, setLoading] = useState(true);
2525
const [marketItem, setMarketItem] = useState(null);
@@ -44,6 +44,7 @@ const MarketDetail = props => {
4444
user,
4545
api,
4646
token,
47+
isAdmin,
4748
history: routeProps.history,
4849
};
4950

@@ -67,6 +68,7 @@ MarketDetail.propTypes = {
6768
api: PropTypes.string,
6869
user: PropTypes.string,
6970
token: PropTypes.string,
71+
isAdmin: PropTypes.bool,
7072
routeProps: PropTypes.object,
7173
};
7274

webportal_plugin/src/app/market_list/index.jsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ import ListView from './list_view';
1717
import { getPendingItems, ensureUser } from 'App/utils/marketplace_api';
1818

1919
const MarketList = props => {
20-
const { api, user, token, routeProps } = props;
21-
const admin = cookies.get('admin');
20+
const { api, user, token, isAdmin, routeProps } = props;
2221

2322
const [status, setStatus] = useState(initStatus());
2423
const [pendingListNumber, setPendingListNumber] = useState(0);
2524

2625
function initStatus() {
2726
const status = qs.parse(routeProps.location.search).status;
28-
if (!admin) {
27+
if (!isAdmin) {
2928
return 'approved';
3029
}
3130
if (isNil(status)) {
@@ -41,6 +40,7 @@ const MarketList = props => {
4140
api,
4241
user,
4342
token,
43+
isAdmin,
4444
history: routeProps.history,
4545
};
4646

@@ -82,7 +82,7 @@ const MarketList = props => {
8282
>
8383
<ListView status={status} />
8484
</PivotItem>
85-
{admin && (
85+
{isAdmin && (
8686
<PivotItem
8787
itemKey='pending'
8888
headerText='Pending list'
@@ -103,6 +103,7 @@ MarketList.propTypes = {
103103
api: PropTypes.string,
104104
user: PropTypes.string,
105105
token: PropTypes.string,
106+
isAdmin: PropTypes.bool,
106107
routeProps: PropTypes.object,
107108
};
108109

webportal_plugin/src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ class PAIMarketplacePluginElement extends HTMLElement {
1111
const api = this.getAttribute('pai-rest-server-uri');
1212
const user = this.getAttribute('pai-user');
1313
const token = this.getAttribute('pai-rest-server-token');
14+
const isAdmin = cookies.get('admin') === 'true';
1415
if (user === null || token === null) {
1516
window.location.href = '/login.html';
1617
return;
1718
}
18-
ReactDOM.render(React.createElement(App, { api, user, token }), this);
19+
ReactDOM.render(
20+
React.createElement(App, { api, user, token, isAdmin }),
21+
this,
22+
);
1923
}
2024

2125
disconnectedCallback() {

0 commit comments

Comments
 (0)