Skip to content

Commit

Permalink
Merge branch 'master' into update/create-org-flow
Browse files Browse the repository at this point in the history
  • Loading branch information
tobi-bams committed Nov 13, 2023
2 parents a97ac40 + a481dac commit 40256c4
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 71 deletions.
2 changes: 1 addition & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ func (db database) CreateOrEditOrganization(m Organization) (Organization, error
func (db database) GetOrganizationUsers(uuid string) ([]OrganizationUsersData, error) {
ms := []OrganizationUsersData{}

err := db.db.Raw(`SELECT org.org_uuid, org.created as user_created, person.* FROM public.organization_users AS org LEFT OUTER JOIN public.people AS person ON org.owner_pub_key = person.owner_pub_key WHERE org.org_uuid = '` + uuid + `' ORDER BY org.created DESC`).Find(&ms).Error
err := db.db.Raw(`SELECT org.org_uuid, org.created as user_created, person.* FROM public.organization_users AS org LEFT OUTER JOIN public.people AS person ON org.owner_pub_key = person.owner_pub_key WHERE org.org_uuid = '` + uuid + `' OR org.organization = '` + uuid + `' ORDER BY org.created DESC`).Find(&ms).Error

return ms, err
}
Expand Down
44 changes: 29 additions & 15 deletions frontend/app/src/people/widgetViews/OrganizationDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,31 @@ const OrganizationDetails = (props: {
name: role.name,
status: false
}));

setBountyRolesData(bountyRolesData);
}, [main.bountyRoles]);

const getUserRoles = async (user: any) => {
if (uuid && user.owner_pubkey) {
const userRoles = await main.getUserRoles(uuid, user.owner_pubkey);
setUserRoles(userRoles);
const getUserRoles = useCallback(
async (user: any) => {
if (uuid && user.owner_pubkey) {
const userRoles = await main.getUserRoles(uuid, user.owner_pubkey);
setUserRoles(userRoles);

// set all values to false, so every user data will be fresh
const rolesData = bountyRolesData.map((data: any) => ({ name: data.name, status: false }));
// set all values to false, so every user data will be fresh
const rolesData = bountyRolesData.map((data: any) => ({ name: data.name, status: false }));
userRoles.forEach((userRole: any) => {
const index = rolesData.findIndex((role: any) => role.name === userRole.role);

userRoles.forEach((userRole: any) => {
const index = rolesData.findIndex((role: any) => role.name === userRole.role);
rolesData[index]['status'] = true;
});
if (index !== -1) {
rolesData[index]['status'] = true;
}
});

setBountyRolesData(rolesData);
}
};
setBountyRolesData(rolesData);
}
},
[uuid, main]
);

