From d9125314809e7f03cb155f91d535e94da583a365 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Tue, 31 Jan 2023 22:21:19 -0500 Subject: custosm avatars and banners --- public/js/api.js | 29 +++++++++++++++++++++++++++++ public/js/main.js | 6 +++++- public/js/profile.js | 27 ++++++++++++++++++++++++--- 3 files changed, 58 insertions(+), 4 deletions(-) (limited to 'public/js') diff --git a/public/js/api.js b/public/js/api.js index 9845be5..b2ea597 100644 --- a/public/js/api.js +++ b/public/js/api.js @@ -1,6 +1,27 @@ const endpoint = '/api' +const fileRequest = async (url, file, method) => { + if (method === undefined) method = 'POST' + const response = await fetch(endpoint + url, { + method, + body: file, + headers: {} + }); + if (response.status == 401) { + location.href = 'login' + } + const contentType = response.headers.get("content-type"); + if (contentType && contentType.indexOf("application/json") !== -1) { + const json = await response.json() + return { status: response.status, msg: json.msg, json } + } else { + const msg = await response.text(); + return { status: response.status, msg } + } +} + const request = async (url, body, method) => { + if (method === undefined) method = 'POST' const response = await fetch(endpoint + url, { method, @@ -88,4 +109,12 @@ const adminusers = async () => { const adminsessions = async () => { return await request('/admin/sessions', {}) +} + +const updateavatar = async (file) => { + return await fileRequest('/users/avatar', file, 'PUT') +} + +const updatebanner = async (file) => { + return await fileRequest('/users/banner', file, 'PUT') } \ No newline at end of file diff --git a/public/js/main.js b/public/js/main.js index 87dd8e0..ffbc1f3 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -33,7 +33,11 @@ function remove(id) { } function pfp(id) { - return `` + return `` +} + +function banner(id) { + return `` } const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', diff --git a/public/js/profile.js b/public/js/profile.js index 10eb873..90787f0 100644 --- a/public/js/profile.js +++ b/public/js/profile.js @@ -16,24 +16,44 @@ function swap(value) { } } +function changeimage(fn) { + + var input = document.createElement('input') + input.type = 'file' + input.accept= 'image/png' + + input.onchange = async (e) => { + var file = e.target.files[0]; + if (file.type !== 'image/png') { + return + } + let response = await fn(file); + alert(response.msg) + } + + input.click(); +} + function render() { const html = `