Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

www: dashboard updates #1878

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions packages/www/components/Admin/AdminUsageTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const Index = ({ id }: { id: string }, children) => {
const [toTime, setToTime] = useState("");
const [usage, setUsage] = useState(null);
const [users, setUsers] = useState([]);
const { getUsers, getUsage } = useApi();
const { getUsers, getUsage, getBillingUsage } = useApi();
useEffect(() => {
getUsers(10000)
.then((result) => {
Expand Down Expand Up @@ -49,7 +49,7 @@ const Index = ({ id }: { id: string }, children) => {
setMessage(`Invalid date ${toTime}`);
return;
}
const [res, usage] = await getUsage(ftd, ttd, userId);
const [res, usage] = await getBillingUsage(ftd, ttd, null, null, userId);
if (res.status == 200) {
console.log(`got usage data:`, usage);
setUsage(usage);
Expand Down Expand Up @@ -95,24 +95,43 @@ const Index = ({ id }: { id: string }, children) => {
Get usage
</Button>
</Box>
<Table sx={{ gridTemplateColumns: "auto auto auto auto auto" }}>
<Table
sx={{
border: "1px solid #e0e0e0",
gridTemplateColumns: "auto auto auto auto auto",
gap: "1rem",
}}>
<>
<TableRow variant={TableRowVariant.Header} key="usage header">
<TableRow
variant={TableRowVariant.Header}
key="usage header"
sx={{ background: "#f7f7f7", fontWeight: "bold" }}>
<>
<Box></Box>
<Box>Source seconds</Box>
<Box>Transcoded seconds</Box>
<Box>Source segments</Box>
<Box>Transcoded segments</Box>
<Box sx={{ padding: "0.5rem" }}>Category</Box>
<Box sx={{ padding: "0.5rem", textAlign: "center" }}>
Transcoding
</Box>
<Box sx={{ padding: "0.5rem", textAlign: "center" }}>
Delivery
</Box>
<Box sx={{ padding: "0.5rem", textAlign: "center" }}>Storage</Box>
</>
</TableRow>
<TableRow key="just one row for now" variant={TableRowVariant.Normal}>
<TableRow
key="just one row for now"
variant={TableRowVariant.Normal}
sx={{ borderBottom: "1px solid #e0e0e0" }}>
<>
<Box></Box>
<Box>{dur2str(usage && usage.sourceSegmentsDuration)}</Box>
<Box>{dur2str(usage && usage.transcodedSegmentsDuration)}</Box>
<Box>{usage && usage.sourceSegments}</Box>
<Box>{usage && usage.transcodedSegments}</Box>
<Box sx={{ padding: "0.5rem" }}>Values</Box>
<Box sx={{ padding: "0.5rem", textAlign: "right" }}>
{usage && usage.TotalUsageMins}
</Box>
<Box sx={{ padding: "0.5rem", textAlign: "right" }}>
{usage && usage.DeliveryUsageMins}
</Box>
<Box sx={{ padding: "0.5rem", textAlign: "right" }}>
{usage && usage.StorageUsageMins}
</Box>
</>
</TableRow>
</>
Expand Down
43 changes: 42 additions & 1 deletion packages/www/components/Admin/UserTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@ const UserTable = ({ userId, id }: UserTableProps) => {
const [removeAdminModal, setRemoveAdminModal] = useState(false);
const [suspendModal, setSuspendModal] = useState(false);
const [unsuspendModal, setUnsuspendModal] = useState(false);
const [enterpriseModal, setEnterpriseModal] = useState(false);
const [nextCursor, setNextCursor] = useState("");
const [lastCursor, setLastCursor] = useState("");
const [lastOrder, setLastOrder] = useState("");
const [lastFilters, setLastFilters] = useState([]);
const [loading, setLoading] = useState(true);
const [loadingError, setLoadingError] = useState("");

const { getUsers, makeUserAdmin, setUserSuspended } = useApi();
const { getUsers, makeUserAdmin, setUserSuspended, makeUserEnterprise } =
useApi();

const close = () => {
setAdminModal(false);
setRemoveAdminModal(false);
setSuspendModal(false);
setUnsuspendModal(false);
setEnterpriseModal(false);
};

const getProductName = (productId) =>
Expand Down Expand Up @@ -208,6 +211,34 @@ const UserTable = ({ userId, id }: UserTableProps) => {
</Flex>
</Modal>
)}
{enterpriseModal && selectedUser && (
<Modal onClose={close}>
<h3>Make User Enterprise</h3>
<p>
Are you sure you want to subscribe the user "{selectedUser.email}"
to the Enterprise plan?
</p>
<Flex sx={{ justifyContent: "flex-end" }}>
<Button
type="button"
variant="outlineSmall"
onClick={close}
sx={{ mr: 2 }}>
Cancel
</Button>
<Button
type="button"
variant="primarySmall"
onClick={() => {
makeUserEnterprise(selectedUser.id)
.then(refetch)
.finally(close);
}}>
Make User Enterprise
</Button>
</Flex>
</Modal>
)}
{removeAdminModal && selectedUser && (
<Modal onClose={close}>
<h3>Remove Admin Rights</h3>
Expand Down Expand Up @@ -276,6 +307,16 @@ const UserTable = ({ userId, id }: UserTableProps) => {
onClick={() => selectedUser && setUnsuspendModal(true)}>
Unsuspend User
</Button>
<Button
variant="outlineSmall"
disabled={
!selectedUser ||
selectedUser.stripeProductId == "prod_OTTbwpzxNLMNSh"
}
sx={{ ml: "1em" }}
onClick={() => selectedUser && setEnterpriseModal(true)}>
Make enterprise
</Button>
</CommonAdminTable>
</Container>
);
Expand Down
24 changes: 23 additions & 1 deletion packages/www/hooks/use-api/endpoints/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,16 @@ export const getBillingUsage = async (
fromTime: number,
toTime: number,
creatorId?: number,
timeStep?: string
timeStep?: string,
userId?: string
): Promise<[Response, BillingUsageData | ApiError]> => {
let [res, usage] = await context.fetch(
`/data/usage/query?${qs.stringify({
from: fromTime,
to: toTime,
creatorId,
timeStep,
userId,
})}`,
{}
);
Expand Down Expand Up @@ -277,6 +279,26 @@ export const makeUserAdmin = async (
return res;
};

export const makeUserEnterprise = async (
userId
): Promise<[Response, User | ApiError]> => {
const [res, body] = await context.fetch("/stripe/user/subscribe/enterprise", {
method: "POST",
body: JSON.stringify({ userId: userId, actually_migrate: true }),
headers: {
"content-type": "application/json",
},
});

setState((state) => ({ ...state, userRefresh: Date.now() }));

if (res.status !== 201) {
return body;
}

return res;
};

export const setUserSuspended = async (
userId: string,
payload: SuspendUserPayload
Expand Down