Skip to content

Commit

Permalink
fixed issue with adding to collection from basket, fixed basket resto…
Browse files Browse the repository at this point in the history
…re bug
  • Loading branch information
danielfang97 committed Jun 17, 2024
1 parent 570de43 commit 0ccd05a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 24 deletions.
3 changes: 2 additions & 1 deletion frontend/components/Basket/Basket.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const { publicRuntimeConfig } = getConfig();
*/
export default function Basket() {
const [showBasket, setShowBasket] = useState(false);
const token = useSelector(state => state.user.token);
const basketItems = useSelector(state => state.basket.basket);
const dispatch = useDispatch();
const [selected, setSelected] = useState(new Map());
Expand All @@ -40,7 +41,7 @@ export default function Basket() {
const theme = JSON.parse(localStorage.getItem('theme')) || {};

useEffect(() => {
dispatch(restoreBasket());
dispatch(restoreBasket(token));
}, []);

useEffect(() => {
Expand Down
3 changes: 3 additions & 0 deletions frontend/components/Basket/CreateCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
} from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { useDispatch, useSelector } from 'react-redux';
import { useState } from 'react';

import { getCanSubmitTo } from '../../redux/actions';
import styles from '../../styles/basket.module.css';
Expand All @@ -12,6 +13,7 @@ import SubmissionStatusPanel from '../Submit/SubmissionStatusPanel';
import SubmitButton from '../Submit/SubmitButton';

export default function CreateCollection(properties) {
const [selectedHandler, setSelectedHandler] = useState({value: 'default', label: 'Default Handler'});
const showSubmitProgress = useSelector(
state => state.submit.showSubmitProgress
);
Expand Down Expand Up @@ -49,6 +51,7 @@ export default function CreateCollection(properties) {
files={properties.itemsToAddToCollection}
overwriteCollection={false}
addingToCollection={true}
submitHandler={selectedHandler}
/>
</div>
)}
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/Submit/SubmitButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function SubmitButton(properties) {
alert(
'You must select one or more files and a destination collection before you can submit.'
);
else if (properties.submitHandler.value === 'configure') {
else if (properties.submitHandler && properties.submitHandler.value === 'configure') {
properties.configure()
}
else {
Expand Down
2 changes: 1 addition & 1 deletion frontend/public/commitHash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4b214d663d2a8faf784303aee3e886818e3c73e8
570de43852e519fbbfe2b65ec71ec3915e7240a3
35 changes: 14 additions & 21 deletions frontend/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { saveAs } from 'file-saver';
import JSZip from 'jszip';
import getConfig from 'next/config';
import { mutate } from 'swr';
import { useSelector } from 'react-redux';

import * as types from './types';
const { publicRuntimeConfig } = getConfig();
Expand Down Expand Up @@ -1145,11 +1146,11 @@ export const addToBasket = items => async (dispatch, getState) => {
localStorage.setItem('basket', JSON.stringify(newBasket));
};

export const restoreBasket = () => dispatch => {
export const restoreBasket = (token) => dispatch => {
const basket = JSON.parse(localStorage.getItem('basket'));
if (basket) {
for (let i = 0; i < basket.length; i++) {
if (!checkUriExists(basket[i].uri)) {
if (!checkUriExists(basket[i].url, token)) {
console.log("item not exist");
dispatch(clearBasket(item));
}
Expand Down Expand Up @@ -1181,33 +1182,25 @@ export const clearBasket = itemsToClear => (dispatch, getState) => {
localStorage.setItem('basket', JSON.stringify(newBasket));
};

export async function checkUriExists(uri) {
const query = `SELECT ?subject ?predicate ?object
WHERE {
<${uri}> ?predicate ?object .
BIND(<${uri}> AS ?subject)
}`;
const url = `${publicRuntimeConfig.backend}/sparql?query=${encodeURIComponent(query)}`;
async function checkUriExists(url, token) {

try {
const response = await fetch(url, {
headers: { 'Accept': 'application/sparql-results+json' }
});
const uri = `${publicRuntimeConfig.backend}${url}`;

if (!response.ok) {
console.error(`HTTP error! status: ${response.status}`);
return false;
}
const headers = {
'Accept': 'application/sparql-results+json',
'X-authorization': token
};

try {
const response = await axios.get(uri, { headers });

const data = await response.json();
const data = await response.data;

// Check if there are any bindings in the results
const bindings = data.results.bindings;
const bindings = data;
if (bindings && bindings.length > 0) {
console.log("URI exists:", bindings);
return true; // URI exists
} else {
console.log("URI does not exist.");
return false; // URI does not exist
}
} catch (error) {
Expand Down

0 comments on commit 0ccd05a

Please sign in to comment.