hooks#useUser JavaScript Examples
The following examples show how to use
hooks#useUser.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: appointments.jsx From apps with GNU Affero General Public License v3.0 | 5 votes |
Appointments = () => {
const user = useUser();
const appointments = user.appointments().result().appointments;
const render = () => {
let invitations = [];
if (appointments !== undefined && appointments.data !== null)
for (const appointment of appointments.data)
invitations.push(appointment);
if (invitation !== undefined && invitation.data !== null)
for (const offer of invitation.data) {
if (
invitations.some(
(inv) => inv.provider.name === offer.provider.name
)
)
continue;
invitations.push(offer);
}
if (
acceptedInvitation !== undefined &&
acceptedInvitation.data !== null
) {
const ai = invitations.find((inv) => {
if (inv === null) return false;
return inv.offers.some((offer) =>
offer.slotData.some((sla) =>
acceptedInvitation.data.offer.slotData.some(
(slb) => slb.id === sla.id
)
)
);
});
if (ai === undefined) return <InvitationDeleted />;
return <AcceptedInvitation offers={ai.offers} />;
}
// we only show relevant invitations
invitations = invitations.filter((inv) =>
filterInvitations(inv, acceptedInvitation)
);
if (invitations.length === 0)
return <NoInvitations tokenData={tokenData.data} />;
const details = invitations.map((data) => (
<InvitationDetails
tokenData={tokenData}
data={data}
key={data.provider.signature}
/>
));
return <F>{details}</F>;
};
return (
<WithLoader
resources={[user.appointments().result()]}
renderLoaded={render}
/>
);
}
Example #2
Source File: contact-data.jsx From apps with GNU Affero General Public License v3.0 | 4 votes |
BaseContactData = ({
form: { set, data, error, valid, reset },
router,
}) => {
const [modified, setModified] = useState(false);
const [initialized, setInitialized] = useState(false);
const user = useUser();
const onSubmit = () => {
if (!valid) return;
user.contactData = data;
// we redirect to the 'verify' step
router.navigateToUrl(`/user/setup/finalize`);
};
useEffect(() => {
if (initialized) return;
setInitialized(true);
setModified(false);
reset(user.contactData || {});
});
const submitting = false;
const setAndMarkModified = (key, value) => {
setModified(true);
set(key, value);
};
const controls = (
<React.Fragment>
<ErrorFor error={error} field="code" />
<RetractingLabelInput
value={data.code || ''}
onChange={(value) => setAndMarkModified('code', value)}
description={
<T t={t} k="contact-data.access-code.description" />
}
label={<T t={t} k="contact-data.access-code.label" />}
/>
</React.Fragment>
);
const redirecting = false;
return (
<React.Fragment>
<div className="kip-cm-contact-data">
<FormComponent onSubmit={onSubmit}>
<FieldSet disabled={submitting}>
{
<React.Fragment>
<CardContent>{controls}</CardContent>
<CardFooter>
<SubmitField
disabled={!valid}
type={'success'}
onClick={onSubmit}
waiting={submitting || redirecting}
title={
redirecting ? (
<T
t={t}
k="contact-data.success"
/>
) : submitting ? (
<T
t={t}
k="contact-data.saving"
/>
) : (
<T
t={t}
k={
'contact-data.save-and-continue'
}
/>
)
}
/>
</CardFooter>
</React.Fragment>
}
</FieldSet>
</FormComponent>
</div>
</React.Fragment>
);
}
Example #3
Source File: finalize.jsx From apps with GNU Affero General Public License v3.0 | 4 votes |
Finalize = withForm(
withSettings(
withRouter(
({
settings,
router,
form: { set, data, error, valid, reset },
}) => {
const [initialized, setInitialized] = useState(false);
const [modified, setModified] = useState(false);
const [submitting, setSubmitting] = useState(false);
const [tv, setTV] = useState(0);
const user = useUser();
useEffect(async () => {
if (initialized) return;
setInitialized(true);
await user.initialize();
const initialData = {
distance: 5,
};
for (const [k, v] of Object.entries(
t['contact-data'].properties
)) {
for (const [kv, vv] of Object.entries(v.values)) {
initialData[kv] = vv._default;
}
}
reset(user.queueData || initialData);
});
const submit = async () => {
setSubmitting(true);
// we store the queue data
user.queueData = data;
const result = await user.getToken({});
setSubmitting(false);
if (result.status === Status.Failed) return;
await user.backupData();
router.navigateToUrl('/user/setup/store-secrets');
};
const setAndMarkModified = (key, value) => {
setModified(true);
set(key, value);
};
const properties = Object.entries(
t['contact-data'].properties
).map(([k, v]) => {
const items = Object.entries(v.values).map(([kv, vv]) => (
<li key={kv}>
<Switch
id={kv}
checked={data[kv] || false}
onChange={(value) =>
setAndMarkModified(kv, value)
}
>
</Switch>
<label htmlFor={kv}>
<T
t={t}
k={`contact-data.properties.${k}.values.${kv}`}
/>
</label>
</li>
));
return (
<F key={k}>
<h2>
<T
t={t}
k={`contact-data.properties.${k}.title`}
/>
</h2>
<ul className="kip-properties">{items}</ul>
</F>
);
});
const render = () => {
let failedMessage;
let failed;
if (
getToken !== undefined &&
getToken.status === 'failed'
) {
failed = true;
if (getToken.error.error.code === 401) {
failedMessage = (
<Message type="danger">
<T t={t} k="wizard.failed.invalid-code" />
</Message>
);
}
}
if (failed && !failedMessage)
failedMessage = (
<Message type="danger">
<T t={t} k="wizard.failed.notice" />
</Message>
);
return (
<React.Fragment>
<CardContent>
{failedMessage}
<div className="kip-finalize-fields">
<ErrorFor error={error} field="zipCode" />
<RetractingLabelInput
value={data.zipCode || ''}
onChange={(value) =>
setAndMarkModified('zipCode', value)
}
label={
<T
t={t}
k="contact-data.zip-code"
/>
}
/>
<label
className="kip-control-label"
htmlFor="distance"
>
<T
t={t}
k="contact-data.distance.label"
/>
<span className="kip-control-notice">
<T
t={t}
k="contact-data.distance.notice"
/>
</span>
</label>
<ErrorFor error={error} field="distance" />
<RichSelect
id="distance"
value={data.distance || 5}
onChange={(value) =>
setAndMarkModified(
'distance',
value.value
)
}
options={[
{
value: 5,
description: (
<T
t={t}
k="contact-data.distance.option"
distance={5}
/>
),
},
{
value: 10,
description: (
<T
t={t}
k="contact-data.distance.option"
distance={10}
/>
),
},
{
value: 20,
description: (
<T
t={t}
k="contact-data.distance.option"
distance={20}
/>
),
},
{
value: 30,
description: (
<T
t={t}
k="contact-data.distance.option"
distance={30}
/>
),
},
{
value: 40,
description: (
<T
t={t}
k="contact-data.distance.option"
distance={40}
/>
),
},
{
value: 50,
description: (
<T
t={t}
k="contact-data.distance.option"
distance={50}
/>
),
},
]}
/>
{properties}
</div>
</CardContent>
<CardFooter>
<Button
waiting={submitting}
type={failed ? 'danger' : 'success'}
onClick={submit}
disabled={submitting || !valid}
>
<T
t={t}
k={
failed
? 'wizard.failed.title'
: submitting
? 'wizard.please-wait'
: 'wizard.continue'
}
/>
</Button>
</CardFooter>
</React.Fragment>
);
};
return <WithLoader resources={[]} renderLoaded={render} />;
}
)
),
FinalizeForm,
'form'
)
Example #4
Source File: store-secrets.jsx From apps with GNU Affero General Public License v3.0 | 4 votes |
StoreOnline = ({ secret, embedded, hideNotice }) => {
const [bookmarkModal, setBookmarkModal] = useState(false);
const [copyModal, setCopyModal] = useState(false);
const settings = useSettings();
const user = useUser();
let modal;
const showBookmarkModal = () => {
history.pushState(
{},
settings.t(t, 'store-secrets.restore.title'),
`/user/restore#${user.secret}`
);
setBookmarkModal(true);
};
const hideBookmarkModal = () => {
history.back();
setBookmarkModal(false);
};
if (bookmarkModal)
modal = (
<Modal
title={<T t={t} k="store-secrets.bookmark-modal.title" />}
onClose={hideBookmarkModal}
>
<T t={t} k="store-secrets.bookmark-modal.text" />
</Modal>
);
const chunks = user.secret.match(/.{1,4}/g);
const fragments = [];
for (let i = 0; i < chunks.length; i++) {
fragments.push(<F key={`${i}-main`}>{chunks[i]}</F>);
if (i < chunks.length - 1)
fragments.push(
<strong key={`${i}-dot`} style={{ userSelect: 'none' }}>
ยท
</strong>
);
}
return (
<F>
{modal}
{!embedded && (
<p className="kip-secrets-notice">
<T t={t} k="store-secrets.online.text" safe />
</p>
)}
<div
className={
'kip-secrets-box' + (embedded ? ' kip-is-embedded' : '')
}
>
{
<F>
<div className="kip-uid">
{!hideNotice && (
<span>
<T t={t} k="store-secrets.secret" />
</span>
)}
<code>{fragments}</code>
</div>
</F>
}
</div>
{!embedded && (
<div className="kip-secrets-links">
<A
className="bulma-button bulma-is-small"
onClick={showBookmarkModal}
>
<T t={t} k="store-secrets.bookmark" />
</A>
</div>
)}
</F>
);
}