Skip to content

Commit 9c90372

Browse files
author
pmosk
committed
Show selected date after Create button
1 parent a7c2aeb commit 9c90372

File tree

4 files changed

+50
-25
lines changed

4 files changed

+50
-25
lines changed

src/endpoint/Timesheet.Create/Flow/Flow.Run.cs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,40 @@ internal static async ValueTask<Unit> RunAsync(
2727
return await context.BotFlow.NextAsync(cancellationToken).ConfigureAwait(false);
2828
}
2929

30-
var chatFlow = await context.GetChatFlowAsync(commandName, cancellationToken).ConfigureAwait(false);
30+
var chatFlow = await InnerGetChatFlowAsync().ConfigureAwait(false);
3131
if (chatFlow is null)
3232
{
3333
return await context.BotFlow.NextAsync(cancellationToken).ConfigureAwait(false);
3434
}
3535

36-
var data = GetWebAppUpdateResponseJson(context);
37-
38-
await chatFlow.RunFlow(context, crmProjectApi, crmTimesheetApi, option, data).GetFlowStateAsync(cancellationToken).ConfigureAwait(false);
36+
_ = await chatFlow.GetFlowStateAsync(cancellationToken).ConfigureAwait(false);
3937
return await context.BotFlow.EndAsync(cancellationToken).ConfigureAwait(false);
40-
}
4138

42-
private static async Task<ChatFlowStarter<TimesheetCreateFlowState>?> GetChatFlowAsync(
43-
this IBotContext context, string commandName, CancellationToken cancellationToken)
44-
{
45-
var starter = context.GetChatFlowStarter<TimesheetCreateFlowState>("TimesheetCreate");
46-
if (await starter.IsStartedAsync(cancellationToken).ConfigureAwait(false))
39+
async Task<ChatFlow<TimesheetCreateFlowState>?> InnerGetChatFlowAsync()
4740
{
48-
return starter;
49-
}
41+
var starter = context.GetChatFlowStarter<TimesheetCreateFlowState>("TimesheetCreate");
42+
if (await starter.IsStartedAsync(cancellationToken).ConfigureAwait(false))
43+
{
44+
return starter.RunFlow(context, crmProjectApi, crmTimesheetApi, option, default);
45+
}
5046

51-
if (context.TurnContext.RecognizeCommandOrAbsent(commandName).IsPresent)
52-
{
53-
return starter;
54-
}
47+
if (context.TurnContext.RecognizeCommandOrAbsent(commandName).IsPresent)
48+
{
49+
return starter.RunFlow(context, crmProjectApi, crmTimesheetApi, option, default);
50+
}
5551

56-
var timesheet = GetWebAppUpdateResponseJson(context);
57-
if (string.Equals(timesheet?.Command, "updatetimesheet", StringComparison.InvariantCultureIgnoreCase))
58-
{
59-
await context.TurnContext.DeleteActivityAsync(context.TurnContext.Activity.Id, cancellationToken).ConfigureAwait(false);
60-
return starter;
61-
}
52+
var timesheet = context.GetWebAppUpdateResponseJson();
53+
if (string.Equals(timesheet?.Command, "updatetimesheet", StringComparison.InvariantCultureIgnoreCase))
54+
{
55+
await context.TurnContext.DeleteActivityAsync(context.TurnContext.Activity.Id, cancellationToken).ConfigureAwait(false);
56+
return starter.RunFlow(context, crmProjectApi, crmTimesheetApi, option, timesheet);
57+
}
6258

63-
return null;
59+
return null;
60+
}
6461
}
6562

66-
private static WebAppDataTimesheetUpdateJson? GetWebAppUpdateResponseJson(IBotContext context)
63+
private static WebAppDataTimesheetUpdateJson? GetWebAppUpdateResponseJson(this IBotContext context)
6764
{
6865
var dataWebApp = TelegramWebAppResponse.FromChannelData(context.TurnContext.Activity.ChannelData);
6966
return JsonConvert.DeserializeObject<WebAppDataTimesheetUpdateJson>((dataWebApp.Message?.WebAppData?.Data).OrEmpty());

src/endpoint/Timesheet.Create/Flow/TimesheetCreateChatFlow.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ internal static ChatFlow<TimesheetCreateFlowState> RunFlow(
2020
ValueHours = data?.Duration,
2121
Project = data?.Project,
2222
Date = data?.Date,
23+
ShowSelectedDate = data?.Date is not null && data?.Id is null,
2324
LimitationDay = option.LimitationDay,
2425
UrlWebApp = option.UrlWebApp,
2526
WithoutConfirmation = data?.Project is not null

src/endpoint/Timesheet.Create/FlowState/TimesheetCreateFlowState.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public string? DateText
2525
init => Date = value is null ? null : DateOnly.Parse(value, CultureInfo.InvariantCulture);
2626
}
2727

28+
[JsonProperty("showSelectedDate")]
29+
public bool ShowSelectedDate { get; init; }
30+
2831
[JsonProperty("valueHours")]
2932
public decimal? ValueHours { get; init; }
3033

src/endpoint/Timesheet.Create/FlowStep/Step.AwaitDate.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using GarageGroup.Infra.Bot.Builder;
55
using Microsoft.Bot.Builder;
6+
using Microsoft.Bot.Schema;
67

78
namespace GarageGroup.Internal.Timesheet;
89

@@ -11,7 +12,14 @@ partial class TimesheetCreateFlowStep
1112
internal static ChatFlow<TimesheetCreateFlowState> AwaitDate(
1213
this ChatFlow<TimesheetCreateFlowState> chatFlow)
1314
=>
14-
chatFlow.AwaitDate(
15+
chatFlow.SendActivityOrSkip(
16+
BuildSelectedDateActivity)
17+
.MapFlowState(
18+
static state => state with
19+
{
20+
ShowSelectedDate = false
21+
})
22+
.AwaitDate(
1523
static context => new(
1624
text: context.GetDateText(),
1725
confirmButtonText: "Choose",
@@ -29,6 +37,22 @@ internal static ChatFlow<TimesheetCreateFlowState> AwaitDate(
2937
Date = date
3038
});
3139

40+
private static Activity? BuildSelectedDateActivity(IChatFlowContext<TimesheetCreateFlowState> context)
41+
{
42+
if (context.FlowState.Date is null || context.FlowState.ShowSelectedDate is false || context.IsNotTelegramChannel())
43+
{
44+
return null;
45+
}
46+
47+
var parameters = new TelegramParameters($"Date: <b>{context.FlowState.Date.Value.ToDisplayText()}</b>")
48+
{
49+
ParseMode = TelegramParseMode.Html,
50+
ReplyMarkup = new TelegramReplyKeyboardRemove()
51+
};
52+
53+
return parameters.BuildActivity();
54+
}
55+
3256
private static string GetDateText(this ITurnContext context)
3357
{
3458
if (context.IsMsteamsChannel())

0 commit comments

Comments
 (0)