Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F1262538
todo.form.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
todo.form.ts
View Options
import
{
Tasks
}
from
"./../collections/collections"
;
import
{
tempstream
}
from
"tempstream"
;
import
{
Context
}
from
"koa"
;
import
{
Form
,
FormData
,
FormDataValue
,
Fields
,
Controls
}
from
"@sealcode/sealgen"
;
import
html
from
"../html"
;
import
{
TaskList
}
from
"./common/tasks-view"
;
export
const
actionName
=
"Todo"
;
const
fields
=
{
name
:
new
Fields
.
CollectionField
(
true
,
Tasks
.
fields
.
title
),
};
export
const
TodoShape
=
Fields
.
fieldsToShape
(
fields
);
export
default
new
(
class
TodoForm
extends
Form
<
typeof
fields
,
void
>
{
defaultSuccessMessage
=
"Task has been successfully created"
;
fields
=
fields
;
controls
=
[
new
Controls
.
SimpleInput
(
fields
.
name
,
{
label
:
"Task name:"
,
type
:
"text"
,
placeholder
:
"Write an Matrix bot"
,
}),
new
Controls
.
HTML
(
"decoration"
,
(
fctx
)
=>
{
return
`<input class="hidden-button" type="hidden" id="action" name="action" value="create" form="
${
fctx
.
form_id
}
" />`
;
}),
];
async
validateValues
(
ctx
:
Context
,
data
:
Record
<
string
,
FormDataValue
>
)
:
Promise
<
{
valid
:
boolean
;
error
:
string
}
>
{
const
{
parsed
:
name
}
=
await
this
.
fields
.
name
.
getValue
(
ctx
,
data
);
if
((
name
||
""
).
length
<
3
)
{
return
{
valid
:
true
,
error
:
"The name of the task must have at least 3 characters"
,
};
}
else
{
const
filter
:
object
=
name
?
{
title
:
name
}
:
{};
const
tasks
=
await
ctx
.
$app
.
collections
.
tasks
.
list
(
ctx
.
$context
)
.
filter
(
filter
)
.
fetch
();
if
(
tasks
.
empty
)
{
return
{
valid
:
true
,
error
:
""
};
}
return
{
valid
:
false
,
error
:
"Task with the same name already exists"
};
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async
canAccess
(
ctx
:
Context
)
{
if
(
ctx
.
$context
.
session_id
)
{
return
{
canAccess
:
true
,
message
:
""
};
}
return
{
canAccess
:
false
,
message
:
""
};
}
async
onSubmit
(
ctx
:
Context
,
data
:
FormData
)
{
const
action
:
FormDataValue
=
data
.
raw_values
.
action
;
switch
(
action
)
{
case
"create"
:
{
try
{
await
ctx
.
$app
.
collections
.
tasks
.
create
(
ctx
.
$context
,
{
title
:
String
(
data
.
raw_values
.
name
),
done
:
false
,
});
}
catch
(
error
)
{
throw
new
Error
();
}
console
.
debug
(
`task has been successfully created`
);
break
;
}
case
"delete"
:
{
const
task
=
await
ctx
.
$app
.
collections
.
tasks
.
getByID
(
ctx
.
$context
,
data
.
raw_values
.
taskId
as
string
);
await
task
.
remove
(
ctx
.
$context
);
console
.
debug
(
`task has been successfully removed`
);
break
;
}
default
:
{
console
.
debug
(
"Wrong action"
);
break
;
}
}
return
;
}
async
render
(
ctx
:
Context
,
data
:
FormData
,
show_field_errors
:
boolean
)
{
return
html
(
ctx
,
"Todo"
,
tempstream
`
${
await
super
.
render
(
ctx
,
data
,
show_field_errors
)
}
${
TaskList
(
ctx
)
}
`
);
}
})();
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Fri, Jan 24, 13:48 (1 d, 19 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
599599
Default Alt Text
todo.form.ts (2 KB)
Attached To
Mode
rPHOTOAPP photo-app-demo
Attached
Detach File
Event Timeline
Log In to Comment