Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F7189013
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
{
tempstream
}
from
"tempstream"
;
import
{
Context
}
from
"koa"
;
import
{
Form
,
FormData
,
FormDataValue
,
Fields
,
Controls
,
fieldsToShape
,
}
from
"@sealcode/sealgen"
;
import
{
Tasks
}
from
"../collections/collections.js"
;
import
html
from
"../html.js"
;
import
{
TaskList
}
from
"./common/tasks-view.js"
;
export
const
actionName
=
"Todo"
;
const
fields
=
{
name
:
new
Fields
.
CollectionField
(
true
,
Tasks
.
fields
.
title
),
};
export
const
TodoShape
=
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
();
}
break
;
}
case
"delete"
:
{
const
task
=
await
ctx
.
$app
.
collections
.
tasks
.
getByID
(
ctx
.
$context
,
String
(
data
.
raw_values
.
taskId
)
);
await
task
.
remove
(
ctx
.
$context
);
break
;
}
default
:
{
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
Tue, Jul 8, 08:41 (2 h, 8 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
801502
Default Alt Text
todo.form.ts (2 KB)
Attached To
Mode
rPHOTOAPP photo-app-demo
Attached
Detach File
Event Timeline
Log In to Comment