Skip to content
Snippets Groups Projects
Commit abcc2a9c authored by Samed's avatar Samed Committed by Leonardo Christino
Browse files

fix: sharebutton

parent b26fa784
No related branches found
No related tags found
1 merge request!311fix: sharebutton
Pipeline #141364 passed
import { useAppDispatch, useAuthCache } from '../store';
import { authenticated, changeRoom, UserAuthenticationHeader } from '../store/authSlice';
import { addInfo, addError } from '../store/configSlice';
const domain = import.meta.env.BACKEND_URL;
const userURI = import.meta.env.BACKEND_USER;
......@@ -16,15 +17,15 @@ export const useAuthentication = () => {
const handleError = (err: any) => {
console.error(err);
dispatch(addError('Failed to copy: ' + (err.message || 'Unknown error occurred')));
};
const login = () => {
fetch(`${domain}${userURI}/headers`, fetchSettings)
.then((res) => {
.then((res) =>
res
.json()
.then((res: UserAuthenticationHeader) => {
console.log(res, 'headers');
dispatch(
authenticated({
username: res.username,
......@@ -36,24 +37,47 @@ export const useAuthentication = () => {
}),
);
})
.catch(handleError);
})
.catch(handleError);
};
const newShareRoom = () => {
fetch(`${domain}${userURI}/share`, { ...fetchSettings, method: 'POST' })
.then((res) =>
res
.json()
.then((res: { Roomid: string; Sessionid: string }) => {
// TODO: send to backend current state and make redux accordingly
dispatch(changeRoom(res.Roomid));
})
.catch(handleError),
)
.catch(handleError);
};
const copyToClipboard = async (text: string) => {
try {
await navigator.clipboard.writeText(text);
return true;
} catch (err) {
console.error('Failed to copy:', err);
return false;
}
};
const newShareRoom = async () => {
try {
// TODO: Implement share room functionality when backend is ready
// fetch(`${domain}${userURI}/share`, { ...fetchSettings, method: 'POST' })
// .then((res) =>
// res
// .json()
// .then((res: { Roomid: string; Sessionid: string }) => {
// dispatch(changeRoom(res.Roomid));
// })
// .catch(handleError),
// )
// .catch(handleError);
const shareUrl = window.location.href;
const copied = await copyToClipboard(shareUrl);
if (copied) {
dispatch(addInfo('Link copied to clipboard!'));
} else {
throw new Error('Failed to copy to clipboard');
}
} catch (error: any) {
handleError(error);
}
};
return { login, newShareRoom };
};
};
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment