-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
208 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { getUser } from './get-user.js' | ||
import { putUser } from './put-user.js' | ||
|
||
export default async function userRoutes (fastify, opts) { | ||
await Promise.all([ | ||
getUser(fastify, opts) | ||
getUser(fastify, opts), | ||
putUser(fastify, opts) | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import SQL from '@nearform/sql' | ||
|
||
export async function putUser (fastify, opts) { | ||
fastify.put( | ||
'/', | ||
{ | ||
preHandler: fastify.auth([fastify.verifyJWT]), | ||
schema: { | ||
body: { | ||
type: 'object', | ||
additionalProperties: false, | ||
minProperties: 1, | ||
properties: { | ||
username: { | ||
type: 'string', | ||
minLength: 1, | ||
maxLength: 100 | ||
}, | ||
password: { | ||
type: 'string', | ||
minLength: 8, | ||
maxLength: 50 | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
async function putUserHandler (request, reply) { | ||
return fastify.pg.transact(async client => { | ||
const userID = request.user.id | ||
const user = request.body | ||
|
||
const updates = [] | ||
|
||
if (user.username) updates.push(SQL`username = ${user.username}`) | ||
if (user.password) updates.push(SQL`password = crypt(${user.password}, gen_salt('bf'))`) | ||
|
||
if (updates.length > 0) { | ||
const query = SQL` | ||
update users | ||
set ${SQL.glue(updates, ' , ')} | ||
where id = ${userID} | ||
` | ||
await client.query(query) | ||
} | ||
|
||
return { | ||
status: 'ok' | ||
} | ||
}) | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Component, html, useState, useRef, useCallback } from 'uland-isomorphic' | ||
|
||
export const usernameEdit = Component(({ user, onSave, onCancelEdit }) => { | ||
const [error, setError] = useState(null) | ||
const [disabled, setDisabled] = useState(false) | ||
const formRef = useRef() | ||
|
||
const handleSave = useCallback(async (ev) => { | ||
ev.preventDefault() | ||
setDisabled(true) | ||
setError(null) | ||
|
||
const form = formRef.current | ||
|
||
const username = form.username.value | ||
|
||
const formState = { | ||
username | ||
} | ||
|
||
try { | ||
await onSave(formState) | ||
} catch (err) { | ||
setDisabled(false) | ||
setError(err) | ||
} | ||
}, [setDisabled, setError, formRef?.current, onSave]) | ||
|
||
return html` | ||
<div class='bc-account-username-edit'> | ||
<form ref="${formRef}" class="bc-account-username-edit-form" id="bc-account-username-edit-form" onsubmit=${handleSave}> | ||
<fieldset ?disabled=${disabled}> | ||
<legend class="bc-account-username-edit-legend">Edit username</legend> | ||
<div> | ||
<label class='block'> | ||
username: | ||
<input class='block' type="text" name="username" value="${user.username}"/> | ||
</label> | ||
</div> | ||
<div class="bc-account-username-edit-submit-line"> | ||
<div class="button-cluster"> | ||
${onSave ? html`<input name="submit-button" type="submit">` : null} | ||
${onCancelEdit ? html`<button onClick=${onCancelEdit}>Cancel</button>` : null} | ||
</div> | ||
</div> | ||
${error ? html`<div class="error-box">${error.message}</div>` : null} | ||
</fieldset> | ||
</form> | ||
</div> | ||
` | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* eslint-env browser */ | ||
import { Component, html, useState, useCallback } from 'uland-isomorphic' | ||
import { useLSP } from '../../hooks/useLSP.js' | ||
import { usernameEdit } from './username-edit.js' | ||
import { usernameView } from './username-view.js' | ||
|
||
export const usernameField = Component(({ user, reload }) => { | ||
const state = useLSP() | ||
const [editing, setEditing] = useState(false) | ||
|
||
const handleEdit = useCallback(() => { | ||
setEditing(true) | ||
}, [setEditing]) | ||
|
||
const handleCancelEdit = useCallback(() => { | ||
setEditing(false) | ||
}, [setEditing]) | ||
|
||
const handleSave = useCallback(async ({ username }) => { | ||
const endpoint = `${state.apiUrl}/user` | ||
const response = await fetch(endpoint, { | ||
method: 'put', | ||
headers: { | ||
'content-type': 'application/json' | ||
}, | ||
body: JSON.stringify({ username }) | ||
}) | ||
|
||
if (response.ok && response.headers.get('content-type')?.includes('application/json')) { | ||
console.log(await response.json()) | ||
setEditing(false) | ||
reload() | ||
} else { | ||
throw new Error(`${response.status} ${response.statusText}: ${await response.text()}`) | ||
} | ||
}, [user?.username, state.apiUrl, setEditing]) | ||
|
||
return html` | ||
${ | ||
editing | ||
? usernameEdit({ | ||
user, | ||
onSave: handleSave, | ||
onCancelEdit: handleCancelEdit | ||
}) | ||
: usernameView({ | ||
user, | ||
onEdit: handleEdit | ||
}) | ||
} | ||
` | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Component, html } from 'uland-isomorphic' | ||
|
||
export const usernameView = Component(({ user, onEdit }) => { | ||
return html` | ||
<dt>username</dt> | ||
<dd> | ||
${user?.username} | ||
<button onClick=${onEdit}>Edit</button> | ||
</dd> | ||
` | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.