diff --git a/src/components/Settings/Settings.js b/src/components/Settings/Settings.js index 09f9d2a..1bc0724 100644 --- a/src/components/Settings/Settings.js +++ b/src/components/Settings/Settings.js @@ -1,15 +1,58 @@ -import { useContext, useState } from 'react' +import ky from 'ky' +import { useContext, useEffect, useState } from 'react' +import { useLocation } from 'react-router' import { promptLogin, credentials, clearCredentials } from '../../Auth' import SettingsContext from '../../contexts/Settings' +import Modal from '../Modal/Modal' + import './Settings.css' +function checkCode(code) { + return ky + .get(`/api/code/info/${code}`, { + headers: { authorization: credentials.token }, + }) + .json() +} + +function claimCode(code) { + return new Promise((resolve, reject) => { + ky.post(`/api/code/claim/${code}`, { + headers: { authorization: credentials.token }, + }) + .json() + .then((ok) => { + resolve(ok) + }) + }) +} + +function listCodes() { + return ky + .get(`/api/code/list`, { + headers: { authorization: credentials.token }, + }) + .json() +} + const defaults = { hueFromArt: true, } +function CodeListing({ code: { value, type, used } }) { + return ( +
+ + {value} - {type} + + +
+ ) +} + function Toggle({ value }) { return (
@@ -54,7 +97,32 @@ function SettingToggle({ children, id }) { } function Settings() { - const [creds, setCreds] = useState({ ...credentials }) + const [creds, setCreds] = useState({ ...credentials }), + [code, setCode] = useState(null), + [modalCode, setModalCode] = useState(false), + [codes, setCodes] = useState([]) + + useEffect(() => { + if (credentials.token) { + listCodes().then((list) => setCodes(list)) + } + }, []) + + const { search } = useLocation() + + useEffect(() => { + let params = new URLSearchParams(search), + c = params.get('c') + + if (c) { + checkCode(c) + .then((info) => { + setCode(info) + setModalCode(true) + }) + .catch((err) => console.error(err)) + } + }, [search]) return (
@@ -82,10 +150,55 @@ function Settings() { {creds.token ? null : (
- Zaloguj się aby móc głosować więcej niż na jeden utwór + Zaloguj się aby móc głosować na utwory
)} + {codes.length > 0 && ( +
+ Zebrane kody: + {codes.map((code) => ( + + ))} +
+ )} + + {modalCode && ( + +
Znalazłeś kod do aktywacji!
+
+

+ Kod "{code.value}" jest + {code.claimed ? 'Zajęty 😥' : 'Wolny 😁'}.
+

+ {!credentials.token &&

Zaloguj się aby móc go zająć.

} + Kod typu + {code.type}: {code.desc} +
+
+ + {!code.claimed && credentials.token && ( + + )} +
+
+ )} +
Ustawiaj kolorystyke strony na podstawie okładki albumu