const getOrganizationBudget = useCallback(async () => {
if (!viewReportDisabled) {
Expand Down Expand Up @@ -250,7 +256,6 @@ const OrganizationDetails = (props: {
budget: org.budget
};

console.log(newOrg);
const res = await main.updateOrganization(newOrg);
if (res.status === 200) {
addToast('Sucessfully updated organization', 'success');
Expand Down Expand Up @@ -366,7 +371,16 @@ const OrganizationDetails = (props: {
getBountyRoles();
getOrganizationBudget();
getPaymentsHistory();
}, [getOrganizationUsers, getBountyRoles, getOrganizationBudget, getPaymentsHistory]);
if (uuid && ui.meInfo) {
getUserRoles(ui.meInfo);
}
}, [
getOrganizationUsers,
getBountyRoles,
getOrganizationBudget,
getPaymentsHistory,
getUserRoles
]);

return (
<Container>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useCallback, useEffect, useState } from 'react';
import { EuiText, EuiFieldText, EuiGlobalToastList } from '@elastic/eui';
import { observer } from 'mobx-react-lite';
import moment from 'moment';
import { calculateTimeLeft, isInvoiceExpired } from 'helpers';
import { isInvoiceExpired, userHasRole } from 'helpers';
import { SOCKET_MSG, createSocketInstance } from 'config/socket';
import { Button, Divider, Modal } from '../../../../components/common';
import { colors } from '../../../../config/colors';
Expand Down Expand Up @@ -37,8 +37,7 @@ import {
TitleBox,
CodingLabels,
AutoCompleteContainer,
AwardBottomContainer,
BountyTime
AwardBottomContainer
} from './style';
import { getTwitterLink } from './lib';

Expand Down Expand Up @@ -90,7 +89,6 @@ function MobileView(props: CodingBountiesProps) {
createdURL,
created,
loomEmbedUrl,
bounty_expires,
commitment_fee,
org_uuid,
id,
Expand All @@ -105,6 +103,10 @@ function MobileView(props: CodingBountiesProps) {
const [lnInvoice, setLnInvoice] = useState('');
const [toasts, setToasts]: any = useState([]);
const [updatingPayment, setUpdatingPayment] = useState<boolean>(false);
const [userHasPayRole, setUserHasPayRole] = useState(false);
const [canEdit, setCanEdit] = useState(true);

const userPubkey = ui.meInfo?.owner_pubkey;

let bountyPaid = paid || invoiceStatus || keysendStatus;

Expand All @@ -116,11 +118,6 @@ function MobileView(props: CodingBountiesProps) {

const pollMinutes = 2;

const bountyExpired = !bounty_expires
? false
: Date.now() > new Date(bounty_expires || '').getTime();
const bountyTimeLeft = calculateTimeLeft(new Date(bounty_expires ?? ''), 'days');

const addToast = (type: string) => {
switch (type) {
case SOCKET_MSG.invoice_success: {
Expand Down Expand Up @@ -333,11 +330,40 @@ function MobileView(props: CodingBountiesProps) {
};
}, []);

const getOrganization = useCallback(async () => {
if (org_uuid && userPubkey) {
const userRoles = await main.getUserRoles(org_uuid, userPubkey);
const organization = await main.getUserOrganizationByUuid(org_uuid);

if (organization) {
const isOrganizationAdmin = organization.owner_pubkey === userPubkey;

const canPayBounty =
isOrganizationAdmin || userHasRole(main.bountyRoles, userRoles, 'PAY BOUNTY');

if (!isOrganizationAdmin) {
setCanEdit(false);
}

setUserHasPayRole(canPayBounty);
}
}
}, [main, org_uuid, userPubkey]);

useEffect(() => {
getOrganization();
}, [getOrganization]);

const isOwner =
{ ...person }?.owner_alias &&
ui.meInfo?.owner_alias &&
{ ...person }?.owner_alias === ui.meInfo?.owner_alias;
const hasAccess = isOwner || userHasPayRole;
const payBountyDisable = !isOwner && !userHasPayRole;

return (
<div>
{{ ...person }?.owner_alias &&
ui.meInfo?.owner_alias &&
{ ...person }?.owner_alias === ui.meInfo?.owner_alias ? (
{hasAccess ? (
/*
* creator view
*/
Expand Down Expand Up @@ -422,33 +448,35 @@ function MobileView(props: CodingBountiesProps) {
<div className="CreatorDescriptionOuterContainerCreatorView">
<div className="CreatorDescriptionInnerContainerCreatorView">
<div>{nametag}</div>
<div className="CreatorDescriptionExtraButton">
<ImageButton
buttonText={'Edit'}
ButtonContainerStyle={{
width: '117px',
height: '40px'
}}
leadingImageSrc={'/static/editIcon.svg'}
leadingImageContainerStyle={{
left: 320
}}
buttonAction={props?.editAction}
/>
<ImageButton
buttonText={!props.deletingState ? 'Delete' : 'Deleting'}
ButtonContainerStyle={{
width: '117px',
height: '40px'
}}
leadingImageSrc={'/static/Delete.svg'}
leadingImageContainerStyle={{
left: 450
}}
disabled={!props?.deleteAction}
buttonAction={props?.deleteAction}
/>
</div>
{canEdit && (
<div className="CreatorDescriptionExtraButton">
<ImageButton
buttonText={'Edit'}
ButtonContainerStyle={{
width: '117px',
height: '40px'
}}
leadingImageSrc={'/static/editIcon.svg'}
leadingImageContainerStyle={{
left: 320
}}
buttonAction={props?.editAction}
/>
<ImageButton
buttonText={!props.deletingState ? 'Delete' : 'Deleting'}
ButtonContainerStyle={{
width: '117px',
height: '40px'
}}
leadingImageSrc={'/static/Delete.svg'}
leadingImageContainerStyle={{
left: 450
}}
disabled={!props?.deleteAction}
buttonAction={props?.deleteAction}
/>
</div>
)}
</div>
<TitleBox color={color}>{titleString}</TitleBox>
<LanguageContainer>
Expand Down Expand Up @@ -604,15 +632,8 @@ function MobileView(props: CodingBountiesProps) {
*/}
{!invoiceStatus && assignee && assignee.owner_alias.length < 30 && (
<>
{bounty_expires && !bountyExpired && (
<BountyTime>
Bounty time remains: Days {bountyTimeLeft.days} Hrs{' '}
{bountyTimeLeft.hours} Mins {bountyTimeLeft.minutes} Secs{' '}
{bountyTimeLeft.seconds}
</BountyTime>
)}
{bountyExpired && <BountyTime>Bounty commitment has expired</BountyTime>}
<Button
disabled={payBountyDisable}
iconSize={14}
width={220}
height={48}
Expand Down
21 changes: 21 additions & 0 deletions frontend/app/src/store/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,27 @@ export class MainStore {
}
}

async getUserOrganizationByUuid(uuid: string): Promise<Organization | undefined> {
try {
const info = uiStore;
if (!info.selectedPerson && !uiStore.meInfo?.id) return undefined;

const r: any = await fetch(`${TribesURL}/organizations/${uuid}`, {
method: 'GET',
mode: 'cors',
headers: {
'Content-Type': 'application/json'
}
});

const data = await r.json();
return await data;
} catch (e) {
console.log('Error getOrganizationByUuid', e);
return undefined;
}
}

@action async addOrganization(body: { name: string; img: string }): Promise<any> {
try {
if (!uiStore.meInfo) return null;
Expand Down
Loading

0 comments on commit 40256c4

Please sign in to comment.