Skip to content

Commit

Permalink
Added set default pfp
Browse files Browse the repository at this point in the history
  • Loading branch information
macterra committed Nov 5, 2023
1 parent 00a1088 commit 6dd5c49
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
30 changes: 28 additions & 2 deletions frontend/src/PfpEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ import React, { useState, useEffect } from 'react';
import { Button } from '@mui/material';
import axios from 'axios';

const PfpEditor = ({ metadata, setTab }) => {
const PfpEditor = ({ metadata }) => {
const [profile, setProfile] = useState({});
const [disablePfp, setDisablePfp] = useState(false);
const [showThumbnailButton, setShowThumbnailButton] = useState(true);
const [disableThumbnail, setDisableThumbnail] = useState(false);
const [showDefaultPfpButton, setShowDefaultPfpButton] = useState(true);
const [disableDefaultPfp, setDisableDefaultPfp] = useState(false);

useEffect(() => {
const fetchProfile = async () => {
try {
const check = await axios.get('/check-auth');
const auth = check.data;
const response = await axios.get(`/api/v1/profile`);
const profile = response.data;

setShowDefaultPfpButton(auth.isAdmin);
setProfile(profile);
setShowThumbnailButton(metadata.asset.owner === profile.xid);
} catch (error) {
Expand All @@ -30,7 +36,7 @@ const PfpEditor = ({ metadata, setTab }) => {
setDisablePfp(true);
}
catch (error) {
console.error('Error updating profile:', error);
console.error('Error updating pfp:', error);
}
};

Expand All @@ -47,6 +53,16 @@ const PfpEditor = ({ metadata, setTab }) => {
}
};

const handleDefaultPfp = async () => {
try {
await axios.patch('/api/v1/admin/save', { default_pfp: metadata.file.path });
setDisableDefaultPfp(true);
}
catch (error) {
console.error('Error updating default pfp:', error);
}
};

return (
<div>
<img
Expand Down Expand Up @@ -78,6 +94,16 @@ const PfpEditor = ({ metadata, setTab }) => {
Set Collection Thumbnail
</Button>
}
<br></br>
{showDefaultPfpButton &&
<Button
variant="contained"
color="primary"
onClick={handleDefaultPfp}
disabled={disableDefaultPfp}>
Set Default Pfp
</Button>
}
</form>
</div >
);
Expand Down
23 changes: 23 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,29 @@ app.get('/api/v1/admin/save', ensureAuthenticated, async (req, res) => {
}
});

app.patch('/api/v1/admin/save', ensureAuthenticated, async (req, res) => {
const { default_pfp } = req.body;

try {
const adminData = admin.getAdmin();

if (!adminData.owner || adminData.owner !== req.user.xid) {
return res.status(401).json({ message: 'Unauthorized' });
}

if (default_pfp) {
adminData.default_pfp = default_pfp;
}

const savedAdmin = await admin.saveAdmin(adminData);
adminData.githash = await archiver.commitChanges({ type: 'save-state', agent: req.user.xid, state: savedAdmin.xid });
res.json(savedAdmin);
} catch (error) {
console.error('Error reading metadata:', error);
res.status(404).json({ message: 'Asset not found' });
}
});

app.get('/api/v1/admin/register', ensureAuthenticated, async (req, res) => {
try {
const adminData = admin.getAdmin();
Expand Down

0 comments on commit 6dd5c49

Please sign in to comment.