Skip to content

Commit

Permalink
fix(ux): 🐛 fix tgl edit no-project not clearing the project (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNZL committed May 19, 2023
1 parent 6a3c0b4 commit e96cac8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
61 changes: 60 additions & 1 deletion src/Toggl/Fetch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,74 @@
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Linq;
using System.Threading.Tasks;

namespace Flow.Launcher.Plugin.TogglTrack.TogglApi
{
/// <summary>
/// This is a custom JsonConverter that ensures that certain fields are never ignored, even if they are null.
///
/// This is necessary as certain fields (ie project_id) still carry meaning when they are null, rather than being necessarily 'optional'.
/// </summary>
public class NullPropertyConverter : JsonConverter<object>
{
private readonly string[] _neverIgnore;

public NullPropertyConverter(string[] neverIgnore)
{
this._neverIgnore = neverIgnore;
}

public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}

public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
{
var type = value?.GetType();
var properties = type?.GetProperties();

if (properties is null)
{
return;
}

writer.WriteStartObject();

foreach (var property in properties)
{
if (!property.CanRead)
{
continue;
}

var propertyValue = property.GetValue(value);

if ((propertyValue is null) && (!this._neverIgnore.Contains(property.Name)))
{
continue;
}

writer.WritePropertyName(property.Name);
JsonSerializer.Serialize(writer, propertyValue);
}

writer.WriteEndObject();
}
}

public class AuthenticatedFetch
{
private readonly static JsonSerializerOptions _serializerOptions = new JsonSerializerOptions
{
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
Converters = {
new NullPropertyConverter(neverIgnore: new string[] {
"project_id",
}),
}
};

private string _token;
Expand Down
2 changes: 1 addition & 1 deletion src/TogglTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ internal async ValueTask<List<Result>> RequestStopEntry(CancellationToken token,
{
this._context.API.LogInfo("TogglTrack", $"{this._selectedProjectId}, {runningTimeEntry.Id}, {runningTimeEntry.WorkspaceId}, {runningTimeEntry.StartDate}, {(int)newElapsed.TotalHours}:{newElapsed.ToString(@"mm\:ss")}, {stopTime}, time span flag", "RequestStopEntry");
var stoppedTimeEntry = (await this._client.EditTimeEntry(runningTimeEntry.Id, runningTimeEntry.WorkspaceId, null, null, null, stopTime, runningTimeEntry.Duration, runningTimeEntry.Tags, runningTimeEntry.Billable))?.ToTimeEntry(me);
var stoppedTimeEntry = (await this._client.EditTimeEntry(runningTimeEntry.Id, runningTimeEntry.WorkspaceId, runningTimeEntry.ProjectId, null, null, stopTime, runningTimeEntry.Duration, runningTimeEntry.Tags, runningTimeEntry.Billable))?.ToTimeEntry(me);
if (stoppedTimeEntry?.Id is null)
{
throw new Exception("An API error was encountered.");
Expand Down

0 comments on commit e96cac8

Please sign in to comment.