Skip to content

Commit

Permalink
Allow adding components with prefilled parent
Browse files Browse the repository at this point in the history
  • Loading branch information
codesoap committed Aug 14, 2024
1 parent 9696341 commit 152ce0a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
43 changes: 32 additions & 11 deletions cmd/mycolog/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type addComponentTmplData struct {
Spawn bool
Grow bool
PossibleParents []string
PrefilledParent string
KnownSpecies []string
Today string
IsFirst bool
Expand All @@ -43,7 +44,6 @@ func handleAddComponent(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, fmt.Sprint("/component/", id), http.StatusSeeOther)
return
} else {
// TODO: Enable passing prefilled parents via query parameter.
possibleParents, err := getPossibleParentIdentifiers()
if err != nil {
showError(w, err, r.URL.Path)
Expand All @@ -66,6 +66,7 @@ func handleAddComponent(w http.ResponseWriter, r *http.Request) {
Spawn: componentType == store.TypeSpawn,
Grow: componentType == store.TypeGrow,
PossibleParents: possibleParents,
PrefilledParent: getPrefilledParentIdentifier(r.FormValue("from")),
KnownSpecies: knownSpecies,
Today: time.Now().Format("2006-01-02"),
IsFirst: !componentsPresent,
Expand All @@ -88,20 +89,40 @@ func getPossibleParentIdentifiers() ([]string, error) {
}
identifiers := make([]string, len(components))
for i, component := range components {
switch component.Type {
case store.TypeSpores:
identifiers[i] = fmt.Sprintf("Spores %s (#%d)", component.Token, component.ID)
case store.TypeMycelium:
identifiers[i] = fmt.Sprintf("Mycelium %s (#%d)", component.Token, component.ID)
case store.TypeSpawn:
identifiers[i] = fmt.Sprintf("Spawn %s (#%d)", component.Token, component.ID)
case store.TypeGrow:
identifiers[i] = fmt.Sprintf("Grow %s (#%d)", component.Token, component.ID)
}
identifiers[i] = toIdentifier(component)
}
return identifiers, nil
}

func getPrefilledParentIdentifier(parentID string) string {
if parentID == "" {
return ""
}
id, err := strconv.ParseInt(parentID, 10, 64)
if err != nil {
return ""
}
comp, err := db.GetComponent(id)
if err != nil {
return ""
}
return toIdentifier(comp)
}

func toIdentifier(comp store.Component) string {
switch comp.Type {
case store.TypeSpores:
return fmt.Sprintf("Spores %s (#%d)", comp.Token, comp.ID)
case store.TypeMycelium:
return fmt.Sprintf("Mycelium %s (#%d)", comp.Token, comp.ID)
case store.TypeSpawn:
return fmt.Sprintf("Spawn %s (#%d)", comp.Token, comp.ID)
case store.TypeGrow:
return fmt.Sprintf("Grow %s (#%d)", comp.Token, comp.ID)
}
return fmt.Sprintf("Unknown component %s (#%d)", comp.Token, comp.ID)
}

func addCreatedComponents(r *http.Request, ct store.ComponentType) (int64, error) {
createdAt, err := time.Parse("2006-01-02", r.FormValue("createdAt"))
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/mycolog/tmpl/add.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ <h1>Add created {{$typeName}}</h1>
<label for="parents">Parents (input component IDs)</label>
<div class="pure-g" id="parents">
<input list="possible-parents" name="parent1" class="pure-u-11-24"
autocomplete="off" onchange="registerChange()" required>
autocomplete="off" onchange="registerChange()" required
{{if .PrefilledParent}}value="{{.PrefilledParent}}"{{end}}>
<div class="pure-u-1-12"></div>
<input list="possible-parents" name="parent2" class="pure-u-11-24"
autocomplete="off" onchange="registerChange()">
Expand Down
14 changes: 10 additions & 4 deletions cmd/mycolog/tmpl/details.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,22 @@ <h1>{{$typeName}} {{.Token}}</h1>
</button>
</fieldset>
</form>
{{if .Graph}}<hr>{{end}}
<hr>
</div>
<div class="pure-u-1 pure-u-md-1-24 pure-u-lg-1-8 pure-u-xl-1-4"></div>
</div>
{{if .Graph}}
{{if not .FullGraph}}
<div class="centered">
{{if and .Graph (not .FullGraph)}}
<a class="pure-button" href="?fullgraph=true">&uarr; Show all predecessors</a>
{{end}}
<span id="addComponents" class="pure-button-group" role="group" aria-label="Add component">
<a href="/add-spores?from={{.ID}}" class="pure-button">Add Spores</a>
<a href="/add-mycelium?from={{.ID}}" class="pure-button">Add Myc.</a>
<a href="/add-spawn?from={{.ID}}" class="pure-button">Add Spawn</a>
<a href="/add-grow?from={{.ID}}" class="pure-button">Add Grow</a>
</span>
</div>
{{end}}
{{if .Graph}}
<div class="centered">{{.Graph}}</div>
{{end}}
{{end}}

0 comments on commit 152ce0a

Please sign in to comment.