Skip to content

Commit

Permalink
Merge pull request #15684 from kraktus/delete_study_flair
Browse files Browse the repository at this point in the history
Delete study flair
  • Loading branch information
ornicar committed Jul 10, 2024
2 parents 5674ab7 + ad3b153 commit 2289f59
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 57 deletions.
34 changes: 18 additions & 16 deletions modules/study/src/main/StudyApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -771,22 +771,24 @@ final class StudyApi(

def editStudy(studyId: StudyId, data: Study.Data)(who: Who) =
sequenceStudy(studyId): study =>
canActAsOwner(study, who.u).flatMap { asOwner =>
asOwner.option(data.settings).so { settings =>
val newStudy = study.copy(
name = Study.toName(data.name),
flair = data.flair.flatMap(flairApi.find),
settings = settings,
visibility = data.vis,
description = settings.description.option {
study.description.filter(_.nonEmpty) | "-"
}
)
(newStudy != study).so {
studyRepo.updateSomeFields(newStudy).andDo(sendTo(study.id)(_.reloadAll)).andDo(indexStudy(study))
}
}
}
canActAsOwner(study, who.u).flatMap: asOwner =>
asOwner
.option(data.settings)
.so: settings =>
val newStudy = study
.copy(
name = Study.toName(data.name),
flair = data.flair.flatMap(flairApi.find),
settings = settings,
visibility = data.vis,
description = settings.description.option:
study.description.filter(_.nonEmpty) | "-"
)
(newStudy != study).so:
studyRepo
.updateSomeFields(newStudy)
.andDo(sendTo(study.id)(_.reloadAll))
.andDo(indexStudy(study))

def delete(study: Study) =
sequenceStudy(study.id): study =>
Expand Down
15 changes: 8 additions & 7 deletions modules/study/src/main/StudyRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,19 @@ final class StudyRepo(private[study] val coll: AsyncColl)(using
.void

def updateSomeFields(s: Study): Funit =
import toBSONValueOption.given
coll:
_.update
.one(
$id(s.id),
$set(
"position" -> s.position,
"name" -> s.name,
$setsAndUnsets(
"position" -> s.position.some,
"name" -> s.name.some,
"flair" -> s.flair,
"settings" -> s.settings,
"visibility" -> s.visibility,
"description" -> ~s.description,
"updatedAt" -> nowInstant
"settings" -> s.settings.some,
"visibility" -> s.visibility.some,
"description" -> s.description,
"updatedAt" -> nowInstant.some
)
)
.void
Expand Down
31 changes: 4 additions & 27 deletions ui/analyse/src/study/studyForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { VNode } from 'snabbdom';
import * as licon from 'common/licon';
import { prop } from 'common';
import { snabDialog } from 'common/dialog';
import flairPickerLoader from 'common/flairPicker';
import { bindSubmit, bindNonPassive, onInsert, looseH as h } from 'common/snabbdom';
import { emptyRedButton } from '../view/util';
import { StudyData } from './interfaces';
Expand Down Expand Up @@ -123,7 +124,7 @@ export function view(ctrl: StudyForm): VNode {
h(
'details.form-control.emoji-details',
{
hook: onInsert(el => flairPicker(el)),
hook: onInsert(el => flairPickerLoader(el)),
},
[
h('summary.button.button-metal.button-no-upper', [
Expand All @@ -141,6 +142,7 @@ export function view(ctrl: StudyForm): VNode {
}),
],
),
data.flair && h(removeEmojiButton, 'Delete'),
]),
h('div.form-group', [
h('label.form-label', { attrs: { for: 'study-name' } }, ctrl.trans.noarg('name')),
Expand Down Expand Up @@ -272,29 +274,4 @@ export function view(ctrl: StudyForm): VNode {
});
}

// TODO FIXME
// copied from ui/bits/src/load/flairPicker.ts
function flairPicker(element: HTMLElement) {
const parent = $(element).parent();
const close = () => element.removeAttribute('open');
const onEmojiSelect = (i?: { id: string; src: string }) => {
parent.find('input[name="flair"]').val(i?.id ?? '');
parent.find('.uflair').remove();
if (i) parent.find('.flair-container').append('<img class="uflair" src="' + i.src + '" />');
close();
};
parent.find('.emoji-remove').on('click', e => {
e.preventDefault();
onEmojiSelect();
$(e.target).remove();
});
$(element).on('toggle', () =>
site.asset.loadEsm('bits.flairPicker', {
init: {
element: element.querySelector('.flair-picker')!,
close,
onEmojiSelect,
},
}),
);
}
const removeEmojiButton = emptyRedButton + '.text.emoji-remove';
4 changes: 2 additions & 2 deletions ui/bits/src/bits.account.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as licon from 'common/licon';
import * as xhr from 'common/xhr';
import { addPasswordVisibilityToggleListener } from 'common/password';
import flairPicker from './load/flairPicker';
import flairPickerLoader from 'common/flairPicker';

site.load.then(() => {
$('.emoji-details').each(function (this: HTMLElement) {
flairPicker(this);
flairPickerLoader(this);
});

addPasswordVisibilityToggleListener();
Expand Down
4 changes: 2 additions & 2 deletions ui/bits/src/bits.dailyFeed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flairPicker from './load/flairPicker';
import flairPickerLoader from 'common/flairPicker';

$('.emoji-details').each(function (this: HTMLElement) {
flairPicker(this);
flairPickerLoader(this);
});
4 changes: 2 additions & 2 deletions ui/bits/src/bits.team.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as xhr from 'common/xhr';
import flairPicker from './load/flairPicker';
import flairPickerLoader from 'common/flairPicker';

interface TeamOpts {
id: string;
Expand Down Expand Up @@ -29,5 +29,5 @@ $('button.explain').on('click', e => {
});

$('.emoji-details').each(function (this: HTMLElement) {
flairPicker(this);
flairPickerLoader(this);
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function flairPicker(element: HTMLElement) {
export default function flairPickerLoader(element: HTMLElement) {
const parent = $(element).parent();
const close = () => element.removeAttribute('open');
const onEmojiSelect = (i?: { id: string; src: string }) => {
Expand Down

0 comments on commit 2289f59

Please sign in to comment.