Skip to content

Commit acd9686

Browse files
fix upload datafile view bug (#663)
Co-authored-by: Aakaash Meduri <aakaash.meduri@gmail.com>
1 parent 5e8635d commit acd9686

File tree

8 files changed

+28
-63
lines changed

8 files changed

+28
-63
lines changed

frontend/app/datafile/add/page.tsx

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,17 @@ import Grid from '@mui/material/Grid'
1818
import Button from '@mui/material/Button'
1919
import Alert from '@mui/material/Alert'
2020
import Typography from '@mui/material/Typography'
21-
// import { useRouter } from 'next/router'
21+
import { useRouter } from 'next/navigation'
2222

2323
export default function DataFileForm() {
2424
const dispatch = useDispatch()
25-
// const router = useRouter()
2625

27-
const cid = useSelector(selectCID)
26+
const router = useRouter()
2827
const errorMessage = useSelector(selectDataFileError)
2928
const isLoading = useSelector(selectDataFileIsLoading)
3029
const walletAddress = useSelector(selectWalletAddress)
3130

3231
const [file, setFile] = useState<File | null>(null)
33-
const [isPublic, setIsPublic] = useState<boolean>(true)
34-
const [isVisible, setIsVisible] = useState<boolean>(true)
3532

3633
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
3734
const uploadedFile = e.target.files && e.target.files[0]
@@ -40,12 +37,8 @@ export default function DataFileForm() {
4037
}
4138
}
4239

43-
const handlePublicChange = (e: React.ChangeEvent<HTMLInputElement>) => {
44-
setIsPublic(!e.target.checked)
45-
}
46-
47-
const handleVisibleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
48-
setIsVisible(!e.target.checked)
40+
const handleSuccess = () => {
41+
router.push('/datafile/list')
4942
}
5043

5144
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
@@ -57,13 +50,11 @@ export default function DataFileForm() {
5750

5851
dispatch(startLoading())
5952
dispatch(setError(null))
60-
const metadata = { walletAddress, isPublic, isVisible };
53+
const metadata = { walletAddress };
6154

6255
try {
63-
await dispatch(saveDataFileAsync({ file, metadata }))
56+
await dispatch(saveDataFileAsync({ file, metadata, handleSuccess }))
6457
dispatch(endLoading())
65-
66-
// router.push('/data/list')
6758
} catch (error) {
6859
dispatch(setError("Error uploading file"))
6960
dispatch(endLoading())
@@ -87,24 +78,6 @@ export default function DataFileForm() {
8778
/>
8879
</Button>
8980
</Grid>
90-
<Grid item container justifyContent="center">
91-
<label>
92-
<input
93-
type="checkbox"
94-
checked={!isPublic}
95-
onChange={handlePublicChange}
96-
/>
97-
File should be private
98-
</label>
99-
<label>
100-
<input
101-
type="checkbox"
102-
checked={!isVisible}
103-
onChange={handleVisibleChange}
104-
/>
105-
File should be hidden
106-
</label>
107-
</Grid>
10881
{errorMessage && (
10982
<Box my={2}>
11083
<Alert severity="error" variant="filled">

frontend/app/datafile/list/page.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ export default function ListDataFiles() {
1515
CID: string;
1616
WalletAddress: string;
1717
Filename: string;
18-
IsPublic: boolean;
19-
IsVisible: boolean;
2018
}
2119

2220
const [datafiles, setDataFiles] = useState<DataFile[]>([]);
@@ -43,8 +41,6 @@ export default function ListDataFiles() {
4341
<TableCell>CID</TableCell>
4442
<TableCell>Uploader Wallet</TableCell>
4543
<TableCell>Filename</TableCell>
46-
<TableCell>Is Public</TableCell>
47-
<TableCell>Is Visible</TableCell>
4844
</TableRow>
4945
</TableHead>
5046
<TableBody>
@@ -57,8 +53,6 @@ export default function ListDataFiles() {
5753
</TableCell>
5854
<TableCell>{datafile.WalletAddress}</TableCell>
5955
<TableCell>{datafile.Filename}</TableCell>
60-
<TableCell>{datafile.IsPublic ? 'Yes' : 'No'}</TableCell>
61-
<TableCell>{datafile.IsVisible ? 'Yes' : 'No'}</TableCell>
6256
</TableRow>
6357
))}
6458
</TableBody>

frontend/app/job/init/page.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ export default function InitJob() {
2020
WalletAddress: string;
2121
Filename: string;
2222
Timestamp: Date;
23-
Public: boolean;
24-
Visible: boolean;
2523
}
2624

2725
const [tools, setTools] = useState<Tool[]>([]);

frontend/lib/redux/slices/dataFileAddSlice/actions.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@ export const saveDataFileToServer = async (
77
const formData = new FormData();
88
formData.append('file', file, file.name);
99
formData.append('filename', file.name)
10-
formData.append('isPublic', String(metadata.isPublic))
11-
formData.append('isVisible', String(metadata.isVisible))
1210

1311
for (const key in metadata) {
1412
formData.append(key, metadata[key]);
1513
}
16-
14+
1715
const response = await fetch(`${backendUrl()}/add-datafile`, {
1816
method: 'POST',
1917
body: formData,
2018
})
21-
19+
2220
if (!response.ok) {
2321
let errorMsg = 'An error occurred while uploading the data file'
2422
try {
@@ -30,7 +28,7 @@ export const saveDataFileToServer = async (
3028
console.log('errorMsg', errorMsg)
3129
throw new Error(errorMsg)
3230
}
33-
31+
3432
const result = await response.json()
3533
console.log('result', result)
3634
return result;

frontend/lib/redux/slices/dataFileAddSlice/thunks.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ import { setCidDataSlice, setFilenameDataSlice, setDataFileError } from './dataS
55
interface DataFilePayload {
66
file: File,
77
metadata: { [key: string]: any }
8+
handleSuccess: () => void
89
}
910

1011
export const saveDataFileAsync = createAppAsyncThunk(
1112
'dataFile/saveDataFile',
12-
async ({ file, metadata }: DataFilePayload, { dispatch }) => {
13+
async ({ file, metadata, handleSuccess }: DataFilePayload, { dispatch }) => {
1314
try {
1415
const response = await saveDataFileToServer(file, metadata);
1516
console.log("Response:", response)
16-
if (response.filename) {
17-
dispatch(setFilenameDataSlice(response.filename));
17+
if (response.cid) {
18+
handleSuccess()
1819
} else {
1920
dispatch(setDataFileError('Failed to save data file.'))
2021
}

gateway/handlers/datafiles.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ import (
1212
"github.com/labdao/plex/gateway/utils"
1313
"github.com/labdao/plex/internal/ipfs"
1414

15+
"log"
16+
1517
"gorm.io/gorm"
1618
)
1719

1820
func AddDataFileHandler(db *gorm.DB) http.HandlerFunc {
1921
return func(w http.ResponseWriter, r *http.Request) {
20-
fmt.Println("Received request at /create-datafile")
22+
log.Println("Received request at /create-datafile")
2123

2224
if err := utils.CheckRequestMethod(r, http.MethodPost); err != nil {
2325
utils.SendJSONError(w, err.Error(), http.StatusBadRequest)
@@ -29,7 +31,7 @@ func AddDataFileHandler(db *gorm.DB) http.HandlerFunc {
2931
utils.SendJSONError(w, "Error parsing multipart form", http.StatusBadRequest)
3032
return
3133
}
32-
fmt.Println("Parsed multipart form")
34+
log.Println("Parsed multipart form")
3335

3436
file, _, err := r.FormFile("file")
3537
if err != nil {
@@ -40,10 +42,8 @@ func AddDataFileHandler(db *gorm.DB) http.HandlerFunc {
4042

4143
walletAddress := r.FormValue("walletAddress")
4244
filename := r.FormValue("filename")
43-
publicBool := r.FormValue("public")
44-
visibleBool := r.FormValue("visible")
4545

46-
fmt.Printf("Received file upload request for file: %s, walletAddress: %s, public: %s, visible: %s\n", filename, walletAddress, publicBool, visibleBool)
46+
log.Printf("Received file upload request for file: %s, walletAddress: %s \n", filename, walletAddress)
4747

4848
tempFile, err := utils.CreateAndWriteTempFile(file, filename)
4949
if err != nil {
@@ -65,13 +65,9 @@ func AddDataFileHandler(db *gorm.DB) http.HandlerFunc {
6565
Timestamp: time.Now(),
6666
}
6767

68-
// if result := db.Create(&dataFile); result.Error != nil {
69-
// utils.SendJSONError(w, fmt.Sprintf("Error saving datafile: %v", result.Error), http.StatusInternalServerError)
70-
// return
71-
// }
72-
7368
result := db.Create(&dataFile)
7469
if result.Error != nil {
70+
log.Println("error saving to DB")
7571
if utils.IsDuplicateKeyError(result.Error) {
7672
utils.SendJSONError(w, "A data file with the same CID already exists", http.StatusConflict)
7773
} else {

gateway/models/datafile.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,4 @@ type DataFile struct {
99
WalletAddress string `gorm:"type:varchar(42);not null"`
1010
Filename string `gorm:"type:varchar(255);not null"`
1111
Timestamp time.Time `gorm:""`
12-
Domain string `gorm:"default:public"`
13-
Visible bool `gorm:"default:true"`
1412
}

gateway/utils/utils.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7+
"log"
78
"net/http"
89
"os"
910
"strings"
@@ -12,12 +13,18 @@ import (
1213
func SendJSONError(w http.ResponseWriter, message string, status int) {
1314
w.Header().Set("Content-Type", "application/json")
1415
w.WriteHeader(status)
15-
json.NewEncoder(w).Encode(map[string]string{"message": message})
16+
if err := json.NewEncoder(w).Encode(map[string]string{"message": message}); err != nil {
17+
log.Printf("Could not encode JSON: %v", err)
18+
}
1619
}
1720

1821
func SendJSONResponseWithCID(w http.ResponseWriter, cid string) {
1922
response := map[string]string{"cid": cid}
20-
jsonResponse, _ := json.Marshal(response)
23+
jsonResponse, err := json.Marshal(response)
24+
if err != nil {
25+
SendJSONError(w, fmt.Sprintf("Error encoding response to JSON: %v", err), http.StatusInternalServerError)
26+
return
27+
}
2128
w.Header().Set("Content-Type", "application/json")
2229
w.Write(jsonResponse)
2330
}

0 commit comments

Comments
 (0)